Printing to a line using ofstream

This is a discussion on Printing to a line using ofstream within the C++ Programming forums, part of the General Programming Boards category; I am trying to output to a certain line in a file using ofstream. My file is automatically loaded with ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Question Printing to a line using ofstream

    I am trying to output to a certain line in a file using ofstream. My file is automatically loaded with certain numbers, and I need to exchange one number at a time. For example, it originally outputs:

    2400
    0000
    0000
    0000

    2500
    0000
    0000
    0000

    and I need to change it to:

    2400
    0000
    0000
    0000

    2500
    0000
    1700
    0000

    I originally thought to use seekp, but I cannot seem to make it work correctly. Any ideas?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    If your line data is always of a fixed size then you can calculate an offset to use with seekp and jump to a given line and then overwrite the existing data at that location.

    If your having issues with seekp then we'd need to see some code to determine if you're using it correctly or not.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Im in the middle of adjusting it right now, so I can't print it right now. I can give more details though. Basically I am trying to use an employee ID to keep track of a clockin/clockout/overtime. Since ofstream likes to create a file all the time, it was wiping my data file during every run. So I decided to copy the data to another file, then rename it to the old file after I am done adjusting it.

    employee IDs are 8 characters long. (ie 24001000). The following three lines are default at 0000. Then the next ID number is listed. I want my program to be able to recognize the ID and then print the changes on the correct line below the ID. So:

    (ID: ) 24006100
    (Overtime: ) 0000
    (Time In: ) 0000
    (Time Out: ) 0000

    If the employee clocks in at 5PM, I want to save that data. So:

    (ID: ) 24006100
    (Overtime: ) 0000
    (Time In: ) 1700
    (Time Out: ) 0000

    I was using the site seekp - C++ Reference to try to figure out how to use seekp/tellp. I've never used it before, so I am trying to manipulate the formula there to suit my needs.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Zinsky View Post
    I've never used it before, so I am trying to manipulate the formula there to suit my needs.
    Do you have a question?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    My question is if tellp can be used for my situation. And if so, how do you manipulate tellp to go down lines rather than forward character lengths.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Zinsky View Post
    My question is if tellp can be used for my situation. And if so, how do you manipulate tellp to go down lines rather than forward character lengths.
    tellp tells you current position.
    you do not "manipulate tellp"

    you can use getline to read whole line from the file and then - use tellp to learn the current position...

    in that way you can learn at what positions end each line of the text file...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You can use getline 4 or 5 times to read and process a single employee's set of data and after each set of read ops you could use tellg (not tellp) to determine where you are. If you've just read the user whose data you need to manipulate (check the employee id), you can then back up an appropriate amount (use seekp and some subtraction) to get to the proper place. You can then write your new data. This all depends on the employee data all being fixed length and you opening your file in read/write mode.

    Or... load everything in the file into memory using an STL container (vector/linked list/etc...) and manipulate it there. After each manipulation of the data in memory - or once at the end of the program - you can flush the changed data to the file overwriting what was there.

    [edit]Or maybe seekp/seekg and tellp/tellg can be used interchangeably I'll need to look into that.[/edit]
    Last edited by hk_mp5kpdw; 04-16-2010 at 11:56 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    you will be working with a small amount of data and memory wont be a problem so lets not try to over thinnk the problem mmm kay? There is no advanced functions for files that are messed up or anything fancy like that, you write it. I am not sure about the extra three zeros or wierd stuff, So i am gonna go with it. K?

    Just do this and you will be happy happy joy joy ok?

    struct employees{
    unsigned int id, ot, in, out;
    };
    list<employees> empdata;


    bool load(){
    ifstream file("employe.dat");// this will open it in text mode, dont do binary , you dont need it
    if(!file) {
    cout<<"cannot find file"<<endl;
    return false;
    }
    emploees temp;
    while(file){// while we have not reached the end of the file and its state is still Goood
    file>>temp.id>>temp.ot>>temp.in>>temp.out;
    empdata.insert(temp);
    }
    file.close();
    return true;
    }
    bool save(){
    ofstream file("employe.dat");// this will clear the file by default but it doesnt matter unless you are writing MASSIVE amounts of data, like more than 10 to 20 megs. Even then the difference inst noticeable, maybe a second more to save
    if(!file) {
    cout<<"cannot open file"<<endl;
    return false;
    }
    for(list<employees>::iterator beg(empdata.begin()); beg!= empdata.end(); ++beg){
    file<<beg->id<<beg->ot<<beg->in<<beg->out;
    }
    file.close();
    return true;
    }

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    [code][/code] tags...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    1

    Red face Re: Printing to a line using ofstream

    Hi,

    I'm trying to use a function to write information to a file.

    Let's say I have a function called WriteStuff that will simply print the line number on each line.

    Code:
    void WriteStuff()
    {
       cout <<"Line 1" << endl;
       cout <<"Line 2" << endl;
    
    }
    In main, I have a ofstream object called OutPutFile.

    How can I use OutPutFile and store the information from the function in a file?

    Code:
    int main()
    {
      ofstream  OutPutFile;
      
      OutPutFile.open("StoreText.txt");
    
      OutPutfile << WriteStuff();
        // I dont know if this is correct
    
      return 0;
    }

    Thanks
    __________
    Techstore are specialists in a range of Printing Services

  11. #11
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    A bit off topic, but ok...
    If you want to use a function that way with chars I am assuming someone else may have a better way I am sure but you could return a char * and save it that way...
    Code:
    
    char *WriteStuff()
    {
       char *foo;
       foo = new char[LENGTH];
       strcpy(foo,"I AM A STRING!");
       return foo;
    }
    
    int main()
    {
       ofstream save;
       save.open("blah.txt");
       save<< WriteStuff();
    }
    Remember to delete foo when you are done with it though...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. line number on a rich edit control
    By rakan in forum Windows Programming
    Replies: 1
    Last Post: 02-18-2008, 06:58 AM
  2. unable to read double A[0] and A[1] when n=1
    By sweetarg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 12:35 PM
  3. printing out a line from text file..
    By loso44x in forum C Programming
    Replies: 3
    Last Post: 10-22-2005, 01:14 PM
  4. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

Tags for this Thread


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