Community
cancel
Showing results for 
Search instead for 
Did you mean: 
forsjh
Beginner
77 Views

fputs in for loop

Sorry if I'm asking inthe wrong place. I am opening a file, going into a for loop, and then writing to the file. Problem is the only way I can get this to work is by opening the file within the for loop. I started with fputs, and tried fflush to no avail. Then moved on to fprintf. Here is what I have, this "works" but isn't very pretty. Also, I cannot put the fclose in the for loop. I get no file written when the closed is in or the open is out of the for loop. Any suggestions?
Thanks!
Hope

#define OPTIONS "a+"

int i;

FILE *fd;

strcpy_s(lictmpfile,12,"r5lic");

fd = fopen(lictmpfile,"w");

if (fd == NULL) {

printf("Error creating %s\\n", lictmpfile);

return -1;

}

fclose(fd);

for(i=0; i

fd = fopen(lictmpfile,OPTIONS);

if (fd == NULL) {

printf("Error opening %s\\n", lictmpfile);

return -1;

}

fprintf(fd,table.EncodedString);

fprintf(fd,"\\n");

}

fclose(fd);

return 0;

0 Kudos
1 Reply
Judith_W_Intel
Employee
77 Views


I tried to recreate your problem. Notice I put the fopen and fcloseoutside the for loop and it works as expected.

!% cat t.c

#include
#include

#define OPTIONS "a+"
int i;
FILE *fd;
const int NumLicenses = 4;

int main() {
char lictmpfile[12];

strcpy_s(lictmpfile,12,"r5lic");
fd = fopen(lictmpfile,"w");
if (fd == NULL) {
printf("Error creating %s\n", lictmpfile);
return -1;
}
fclose(fd);

fd = fopen(lictmpfile,OPTIONS);
if (fd == NULL) {
printf("Error opening %s\n", lictmpfile);
return -1;
}

for(i=0; i fprintf(fd,"foo");
fprintf(fd,"\n");
}

fflush(fd);
fclose(fd);
return 0;
}

!% icl t.c && ./t.exe
Intel C++ Compiler XE for applications running on IA-32, Version Mainline Bet
a Build x
Built Apr 2 2012 21:38:41 by jward4 on JWARD4-DESK in D:/workspaces/cfe/dev
Copyright (C) 1985-2012 Intel Corporation. All rights reserved.

t.c
Microsoft Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.

-out:t.exe
t.obj
!% cat r5lic
foo
foo
foo
foo

Please provide a complete compilable example of what isn't working as you expect.

thanks
Judy

Reply