I need some help with a couple of problems if there is anyone here who would be kind enough to help me. Let me provide some background information first. I am a relatively new programmer. I am not in school but rather am learning programming in C++ on my own from a book entitled "Computer Science A Structured Approach Using C++" written by Behrouz A. Forouzan and Richard F. Gilberg.
So far I have covered the basics up to and including selection/decision operations, and repetition. I am now on a chapter that covers basic file input/output.
At the end of each chapter are several problems for the reader to work on.
The first problem I am having difficulty with is as follows:
Chapter 7 Problem 19,
Write a function that appends one file at the end of the other.
I have done a little research on my own and have discovered that you can use the app flag to accomplish this, however the book I am using has not yet covered the app flag so I must assume they do not want you to use it yet.
The only alternitive I can come up with involves opening both the file to append and the file to be appended as input files and creating a new output file. The way the problem is stated is vague but I belive that would fulfil the requirements of the problem, however if anyone has any other ideas that does not involve creating another problem that would be preferable to me.
The second difficulty I am having is in Chapter 7 Problem 23 which states: Write a function that deletes the last line of any file, by copying all but the last line to a new file.
My code so far follows. I am having problems when I compile and run my code in the second while loop in the
newfile() function. (Note: I have not added the code to tell the program not to copy the last line yet.) When I run the program It creates an output file but there is no text in the file. I have spent hours racking my brain trying to figure out why but I still cannot figure it out. Any help would be appreciated.
CODE
/*****************************************************************************
Chapter 7 Problem 23
Write a function that deletes the last line of any file by copying all but
the last line to a new file.
Written By:
Date:
******************************************************************************/
#include<iostream>
#include<fstream>
using namespace std;
void newfile(ifstream& infile, ofstream& outfile);
int main()
{
ifstream infile;
ofstream outfile;
infile.open("The Gettysburg Address.txt");
if(!infile)
{
cerr << "\aError 100 opening file: "
<< "The Gettysburg Address.txt\n";
exit(100);
}// if
outfile.open("output_file.txt");
if(!outfile)
{
cerr << "\aError 102 opening file: output_file.txt\n";
exit(102);
}// if
newfile(infile, outfile);
return 0;
}//main
void newfile(ifstream& infile, ofstream& outfile)
{
char cursor;
int lineCount = 0;
while(infile.get(cursor))
if(cursor == '\n') lineCount++;
if(cursor != '\n') lineCount++;
int linesWritten = 0;
while(infile.get(cursor))
{
outfile.put(cursor);
if(cursor == '\n')
linesWritten++;
} //while
}//newfile