-
reading a file
i am new to c,
so i am writing this function that reads the content a file into an array of struct,, the code compiles perfectly fine but when i run the program it outputs something like this..
"Gj1323193950166804700000000000000000000000000000 000000000000000000000000000000"
so here is my code.,
Code:
/*************
test.h
**************/
typedef struct{
char commandName[10];
double comValue;
}fileCommand;
Code:
/*************
test.c
**************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "test.h"
FILE* f;
//prototype
int numLine(FILE* fileName);
int main(int argc, char* argv[]){
int lenght, i;
f = fopen(argv[1], "r+");
if( f != NULL){
lenght = numLine(f);
printf("number of lines : %d\n", lenght);
fileCommand* fCommand = malloc(sizeof(*fCommand) * lenght);
for(i=0; i<(lenght); i++){
fscanf(f, "%s %lf\n", &(fCommand->commandName), &(fCommand->comValue));
printf("%s %lf", fCommand->commandName, fCommand->comValue);
}
fclose(f);
free(fCommand);
}
else{
perror("error opening file");
}
return 0;
}
int numLine(FILE* fileNames){
int numLines=0;
char ch;
do{
ch = fgetc(fileNames);
if(ch == '\n')
numLines++;
} while (ch != EOF);
if(ch != '\n' && (numLines) != 0){
numLines++;
}
return numLines;
}
the content of the file i am trying to read is something like this:
hello 56
programming 32
windows 32
CHEERS..
-
Your numlines() function reads until the end of the file. Then, starting from end of file, your main() function attempts to read multiple lines. main() is not doing any error checking, so will keep looping even if fscanf() reports an error - which it will, from the first call.
-
so once i have read until the end of the file using numlines(),, how do i get back to the start of the file??
-
-
so i added before in the numLines()... but it still outputs something like this..
"number of lines : 5
x7; 17513790332202264000000000000000000000000000000000 00000000000000000000000000"..
PLEASE HELP!
-
You read the file.
You rewind()
You read the file again.
Think about where you placed the rewind() call.
Was it between the two passes at reading the file?
-
Code:
if( f != NULL){
lenght = numLine(f);
printf("number of lines : %d\n", lenght);
rewind(f);
fileCommand* fCommand = malloc(sizeof(*fCommand) * lenght);
is this correct? !
-
I am still getting some random output,, like i mentioned in post #1,,,, please help..!!
-
Code:
$ gcc foo.c
foo.c: In function ‘main’:
foo.c:31:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[10]’ [-Wformat]
$ ./a.out foo.txt
number of lines : 4
hello 56.000000
programming 32.000000
windows 32.000000
windows 32.000000
$ cat foo.txt
hello 56
programming 32
windows 32
1. Your scanf format is wrong - you don't need the & when using %s
2. Your line count is wrong - it seems to count an extra line. This leads to the last record being duplicated.
3. "programming" is longer than the 9 (+1 for \0) you allow in your struct, which will trash data.
-
Or you could simply close and open the file again after the first read is through.
-
thank you all,, @salem, the numLines() works fine for me.. it gives me the exact number of lines..
@waltP, closing and opening the file worked,, thanks alot...
-