Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,100 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,266 people online right now. Registration is fast and FREE... Join Now!




Appending Files and other problems

 
Reply to this topicStart new topic

Appending Files and other problems

prisoner_of_perception
post 7 Oct, 2008 - 03:51 PM
Post #1


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 1

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

User is offlineProfile CardPM

Go to the top of the page

GWatt
post 7 Oct, 2008 - 04:31 PM
Post #2


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,150



Thanked 16 times

Dream Kudos: 450
My Contributions


You have to close the file streams for the changes to take place.
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 09:22AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month