reading formatted data files

This is a discussion on reading formatted data files within the C++ Programming forums, part of the General Programming Boards category; is it possible to use the getline() function to skip a word and get the data after the word? I ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    76

    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...

    Code:
    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
    Location
    Canada
    Posts
    8,045
    Use a stringstream or some other tokenizer.
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    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
    Posts
    76
    i havnt really looked up on how stringstreams work but with the getline() method...so 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
    Posts
    725
    Read each line into a string, then use std::string::substr(). Google "c++ string substr" for more info.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /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
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    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, 01:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21