Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,412 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,456 people online right now. Registration is fast and FREE... Join Now!




text file problem

 
Reply to this topicStart new topic

text file problem, cant close

rabbitvt
15 Oct, 2008 - 03:47 AM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 22

cpp

#include<stdio.h>
int main (void)
{
FILE *fpText;
int c;
int closeStatus;

printf("This program copies your input to a file.\n");
printf("When you are through,enter <EOF>.\n\n");

if(!(fpText=fopen("P7-07.DAT","w")))
{
printf("Error opening FILE1.DAT for writing");
return (1);

}
while ((c=getchar()) !=EOF)
fputc(c,fpText);

closeStatus=fclose(fpText);
if(closeStatus==EOF)
{
printf("Error closing file\a\n");
return 100;
}

printf("\n\nYour file is complete\n");
return 0;
}


why i cant type <EOF> to close my file??what have happen??thanks.

** Edit ** code.gif
User is offlineProfile CardPM
+Quote Post

schnalf
RE: Text File Problem
15 Oct, 2008 - 03:57 AM
Post #2

D.I.C Head
**

Joined: 9 Feb, 2008
Posts: 124



Thanked: 2 times
My Contributions
your code is working. getchar() is not retuning EOF by typing <eof> but by pressing strg + d.
User is offlineProfile CardPM
+Quote Post

rabbitvt
RE: Text File Problem
15 Oct, 2008 - 05:22 PM
Post #3

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 22

how i know by pressing strg +d??
thanks.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Text File Problem
15 Oct, 2008 - 05:27 PM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,462



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
In the following statement :

CODE
while ((c=getchar())  !=EOF)


EOF is not the same as the characters uppercase E uppercase O uppercase F. If is a predefined character that represents the end of the file. So the while conditional test will check if getchar is passing a character value to c that does not hold the value of EOF which means, the end of the file.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:31PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month