Thread: reading formatted data files

  1. #1
    Registered User
    Join Date
    Apr 2005

    reading formatted data files

    is it possible to use the getline() function to skip a word and get the data after the word? I am wanting to make a program that reads from a formatted .txt file and then when done with the file, append the new data to the same formatted .txt file.
    here is an example of the txt file...

    EntryNumber:          1
    EntryName:            JOE SOMEBODY
    EntryHPhone:          111-1111
    EntryCPhone:          222-2222
    EntryAdress:          1234 Somewhere St.
    As you can see, i want the data to the right of the colon of each line. How would I read the file to where I could skip the Entry" " part of the line, and then use getline() to read the data?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Use a stringstream or some other tokenizer.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C / C++
    Join Date
    Jan 2006
    The Netherlands
    first use getline() with a string, after that, you can use substr(), because every line is formatted.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    Registered User
    Join Date
    Apr 2005
    i havnt really looked up on how stringstreams work but with the getline() basically it reads the whole file... how would i use substr() to retreive the data that i want?

  5. #5
    Registered User
    Join Date
    Mar 2006
    Read each line into a string, then use std::string::substr(). Google "c++ string substr" for more info.
    #include <stdio.h>
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Join Date
    Jun 2002
    A tokenizer would be best for something like this; substr() works but you would have to assume that the 'actual' data starts at a specific location in the string, which may be the case, although a tokenizer is still the 'cleaner' (but not faster) way of doing it. Basically, when using substr(), you'd pass it the location of the start of the 'actual' data, and have it give you everything from that location to the end of the string.
    *** TITANIC has quit (Excess Flood)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Problem with reading files
    By glopv2 in forum C Programming
    Replies: 3
    Last Post: 07-31-2007, 11:05 PM
  3. Reading data from consecutively named files
    By a1pro in forum C Programming
    Replies: 10
    Last Post: 04-15-2005, 01:48 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Reading a line of data...
    By RedZippo in forum C++ Programming
    Replies: 10
    Last Post: 04-04-2004, 02:14 AM