Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fprintf question

Status
Not open for further replies.

countdrak

Programmer
Jun 20, 2003
358
US
The following code works fine.
Code:
FILE *result;
result = fopen ("results.txt", "r"); 
fprintf(result, "raw array is as follows\n");//line 1
for (k=0; k<100; k++)
{
   temp[k] = raw[k];
   fprintf(result, "%lf\n", raw[k]);
}
fclose(result);
[\code]

k, raw and temp are initialized and everything. Works fine.

But the following code
[code]
FILE *result;
result = fopen ("results.txt", "r");
fprintf(result, "raw array is as follows\n");//line 1
for (k=0; k<100; k++)
{
   temp[k] = raw[k];
   if(raw[k] !=0)
   {
      fprintf(result, "%lf\n", raw[k]);
   }
}
fclose(result);
[\code]

This does not even print line 1. I get an empty file. Any clues? Im tearing my hair over this.
 
I don't see the difference in the two code samples. Did you copy/paste correctly? (Also, it's a "[ignore][/code][/ignore]" to terminate the code block, not a "[ignore][\code][/ignore]").

First, you are opening the file for "read" only. Change your [tt]fopen[/tt] to...
Code:
result = fopen ("results.txt", "w");
Hope this helps.
 
Well, it seems that was not the problem, "r" was a typo in my post..should have been "w". Thanks Dave for that faq. Very interesting.
 
So you want help with code that you haven't posted for us?

Can you post the real code?
 
No, I figured it out. Pointer problem. :) Thanks for your help though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top