EOF delete first cahracter

May 21, 2012 at 1:13pm
hi guys...
i have problem about EOF.
i'm sorry if my english doesn't good..

i'm working for make a program who can write file text like copy con (i think) in windows..

this is code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    char teks[9999];
    char path[1024];
    char fname[1024];
    char* location;
    FILE* files;
    int c;
    cout<<"your path file: "; cin>>path;
    cout<<"your file name: "; cin>>fname;
    location=strcat(path,fname);
   
   
   
    files=fopen(location, "w");
    if(!files)
    {
        cout<<"cannot opened files..\n";
        
    }
    else 
    {
        while(getchar()!=EOF){
            
           gets(teks);
           fputs(teks, files);
           fputs("\n", files);
        }
        
    }

    fclose(files);
    cout<<"   file created..."<<endl<<endl;
}



OK. when this execute. this program work correctly (i think). but, when I look the text file the firs char is deleted.
for example:
I input

hello...
learning cpp is great experience
so you must try it


then when I opened the text file, it will become

hello...
earning cpp is great experience
o you must try it


i'm really confused...
please help me..
May 21, 2012 at 1:18pm
getchar() reads one char.
Last edited on May 21, 2012 at 1:18pm
May 21, 2012 at 1:21pm
mmm.. i'm sorry
what do you mean..
sorry...
May 21, 2012 at 1:23pm
That means that getchar() removes the first character and gets() gets the rest. So the first character is gone (like observed)
May 21, 2012 at 1:34pm
owww...
so, what i can do?
i want make that program...
what the solution?
please help me.. this is for my project in school..

oh...i'm forgot, why getchar() remove first char form gets?
thanks for answer...:)
May 21, 2012 at 11:33pm
my first problem is fixed..
thanks guys...

I have a problem again...
the output file
why is like this


[empty line]
hello...
earning cpp is great experience
o you must try it


my quetion is how to remove the empty line in the first line
plese... help me
thx,
sorry
May 21, 2012 at 11:48pm
What's your final code?
May 22, 2012 at 12:20am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    char teks[9999];
    char path[1024];
    char fname[1024];
    char* location;
    FILE* files;
    char c;
    
    cout<<"your path file: "; cin>>path;
    cout<<"your file name: "; cin>>fname;
    location=strcat(path,fname);
   
   
   
    files=fopen(location, "w");
    if(!files)
    {
        cout<<"cannot opened files..\n";
        
    }
    else 
    {
        while((c=getchar())!=EOF){
           gets(teks);
           fputc(c, files);
           fputs(teks, files);
           fputs("\n", files);
        }
        
    }
    fclose(files);
    cout<<"   file created..."<<endl<<endl;
}


that my latest code
May 22, 2012 at 12:25am
fputc(c, files);
c is always '\n'. Because of: c=getchar(). If you press return, it will print a newline to the file. Eventually:
1
2
3
4
5
6
7
8
c = 0;
do {
    gets(teks);
    if(c)
        fputc(c, files);
    fputs(teks, files);
    fputs("\n");
} while((c = getchar()) != EOF);

Or simply comment out the "fputc" line.
May 22, 2012 at 12:38am
i try and out put is same before.
fputs("\n");
error

i try for change fputs become fputs("\n", files);
the output still same
May 22, 2012 at 12:42am
Wait. gets attaches a \n character by itself. You can comment the fputs("\n",files) line.
EDIT: Or was it the getline? Check the References Section eventually.
Last edited on May 22, 2012 at 12:43am
May 22, 2012 at 1:00am
i try to commnet the fputs("\n", file)
but the gets not attach a "\n" char by itself
Topic archived. No new replies allowed.