When writing to a text file how do you...

This is a discussion on When writing to a text file how do you... within the C++ Programming forums, part of the General Programming Boards category; Erase all the text on a certain line? and then Write to this line? Code: ofstream mnfile(c:\temporary.txt, ios::ate); mnfile.getline(input, 255, ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    44

    When writing to a text file how do you...

    Erase all the text on a certain line?
    and then
    Write to this line?

    Code:
       ofstream mnfile(c:\\temporary.txt, ios::ate);
       
        mnfile.getline(input, 255, '\n');
        mnfile<<(manumb);
        mnfile.close();
    I tried using the getline function, but it isn't recognised within ofstream

    Thanks JamMan..
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Ok ofstream is an OUTPUT stream so you will not be able to use it to READ IN text.
    If you wish to open a file and change one full line do this
    What you need to do is open the file, and write to a new temp file.
    Replace the line that you want, then delete the orignal file and rename the temp file

    Code:
    ifstream input("file.txt");
    ofstream output("temp");
    string s;
    while( ! input.eof() )
    {
     input.getline( s,255,'\n');
     //if it the line to replace do it here
    
     output << s;
    }
    input.close();
    output.close();
    remove("file.txt");
    rename("temp","file.txt");
    there you go that should help

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    44
    Thats helped,
    but I'm getting a compiler error.
    Here's my code:

    Code:
        ifstream input("file.txt");
        ofstream output("temp");
        string s;
        int i = 0;
    while( ! input.eof() )
    {
    input.getline(s, 255, '\n');                 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
    if (i == 0) output << manumb;
    
    else output << s;
    
    i++;
    }
     input.close();
    output.close();
    remove("file.txt");
    rename("temp","file.txt");
    On the line with the !'s next to it I'm getting the compiler error??

    Its a problem using the getline with ifstream??


    Last edited by JamMan; 12-14-2001 at 01:45 PM.
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    hmm, try using the getline function

    Code:
    getline(input,s);
    i think it is in the string header file, and should work fine

  5. #5
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>input.getline(s, 255, '\n');



    s needs to be a character array and not a string.

    char s[256];

    ifstream_component.getline(s,255); // it assumes newline by default
    Last edited by Betazep; 12-15-2001 at 02:08 PM.
    Blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21