//////////////////////////////////////////////////////////////////////////////////
// Now I have an idea how to get my strings from the file so that I can properly
// play with them (with fstream I did not have the string "in my clutches" quite
// the way I wanted it). However, now I have a problem with my looping because
// this way, I am seeing the last line *TWICE* in my output, so feof must trigger
// from the error caused by reading past the end of the file or what just is the
// reason?
//
// Is there an elegant way to exit from this? My first thought was to use a
// do {...} while; but that does not solve it either. Maybe if there was a
// do { ... } until; Oh well. Maybe someone will have an idea? I am sure that
// I don't have a firm grip on this.
//
// Thank You for Your Kind indulgence (bad case of the newbies!).
//
// 73
// -Grace
// NNNN
// z

//////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#define BUFFERSIZE 256	// Make sure lines will not be longer than this

char LineBuffer[BUFFERSIZE];

int main ()
{
	FILE* outfile = fopen("MyOuput.txt", "w");
	FILE* infile =  fopen("ReadWriteLine.cpp", "r");
//	infile is this file since it changes and it's fun

	while (!feof(infile))
	{
		fgets(LineBuffer, BUFFERSIZE, infile);
		// lines will be truncated if longer than BUFFERSIZE		
		printf("%s", LineBuffer);	// now output to console
		fprintf(outfile, "%s",  LineBuffer); // and our file
	}

	fclose(infile);
	fclose(outfile);

	system("PAUSE");
	return(0);
}
//////////////////////////////////////////////////////////////////////////////////
// -eof- (This line is output *TWICE*)

However, now I have a problem with my looping because this way, I am seeing the last line *TWICE* in my output

This is precisely the reason why feof() shouldn't be used as the only loop condition. It only returns true after you've tried and failed to read from the stream, which means the last record will be processed twice. fgets() can be used directly as the loop condition because it returns NULL on any kind of failure (including end-of-file):

while (fgets(LineBuffer, BUFFERSIZE, infile) != NULL)
{
    printf("%s", LineBuffer);
    fprintf(outfile, "%s",  LineBuffer);
}

You need to check if fgets() returns the address of lineBuffer vs. NULL inside your loop, since when you read the last line, the eof flag on infile will not have been set. So, change your loop internals to this and see what happens:

while (!feof(infile))
{
    if (fgets(LineBuffer, BUFFERSIZE, infile) != NULL)
    {
        printf("%s", LineBuffer);
        fprintf(outfile, "%s", LineBuffer);
    }
}

From the fgets() man page:

fgets(s, size, fp) returns s on success, and NULL on error or when end of file occurs while no characters have been read.

You might also want to check for an error after reading the line, just in case... :-)

Edited 4 Years Ago by rubberman: n/a

//////////////////////////////////////////////////////////////////////////////////
// Solution: feof() depends on an error to occur. So to avoid the error, we
// check for the end in our loop by testing each input to see if it is the
// last by including the test when the data is read. Then the problem is
// fixed and the last line shows up *ONLY ONCE*.
//
// Thank You for Your Kind indulgence (bad case of the newbies!).
//
// 73
// -Grace
// NNNN
// z
//////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#define BUFFERSIZE 256	// Make sure lines will not be longer than this

char LineBuffer[BUFFERSIZE];

int main ()
{
	FILE* outfile = fopen("MyOuput.txt", "w");
	FILE* infile =  fopen("ReadWriteLine.cpp", "r");
//	infile is this file since it changes and it's fun

	while (fgets(LineBuffer, BUFFERSIZE, infile) != NULL)
	// lines will be truncated if longer than BUFFERSIZE
	{
		printf("%s", LineBuffer);	// now output to console
		fprintf(outfile, "%s",  LineBuffer); // and our file
	}

	fclose(infile);
	fclose(outfile);

	system("PAUSE");
	return(0);
}
//////////////////////////////////////////////////////////////////////////////////
// -eof- (This line is output *ONLY ONCE*)

Thanks everyone who answered this! I guess I figured it out to, so we have a
consensus and a good lesson learned.

Good luck to You all in the new years.

73
-Grace
NNNN
z

This question has already been answered. Start a new discussion instead.