Results 1 to 6 of 6

Thread: A somewhat of an advanced C++ question

  1. #1
    Senior Member
    Join Date
    May 2002
    Posts
    344

    Exclamation A somewhat of an advanced C++ question

    Alright guys this isnt security related but i have to turn in a program in about 5 hours to my instructor and i am stuck. None of the consultants know how to do it (which isnt very encouraging). Anyways i thought maybe someone here could give me some hints or push me in the right direction. Here is what the program has to do:

    Write a program that opens two text files for input and one for output. The program concatenates the corresponding lines of the input files, using a space as a seperator, and writing the reults to the output file. If one file is shorter than the other, the remaining lines in the longer file are also copied to the output file. For example, suppose the first input file has these contents:

    eggs kites donuts
    balloons hammers
    stones

    And suppose the second input file has these contents:

    zero lassitude
    finance drama

    Then the resulting file would have these contents:

    eggs kites donuts zero lassitude
    balloons hammers finance drama
    stones

    PLEASE HELP ME!!! Post as soon as you can!!!
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  2. #2
    Senior Member
    Join Date
    Jun 2002
    Posts
    394
    what about something like,

    getline( stream, string ) ;
    another_String = string ;
    getline( other_stream, string ) ;
    another_String += string ;
    out_stream << another_String ;

    obviously this is extremely rough and has no error checking and the like; so obvious is this that i wasn't even going to make this statement.

    but you said you wanted help (as opposed to the complete solution).
    and you wanted a post asap.
    and i'm for the bed.
    Hmm...theres something a little peculiar here. Oh i see what it is! the sentence is talking about itself! do you see that? what do you mean? sentences can\'t talk! No, but they REFER to things, and this one refers directly-unambigeously-unmistakably-to the very sentence which it is!

  3. #3
    Ok, I'd start by looking at the big picture, then worry about the details. First, you need to find out the length (in lines it looks like) of each file, and see if concatenation is necessary.

    eg.

    int size 1, size 2;
    string x;
    ifstream file1, file2;
    char ch;

    file1.open("text1.txt");
    file2.open("text2.txt");

    while(getline(file1, x) size1++;
    while(getline(file2, x) size2++;//you'd have to add code to check for eof

    Then check to see if concatentation is necessary, if it is, do what max said.
    Darwin\'s rollin\' over in his coffin, The fittest are surviving much lest often,
    Now everything seems to be reversing, And it\'s worsening!
    --nofx, American Errorist

  4. #4
    Senior Member
    Join Date
    May 2002
    Posts
    344
    Thanks for the quick responses guys, this is what i have so far, i will include comments:

    #include <iostream>
    #include <fstream>
    #include <cstdlib>

    using namespace std;

    const char *fileA="bat1.dat";//first text file with stuff in it
    const char *fileB="bat2.dat";//second text file with stuff in it
    const char *fileC="bat3.dat";//third text file needs to put first+second in it
    char ch;

    int main (void)
    {
    ifstream fin;
    ofstream fout;

    fin.open(fileA); //openning fileA
    if (!fin.is_open())//checks to see if file exists...
    { cerr << "Couldn't open file " << fileA << " \n"; //error:cant find the file
    exit(1);
    }
    if (fin.is_open())
    {
    fout.open(fileC);
    while(fin.get(ch))
    fout << ch; //this takes text from fileA and puts in into fileC
    fout.close();
    }
    fin.close(); //closing fileA

    (fileC, ios_base:ut|ios_base::app); //i am appending content from fileA to fileC

    fout.close(); //stop appending

    fin.open(fileB); //openning fileB
    if (!fin.is_open()) //cant find fileB
    {
    cerr << "Couldn't open file " << fileB << " \n"; //again error message
    exit(1);
    }
    if (fin.is_open())
    {
    fout.open(fileC);
    while(fin.get(ch))
    fout << ch; //takes the text from fileB to fileC
    fout.close();
    }
    fin.close(); //closing file B

    (fileC, ios_base:ut|ios_base::app);

    fout.close();
    } //end of main()



    Anyways, I can successfully append the content from fileA to fileC but then, when i have to append fileB to fileC i end up overwriting what was previously in fileC (which was the content from fileA). Anyways thanks guys, i really appriciated quick responses If you know how to fix my problem please post

    sorry about my last post and its length, but i realized that AO changed some of the code into smiley faces gotta love AO Anyways whereever there is a smiley face, this is what the code was suppose to say:

    ( fileC, ios_base :: out | ios_base :: app );

    i hope that works...
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  5. #5
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    Just a thought:

    Don't close fileC before testing/reading B. That way you don't have to re-open C and are able to continue where you left off. Close fileC at the end when everything is done.
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  6. #6
    Senior Member
    Join Date
    May 2002
    Posts
    344
    Close fileC at the end when everything is done.
    Thanks so much, i talk to my instructor, actually the program isnt do until next tuesday so i guess i busted myself for nothing...Anyways, close fileC in the end, thanks so much your post was really helpful
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •