Thread: text file queries

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    36

    text file queries

    I have a few question re manipulating text files using fstream.

    In my project I need to convert one file format to another (basically just adding a header to the new file). However part of the header requires me to specify the number of lines in the original file - something I can only know after I have looped through the entire file.

    Q1 - Is there a way that I can open a file and insert lines at a) a certain point in the file ie line 10, b) insert lines at the start of the file. I only found reference to being able to append ios::app

    Q2 - Is there a way of looping through the file to increment a counter. I tried

    Code:
    while (!finput.eof()){
    	numOfDataPoints++;
    }
    but this just went into an endless loop. I believe because I'm not actually reading anything in. Obviously doing this I would then close the file, and then reopen it for reading. Inefficient but I need the number of lines to be in the header. I could just read something in but again this takes time.

    Thanks for comments

    PS - Q3 - I do a read from my file ie finput >> x >> y >> z; Now my original file has an empty line at the end, and the x,y,z values therefore have the previous entry. Whats the best way of ensuring catering for this situation?
    Last edited by ozzy34; 09-30-2004 at 02:35 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by ozzy34
    Q1 - Is there a way that I can open a file and insert lines at a) a certain point in the file ie line 10, b) insert lines at the start of the file. I only found reference to being able to append ios::app
    Yes and no, if you open the file and go to the beginning and just start writing you will only end up overwriting what is already there and not simply inserting new text. One of the easiest things to do would be to open your original file for input and open a second file for output. Read through the input file once, to get the number of lines that you mention are required. After that loop you need to reset your file pointer to point back to the beginning of the input file. Do this using the seekg and clear member functions as such:
    Code:
    ifstream input("Whatever.txt");
    // Loop through file and count lines
    ...
    // Now reset file flags and file pointer, the order these are called is important
    input.clear();
    input.seekg(0,ios::beg);
    Now that you have your input file ready to be read a second time and the count of your lines, you can write your header to the output file. Then start your read/write loop... reading through the input file and writing to the output file.

    Quote Originally Posted by ozzy34
    Q2 - Is there a way of looping through the file to increment a counter. I tried

    Code:
    while (!finput.eof()){
    	numOfDataPoints++;
    }
    but this just went into an endless loop. I believe because I'm not actually reading anything in.
    Yes, its because you aren't doing anything to the stream that would cause the eof member function to ever return something different other than false.

    Quote Originally Posted by ozzy34
    PS - Q3 - I do a read from my file ie finput >> x >> y >> z; Now my original file has an empty line at the end, and the x,y,z values therefore have the previous entry. Whats the best way of ensuring catering for this situation?
    Depends on what you need to do with this last line of data. If you know it is always going to be a blank line and you simply need to write this blank line to the output file and not rewrite the duplicate x, y, z values a second time then simply don't output them.
    "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
    May 2004
    Posts
    36
    Ta for that

    Yes, its because you aren't doing anything to the stream that would cause the eof member function to ever return something different other than false.
    If I read the entire line in then things are real slow - especially as some of the files have >20 lines.

    Anyway I can just read in a part of the line ie the first float value?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. checking values in a text file
    By darfader in forum C Programming
    Replies: 2
    Last Post: 09-24-2003, 02:13 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM