The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
EOF Character being appended.
Discuss EOF Character being appended. in the C Programming forum on Dev Shed. EOF Character being appended. 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:
|
|
|

August 14th, 2004, 11:46 PM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 88
Time spent in forums: 4 h 24 sec
Reputation Power: 9
|
|
EOF Character being appended.
Hello again everyone!
I have made a program to append File1 and File2 inside File3 by putc and getc functions.
The problem is that the EOF character(ASCII Value 255, its eof char, right ?) is also being appended from File1 into File3.
I tried to delete it using backspace character and tried fflushall but the character is still appended, which I do not want to.
If you want to see the source, do tell so and I will post it here.
I am quite new to C programming, so please bear with my mistakes.
Thanx!
|

August 15th, 2004, 01:36 AM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Colorful Colorado
Posts: 743
Time spent in forums: 21 h 16 m 10 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
|
|
|
make your loops stop before the eof is written. post your code and I will show you what is happening. after you see, you should be able to fix the problem on your own.
|

August 15th, 2004, 02:52 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 88
Time spent in forums: 4 h 24 sec
Reputation Power: 9
|
|
Here ya go
PHP Code:
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
//Define Functions
int feed_file_one(); //Append File One into FileR
int feed_file_two(); //Append File Two into FileR
//Global Declarations
FILE *file1,*file2,*fileR;
//file1 = first-file
//file2 = second-file
//fileR = Result-File
int main(void)
{
clrscr();
feed_file_one();
feed_file_two();
flushall();
fcloseall();
fclose(fileR);
cout << "\aI got the work done, Sir! \n";
cout << "Exiting successfully...";
return 0;
}
int feed_file_one()
{
//Read data from file1 char by char and enter the data in fileR char by char
while (!feof(file1))
fputc(fgetc(file1), fileR);
fclose(file1);
//ungetc(255, fileR); -- Doesn't work -- 12/08-13:42
//flushall(); -- Doesn't work 14/08 -- 8:30
fflush(fileR);
return 0;
}
int feed_file_two()
{
//Read data from file2 char by char and enter the data in fileR char by char
while (!feof(file2))
fputc(fgetc(file2), fileR);
fclose(file2);
fflush(fileR);
return 0;
}
I have got all the program neatly running up and working with the only problem that in the FileR, where the contents of File1 end and the contents of File2 begin, a space is inserted which on being seen in Value view (EDIT /70 fileR.txt in DOS), the bit is shown of value 255. Is that the end-of-file char ?
Edit : Btw, is the use of [php] tags in C forum, right ?
|

August 15th, 2004, 02:57 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 88
Time spent in forums: 4 h 24 sec
Reputation Power: 9
|
|
Btw, that is not the whole code (which is quite big and messy) and I do get the files opened(file1 and file2) and created(fileR) neatly with the fopen and fcreate functions.
Yeah, I know I am mixing C and C++. But I don't know either of them nicely and just adopted the examples in the online help facility in Turbo C IDE, to my need. 
|

August 15th, 2004, 06:37 AM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
For files operated on in binary mode, eof is a condition and not a value (because any value might be data). In text mode there is usually an eof character. For Windows systems, at least, its value is 0x1a, decimal 26. It would likely be implementation dependent. I haven't looked at your code, yet.
EDIT: As is quite common, you're not testing for the EOF condition in such a way that it stops you BEFORE you use the 'data'. Here's your loop sequence:
[code]
while (!feof (file))
// Not at eof
fgetc //gets last character (doesn't set eof)
fputc // writes last character
while (!feof (file))
// Not at eof
fgetc // returns 0xff, to indicate eof (not an eof 'character')
fputc // writes the 0xff
while (!feof (file))
// detects the eof condition and stops -- too late.
[code]
You should test the returns of the functions you use; they have important things to tell you. Particularly when things go bad or fail. It is not misuse to condition your loop on eof, but it is often insufficient to get the job done properly.
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.
Last edited by DaWei_M : August 15th, 2004 at 06:50 AM.
|

