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;the content of the file i am trying to read is something like this: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; }
hello 56
programming 32
windows 32
CHEERS..



5Likes
LinkBack URL
About LinkBacks


