Thread: Move to specified line in CSV file

  1. #16
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Thanks for suggestions!!

    Okay, so how about this:

    - I copy the file content in to memory (up until the line that I specify).
    - Delete the file (can i do this independent of platform?)
    - open up the file again of the same name.

    I dont want to rename because I am sending program over a network and it gets complicated. I dont want to open a new file because I want to continue to use the file handle I already have created for collecting data.

    Any ideas or examples for me to do this?
    Or alternative ideas?
    the easier the better!!

    thanks
    Last edited by strokebow; 07-02-2012 at 10:31 AM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by strokebow
    - Delete the file (can i do this independent of platform?)
    There's the remove function from <cstdio>, but...

    Quote Originally Posted by strokebow
    - open up the file again of the same name.
    Why not just use an open mode that truncates the file?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Thanks very much laserlight

    I like this idea of truncating the file. How would I go about that?

    thanks

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You could refer to jimblumberg's post #13.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by jimblumberg View Post
    You must use the correct open mode if you want to overwrite the information. The default (out) will always append to the end of the file. See this link for the open modes: ofstream.open. You may want to try ate.

    Jim
    Thanks for reply.
    Ok, so I use ios::ate instead of ios:ut

    But I am not sure how that will allow me to truncate the file?

  6. #21
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    So you read the portion of the file that you want to preserve using an ifstream. Now close this ifstream, and using the same file name create an ofstream. If you don't provide the optional third parameter to your ofstream creation it will erase the file contents when it open the file. Now just write the old information to this ofstream.
    Code:
    ifstream ins("MyFileName); // Open a file for reading.
    // Check that it opened correctly and read the information you want to preserve into vectors.
    
    ins.close(); // Close the file.
    ofstream out(MyFileName); // Open a file for writing, erase current contents.
    // Check that it opened correctly and write the new information.
    Jim

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Did you read the page that was linked?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by jimblumberg View Post
    So you read the portion of the file that you want to preserve using an ifstream. Now close this ifstream, and using the same file name create an ofstream. If you don't provide the optional third parameter to your ofstream creation it will erase the file contents when it open the file. Now just write the old information to this ofstream.
    Code:
    ifstream ins("MyFileName); // Open a file for reading.
    // Check that it opened correctly and read the information you want to preserve into vectors.
    
    ins.close(); // Close the file.
    ofstream out(MyFileName); // Open a file for writing, erase current contents.
    // Check that it opened correctly and write the new information.
    Jim
    Cheers Jim. I'll give that a go now!

    Thanks also laserlight for the heads up

  9. #24
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Also, is it really necessary to open the ifstream file?

    Couldn't I just store the data in to memory?

  10. #25
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I thought the information was contained in a file.
    I have a csv file and I want to move to a specific line and then begin overwriting data from that line onwards.
    How do you intend to read the information from this file? An ifstream reads from an existing file.

    Jim

  11. #26
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    How would you plan to obtain and store the data from the existing file without opening it for reading?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  12. #27
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Points taken :-)

    Hey, thanks to everyone.

    Got it sorted. Works nicely. Ended up implementing as Jim suggested. Cheers Jim!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-07-2012, 02:50 AM
  2. Move pointer to next line in .txt file
    By motarded in forum C++ Programming
    Replies: 6
    Last Post: 03-01-2006, 10:18 AM
  3. Move the Caret to a line
    By TheDan in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2005, 12:59 PM
  4. How do you move up a line?
    By epb613 in forum C Programming
    Replies: 3
    Last Post: 05-31-2005, 01:44 PM
  5. How do I make my edit box move text to the next line?
    By ElWhapo in forum Windows Programming
    Replies: 2
    Last Post: 01-04-2005, 11:21 PM