im trying to learn c/c++ and I was creating a 'append' function, it will append the 'inFile' text to the 'outFile', creating 'outFile' if it doesnt exist.
int AppendFile(const char * inFIle, const char * outFile)
{
FILE * ptrInputFile = fopen(inFIle, "rb"); /*Open first file*/
if (ptrInputFile == NULL)
{
fprintf(stderr, "Error opening \"%s\"", inFIle); /*Output to cerr*/
return -1;
}
FILE * ptrOutputFile = fopen(outFile, "ab"); /*Open second file*/
if (ptrOutputFile == NULL)
{
fclose(ptrInputFile); /*Close inFile that was openned in lines above*/
fprintf(stderr, "Error opening \"%s\"", outFile); /*Output to cerr*/
return -1;
}
/*read char by char and append.*/
while (1)
{
char tempChar = fgetc(ptrInputFile); /*Read char in stream*/
if (feof(ptrInputFile)) /*Check if EOL if so, break*/
{
break;
}
fprintf(ptrOutputFile, "%c", tempChar); /*Send character to output stream*/
}
/*Close files.*/
fclose(ptrInputFile);
fclose(ptrOutputFile);
return 0; /*Return successfully*/
}
Code above works, but my first iteration was:
/*read char by char and append.*/
while (1)
{
if (feof(ptrInputFile)) /*Check if EOL if so, break*/
{
break;
}
char tempChar = fgetc(ptrInputFile); /*Read char in stream*/
fprintf(ptrOutputFile, "%c", tempChar); /*Send character to output stream*/
}
but having the fget in there puts a 0xFF between both files, how can it be? to my understanding 'feof' sniffs the current caracter in the strean to check for EOF, and 'fget' returns the character and advances the pointer, right? so my logic was, check first for and EOF so you dont write the EOF to the stream, but the result was a 0xFF in the stream, how can it be?
to my logic the following code will do:
while (1)
{
char tempChar = fgetc(ptrInputFile); /*Read char in stream*/
if (feof(ptrInputFile)) /*Check if EOL if so, break*/
{
break;
}
fprintf(ptrOutputFile, "%c", tempChar); /*Send character to output stream*/
}
get the char and then sniff and then write it, so at the last character, it will read it and sniff the EOL and exit so the last character will be lost, how can it be that I'm not loosing any characters?
Thank you for your help.
PS. I know i can use the fstream classes for c++ but the lesson im taking is on fopen, fclose and fprint so I had to use those.
EDIT: To me is counter intuitive the way the algorith works, so maybe Im not seeing something, for example, having the code:
while (1)
{
if (feof(ptrInputFile))
{
break;
}
char tempChar = fgetc(ptrInputFile);
fprintf(ptrOutputFile, "%c", tempChar);
}
The file contains 'H','EOF', 'ptrInputFile' will point to 'H', while iterations are as follows: 1. H is EOF? No, continue, copy 'H' to tempChar and advance pointer (fgetc does this), write to ptrOutputFile the 'H. 2. 'ptrInputFile' now points to 'EOF, 'EOF' is EOF, yes break.
For some reason this code is writing me an extra character, a 0xFF.
The code that works fine is:
while (1)
{
char tempChar = fgetc(ptrInputFile);
if (feof(ptrInputFile))
{
break;
}
fprintf(ptrOutputFile, "%c", tempChar);
}
like the example above, file contains 'H','EOF', ptrInputFile points to 'H', iterations are as follows (at least what I understand): 1. copy 'H' to tempchar and advance the pointer 1, now ptrInputFile points to EOF, is ptrInputFile EOF? yes, ok break.
So to me it should never write anything but it does, my question is on how exactly feof and fgetc works.