I have this code:
Code:
#include <stdio.h>
#include "useful_functions.h"
int main(int argc, char *argv[]){
//[row][column][chars]
char table[300][20][200];
//longest entry in column
int max[20];
int row,column,chars;
row = column = chars = 0;
int ocolumn;
FILE *fp = fopen(*++argv, "r");
char c;
if (fp != NULL){
while ((c=getc(fp)) != EOF){
if (c == '\n'){
row++;
chars = 0;
ocolumn = column;
column = 0;
}else if (c == ' '){
int length = strlen(table[row][column]);
if (length > max[column]){
max[column] = length;
}
column++;
chars = 0;
}else{
table[row][column][chars++] = c;
}
}
int i,j;
for (i=0;i < row; i++){
for (j=0;j <= ocolumn;j++){
printf("| %s ", table[i][j]);
pad(max[j] - strlen(table[i][j]));
}
printf("|\n");
}
}else{
printf("Could not open file\n");
}
}
"useful_functions":
Code:
void pad(int numofspaces){
int i;
for (i=0;i<numofspaces;i++){
printf(" ");
}
}
when passed this file:
it works, apart from it doesn't pad the second field.
When passed this file:
Code:
Name Age Score
Matthew 100 12
it prints out loads of spaces/newlines...
Bookmarks