The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Outputting to a text file...
Discuss Outputting to a text file... in the C Programming forum on Dev Shed. Outputting to a text file... C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 9th, 2004, 11:51 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Outputting to a text file...
Hello,
I'm having trouble getting this bit of code to work properly..
What its supposed to do, is get all the data that is entered and output it into a sort of table, in a text file...
my problem tho, is that im getting really wierd outputs and i don't really know what's wrong.
Yes, i'm new to C.. and this is the 1st time i've tried doing anything like this.. so forgive my lack of knowledge
Any help would be greatly appreciated.
Thanks,
Mark
PHP Code:
/*
file: student.cpp
date: 8th June 2004
*/
#include <conio.h>
#include <stdio.h>
int main ()
{
char name[21];
int score, count;
float average, total;
FILE *fp;
fp = fopen("scores.txt","w");
printf("Enter Name: ");
gets(name);
fprintf(fp,"Name Score Score Score Score Score Average\n");
fprintf(fp," 1 2 3 4 5 \n");
fprintf(fp,"---------------------------------------------------------------------------------------\n");
while(name[0] != NULL)
{
fprintf(fp,"%s", name);
total=0;
count=0;
printf("Enter Score: ");
scanf("%d", &score);
while(score != -1)
{
total += score;
++count;
if(count<5)
{
printf("Enter Score: ");
scanf("%d", &score);
}
else
score = -1;
}
average = total / count;
printf("%.2f\n", average);
fflush(stdin);
printf("Enter Name: ");
gets(name);
}
fprintf(fp,"%s %d %d %d %d %d %2.2f\n", name, score);
fclose(fp);
return 0;
}
|

June 10th, 2004, 12:15 AM
|
 |
Contributing User
|
|
|
|
Sorry, I didn't try to build it -- but the following links jump to mind.
|

June 10th, 2004, 12:20 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
while(name[0] != NULL)
NULL is a zero pointer.
name[0] is a char .
name is a pointer to a char array, but it does have a non-NULL value.
You appear to be wanting to test for name not being an empty string. This could be done with these possible lines:
while(name[0] != '\0') /* is first char the null-terminator? */
while(strcmp(name,"") /* compare name with an empty string */
while(strlen(name)) /* if length isn't zero, is not empty */
strlen() and strcmp() both require the string.h header file.
Also, are you sure that you're opening the file in text mode. There's a global filemode variable that defines the default. Personally, I always explicitly open my files with "wt" or "wb".
And gets does not protect against buffer overflow, making it a security hole just waiting to happen, or at least a way to crash the program unexpectedly. It is preferable to use fgets with the stdin standard file. However, be aware that fgets does not remove the '\n' from the input string like gets does.
Last edited by dwise1_aol : June 10th, 2004 at 12:26 AM.
|

June 10th, 2004, 05:40 AM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
your program produces the wrong file because the fprintf() is in the wrong place.
Code:
fprintf(fp,"%s %d %d %d %d %d %2.2f\n", name, score);
average = total / count;
printf("%.2f\n", average);
fflush(stdin);
printf("Enter Name: ");
fgets(name,sizeof(name),stdin);
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|