| View previous topic :: View next topic |
| Author |
Message |
407 Master Cheater
Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Sun Jan 23, 2011 4:57 pm Post subject: fprintf in C help |
|
|
| Code: | void results(FILE* in, char studentAns[], char ansKey[], int studentId[])
{
FILE* out;
out = fopen("grades.txt", "w");
if(out == NULL)
{
printf("Error opening file.\n");
}
fprintf(out, "ID\tNumber Correct\tPercent");
return;
} |
It's not printing for some reason.. I can't seem to see what the problem is..
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sun Jan 23, 2011 5:45 pm Post subject: |
|
|
It works fine. I just made this short program:
| Code: | #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
FILE* out;
out = fopen("grades.txt", "w");
if(out == NULL)
{
printf("Error opening file.\n");
}
fprintf(out, "ID\tNumber Correct\tPercent");
fclose( out );
return 0;
} |
Something to note, you are missing a fclose() at the end.
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Sun Jan 23, 2011 5:57 pm Post subject: |
|
|
If you don't close the file handle, I don't think it will write to the file. I had that issue in java the other day.
_________________
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sun Jan 23, 2011 6:00 pm Post subject: |
|
|
| Nope it works without the fclose. Also your check for the fopen is good practice but not wrapping the rest of it in an else or making it return in the case of error is not.
|
|
| Back to top |
|
 |
407 Master Cheater
Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Sun Jan 23, 2011 8:34 pm Post subject: |
|
|
| Slugsnack wrote: | | Nope it works without the fclose. Also your check for the fopen is good practice but not wrapping the rest of it in an else or making it return in the case of error is not. | That's what I thought..
I need 2 parallel files open, because I can't re-start where I left off if I close the file, right?
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Jan 23, 2011 11:08 pm Post subject: |
|
|
| Two wrote: | | Slugsnack wrote: | | Nope it works without the fclose. Also your check for the fopen is good practice but not wrapping the rest of it in an else or making it return in the case of error is not. | That's what I thought..
I need 2 parallel files open, because I can't re-start where I left off if I close the file, right? |
If you wrote to the end of the file just set the file pointer to the end. Look into:
fseek / fsetpos
_________________
- Retired. |
|
| Back to top |
|
 |
|