c - function using fopen() keeps opening the same file, even if given different filenames as parameters -


i'm trying open multiple files sequentially, using fopen(), printing contents , counting chars. in main() call function twice using different filenames function prints contents of first file twice. both files exist, of course, , calling function once print correct contents; tested both files. there seems problem when calling function multiple times.

can please give me hint? wrong?

my function:

void open_and_read(char file_name[80]){          char c;     int buf_length = 16384;     char buf[buf_length];     char* buf_pointer;     int = 0;     file *file_pointer;     file_pointer = fopen(file_name,"r");     buf_pointer = buf;     if(file_pointer==null){         printf("error: file %s not opened!", file_name);     }     else {         while(c!=eof && i<(buf_length-1)){             c = fgetc(file_pointer);             *buf_pointer = c;             buf_pointer++;             i++;         }          buf_pointer--;         *buf_pointer = 0;         printf("the file %s contains follwing: \n%s", file_name, buf);         printf("char-count: %i\n", i);     }     if(fclose(file_pointer)!=0){         printf("file not closed!");     }     else {         printf("file closed!");      } } 

main():

int main(){     open_and_read("/home/x201/dev/ib/data/line1.dat");     open_and_read("/home/x201/dev/ib/data/line2.dat"); 

char c; 

this not initialised anywhere. try char c = '\0' .

edit:

it appropriate if use

   while(((c = fgetc(file_pointer))!= eof) && i<(buf_length-1)) 

there won't need initialise c. receive value fgetc before enter loop 1st time.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -