Thread: input from files

  1. #1
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23

    input from files

    Hi guys,

    Is it possible to jump from one line to another in a file when using the getline() command to read data in without having to pad out the lines so that they are all the same length and then jumping a set number of bytes?

    Also is there a way to read in parts of a line in a file up to a certain delimiter (eg. a comma, fullstop, backslash) rather than simply a whitespace character as when using the >> operators.

    Cheers

    Dunners
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Dunners
    Hi guys,
    Is it possible to jump from one line to another in a file when using the getline() command to read data in without having to pad out the lines so that they are all the same length and then jumping a set number of bytes?
    I'm not quite sure what you are getting at, but if you do something like this:
    Code:
    while(!inFile.eof())
    {
        string str;
        getline(inFile, str);
        
        //do something
    }
    You will read in one line at a time, regardless of each line's length.

    Also is there a way to read in parts of a line in a file up to a certain delimiter (eg. a comma, fullstop, backslash) rather than simply a whitespace character as when using the >> operators.
    There is a third parameter for getline(), which is the delimiter, and it's default value is '\n'. You can change the delimiter to anything you want:

    string str;
    getline(inFile, str, '#');

    The delimiter will be discarded, and it will not be part of the input.

    Getting back to your first question, if you want to read in part of a line, and then skip to the next line if, say, there is a certain word in that text, you can just use getline() with a '\n' as the delimiter to finish off reading that line:

    string discard;
    getline(inFile, discard, '\n');
    Last edited by 7stud; 02-19-2005 at 10:02 AM.

  3. #3
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23
    Hi,

    Basically what I'm doing is outputting several variables to a file and then read them back into those variables at a later stage. This can be done several times with each set of variables being held on a different line. One of the variables is a string that can be of differing length depending on what the user specifies.

    This causes problems when attempting to read a specific line (say I want the variables stored in line 10 of the file), because the lines can be of different lengths I can't figure out how to move to line ten - it can't be done by specifying a different number of bytes to skip as each line isn't the same number of bytes. Is it at all possible to skip to a specific line in the file and then read in the variables stored in it using the in >> <variable name> command?

    Any help with this would be appreciated.

    Cheers
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Is it at all possible to skip to a specific line in the file and then read in the variables stored in it using the in >> <variable name> command?
    The >> operator is defined to stop reading data at the first trailing whitespace it encounters, so any space between words, as well as the end of the line, causes it to stop reading input.

    If you want to go to the 10th line, you can always use a for-loop and getline() to read in 9 lines, discard them, and then read the 10th line into your variable.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Use a loop to read in the first 9 lines and either ignore them or store them in a container. Then read in line 10 and do what you want. If you want to put the changes to line 10 back into the file, and the lines are of different lengths, then you need to read in the whole file to the program, modify the desired data, then rewrite the entire file back to file. If you don't want to keep everything in RAM while you work on the desired line, write anything before the desired line to a second file, alter the desired line and write it to the second file, then write everything else to second file.

    If each "line" or "record" or "field" in the file is of fixed length, and the data to change doesn't alter the fixed length format of the file fields, then you can use bits/bytes to just change the desired data in file without having to rewrite the whole file. But if the file format is random size fields, then you have to use the file rewrite style.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. How to get input from multiple files?
    By jumpyg in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 09:20 PM
  4. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  5. string input from files
    By jriano in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2003, 01:08 PM