August 15th, 2004, 12:40 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Colorful Colorado
Posts: 743
Time spent in forums: 21 h 16 m 10 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
|
|
what dawei is saying is that eof is true, you copy the eof marker, then eof is false. eof cannot see into the future. so you should check the value of get. and to do this in pure c++, here is how you would do so. the only thing to change in this one is to make sure that all the files open and report on their errors.
Code:
#include <fstream>
int main(int argc, char **argv)
{
std::ifstream file1(argv[1]);
std::ifstream file2(argv[2]);
std::ofstream file3(argv[3]);
char c;
while(file1.get(c))
file3.put(c);
while(file2.get(c))
file3.put(c);
file1.close();
file2.close();
file3.close();
return 0;
}
i would be happy to explain anything further, but it is quite a simple program. I am not sure if you will know what argv, argc mean.
I am so bored right now. here is the program with some error checking:
Code:
#include <fstream>
#include <iostream>
int main(int argc, char **argv)
{
if (argc != 4)
{
std::cerr << "Usage: " << argv[0] << " IN1 IN2 OUT" << std::endl;
return EXIT_FAILURE;
}
std::ifstream file1(argv[1]);
if (!file1)
{
std::perror(argv[1]);
return EXIT_FAILURE;
}
std::ifstream file2(argv[2]);
if (!file2)
{
std::perror(argv[2]);
return EXIT_FAILURE;
}
std::ofstream file3(argv[3]);
if (!file3)
{
std::perror(argv[3]);
return EXIT_FAILURE;
}
char c;
while(file1.get(c))
file3.put(c);
file1.close();
while(file2.get(c))
file3.put(c);
file2.close();
file3.close();
return 0;
}
Last edited by jubitzu : August 15th, 2004 at 01:15 PM.
|

August 15th, 2004, 01:07 PM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
Actually, your original code is C. You'll notice if you look that fgetc returns an integer and NOT a character. This is so that it can return a 0xff as a character (best to use an unsigned character to avoid any sign extension if subsequent operations should treat it as an integer) and a -1 (for a 32-bit integer, 0xffffffff) as an EOF indication and they'll be different. I say INDICATION; it's not always something read from the medium, but a condition that is detected when the driver bookkeeping knows there is nothing left to read. The character 0xff will NOT compare to EOF if it's not sign-extended. Make your loop like this:
Code:
while (!feof (readFile)) OR while (true)
{
c = fgetc (readFile);
if (c == EOF) break; // END OF FILE
status = fputc (c, writeFile);
if (status == EOF) break; // WE HAVE AN ERROR IN ATTEMPTING TO WRITE
}
// Now you're done with this input file. If you care about what caused the exit,
// you have c and status to test.
Jubitzu's code is using C++ streams. I would recommend you switch, but that's up to you. Whatever you use, read the docs for the functions. They're different. Regardless, you need to learn how to detect anomalous conditions, whether they're due to running out of data, improper usage, or other errors. Believe me, for example code and postings such as this, you might get away with sloppiness, but in real life a gator is gonna come along and bite off your toes right up behind your ears.
|

August 16th, 2004, 08:48 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 88
Time spent in forums: 4 h 24 sec
Reputation Power: 9
|
|
Great thanx jubitzu and DaWei_M for those detailed solution!
I am still contemplating on what solution to use, since I have really raw(meaning uncooked) knowledge of both C and C++. 
|

August 16th, 2004, 11:46 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 88
Time spent in forums: 4 h 24 sec
Reputation Power: 9
|
|
I included the C version of the solution, for timebeing.
The problem is I really don't know how to handle file streams(..they are still called streams, right ?) in CPP. The program is still 'mixed' because I don't know defining functions in C
(again !) and I really hope to resolve that soon.
Its working for now and I have the satisfaction of getting something in place :d
Great thanx to both of you for the wonderful help!
|
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
|
|
|
|
|