Thread: Reading From Text File

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question Reading From Text File

    Hi,

    I have data in a text file that is set out like this:
    LM Account
    22.356 Files
    Time Thanks
    22.0000 0
    22.1000 15
    22.2000 11

    etc..

    What I want to do is read in line by line. I want to ignore the white space at the beginning of the line (there is some varying amount of white space at the beginning of each line), recognise the white space in between entries, and replace it with a comma and then write that line to a csv file. And then continue line by line until I reach the end of the file, essentially writing the text file to CSV.

    e.g.) Output for first 2 lines would be:
    LM,Account
    22.356,Files


    But I must do it line by line. The reason being, I will be taking data from other text files which are arranged in the same column like format but putting all of the data side by side in one csv file.

    Any tips and suggestions on how I might do this???

    Cheers

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    C++ getline() stream functions by default read upto a newline, then discard it from the stream, placing the data into a buffer. Ie, they are for doing exactly this: reading text line by line.

    If you use the fstream getline() method, you have to use a c-string for a buffer, but if you use the global getline() from <string>, you can use a C++ string object, which is probably what you want because you can then use the various string and stringstream methods to do your parsing.

    IMO, c-strings and cstdio functions might be a little tidier in this case, but if you are not already familiar with them you should just learn the C++ methods. They are very different syntactically.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Thanks for your reply.

    So if I use the C++ methods. How would you go about coding this?

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Because If I use a C++ string variable to store a line of data. How can I search through this line to extract my data in a delimited form.

    How would I be able to code for ignoring any initial whitespace? and code for recognising the whitespace between the words?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    To get past the whitespace at the beginning, find_first_not_of:

    Code:
    int start_pos = str.find_first_not_of(" \t");
    You can then use find_first_of(" ", start_pos) to get where the first comma should go and keep going from there, maybe:

    Code:
    int p = str.find_first_of(" ", start_pos);
    while (p != string::npos) {
          str[p] = ',';
          p = str.find_first_of(" ", p);
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Wouldn't this just fill the white space between 2 pieces of data with commas.

    e.g. str could contain

    wwwwwLM,,,,,,,Account

    Where, w = whitespace.


    Or am I missing something?

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    What would you think of:
    int start_pos = linestr.find_first_not_of(" \t");
    int com = linestr.find_first_of(" ", start_pos);
    int last_data = linestr.find_first_not_of(" \t", com);
    Having this information: where the first piece of data starts/ends and where the second piece of data starts.

    Then I will be able to use this to write the first piece of data "," and the last piece of data to the csv file.

    What do you think?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by strokebow View Post
    What do you think?
    Since you seem to be doing this in the context of a larger program, what I'd recommend is that you write a smaller separate program that just opens a file, parses it, and outputs to the console. You've got some basic ideas, if you fool around with them eventually you will get what you want, then you can just copy paste into your larger project. This makes life a lot easier; it's actually exactly what I'm doing right now (figuring out the best way to parse a data file using a test program), esp. if you need to examine what's really going on (as opposed to what you thought would happen) at various points.

    If a variable amount of whitespace between fields is an issue, put that code into a function:

    Code:
    int skipWS (const string &str, int pos) {
        while (str[pos] == ' ' || str[pos] == '\t') pos++;
        return pos;
    }
    You can use that in your (other) loop after you find_first_of(" \t"), eg:

    Code:
    pos = skipWS(str, str.find_first_of(" \t", pos));
    Last edited by MK27; 06-29-2011 at 10:05 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also don't forget that there is the white space extraction operator (ws) available with C++ streams.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  2. Reading text file.
    By pe4enka in forum C Programming
    Replies: 4
    Last Post: 04-30-2010, 01:29 PM
  3. Reading from a text file
    By chevyman2002 in forum C Programming
    Replies: 10
    Last Post: 03-09-2009, 04:40 PM
  4. Reading a text file
    By AmazingRando in forum C Programming
    Replies: 2
    Last Post: 09-08-2003, 10:38 AM
  5. Reading in a text file
    By speve in forum C Programming
    Replies: 1
    Last Post: 01-01-2002, 08:00 AM