i have to create a c file that reads values from text file, and passes them into an array. new to c.
file.txt to read from
1989 500 222000
1997 1500 180000
1976 4000 20000
1967 20000 10000
program should read table data from stdin. as follows (from the shell): % ./tsort file.txt
my code so far
int main( int argc, char *argv[] ) {
FILE *inputFile;
inputFile = fopen( argv[2], "r" );
int number;
while(fscanf(inputFile, "%i", number)==1)
{
printf("%i", number);
}
return 0;
}
Try this. Note the & on number and the checking of the inputFile for null. The command line should be "./tsort file.txt"
int main( int argc, char *argv[] ) {
FILE *inputFile;
int number;
if(argc != 2) {
printf("Usage: %s filename\n", argv[0]);
return 1;
}
inputFile = fopen(argv[1], "r" );
if(inputFile == 0) {
printf("Can't open '%s'\n", argv[1]);
return 1;
}
while(fscanf(inputFile, "%i", &number)==1)
{
printf("%i\n", number);
}
return 0;
}