Thread: File I/O merging and adding

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    36

    File I/O merging and adding

    Ok, I already have file1.txt and file2.txt which are created by me. file3.txt will add a line inputted by the user to the end of file1.txt. file4.txt will merge file3.txt and file2.txt. I have the following portoin which I thought would add a line and create file3.txt but it doesn't. It DOES create file3.txt but writes nothing to it. What I have here is just the first portion of my problem, I believe if I can figure this part out, it shouldnt be that hard to figure out the merging.

    Code:
    (original code delete new one posted)
    Last edited by stevedawg85; 05-06-2006 at 01:00 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> O_hw7 << line << endl;
    You output that line before you open the file.

    Also, why do you specify ios::in for an output file?

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    Ok, i did what you said but no luck. Oh, and the bottom portion is something I'm supposed to have as well, so you can ignore it i guess. And you asked about ios::in, well like they say, if it aint broke, dont fix it. it works fine and i used a format similar to this for my last prog and it worked
    anyways, my continuing problem:
    I'm supposed to create file3.txt with the original contents of file1.txt in addition to the sentence(s) entered by the user.

    Code:
    string file= "HW7_1.txt";	
    string line;
    ifstream HW7_1txt;	
    ofstream O_hw7;	
    string sentence;
    char choice;
    
    O_hw7.open("HW7_3.txt", ios::in| ios ::app);		//Opens .txt file in input mode and append mode
    HW7_1txt.open (file.c_str());
    while (getline(HW7_1txt, line));
    O_hw7 << line << endl;		
    HW7_1txt.clear ();
    HW7_1txt.close ();		
    
    do
    {
    cout << "Enter a sentence" << endl;
    getline (cin, sentence);	
    			
    std :: cout << "The sentence to be added is: " << sentence << endl;
    O_hw7 << sentence<< endl;
    
    cout << "would you like to enter another sentence?" << endl;
    cin >> choice;
    cin.ignore();
    }
    while (choice == 'y' || choice == 'Y');
    O_hw7.close();

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    while (getline(HW7_1txt, line));         // remove the semicolon
         O_hw7 << line << endl;
    Kurt

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    whoa, something that small..... thanks so much! you probably saved me a couple of hours(seriously....me and this programming) from troubleshooting it, thanks so much!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM