EOF delete first cahracter

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..
getchar() reads one char.
Last edited on
mmm.. i'm sorry
what do you mean..
sorry...
That means that getchar() removes the first character and gets() gets the rest. So the first character is gone (like observed)
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...:)
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
What's your final 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
    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
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.
i try and out put is same before.
fputs("\n");
error

i try for change fputs become fputs("\n", files);
the output still same
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
i try to commnet the fputs("\n", file)
but the gets not attach a "\n" char by itself
Topic archived. No new replies allowed.