Thread: Using ifstream problems...[URGENT]

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    Exclamation Using ifstream problems...[URGENT]

    hi ppl..assignment due in 2 days...this is the file that i need to read...
    Code:
    *CLASS* YES NO
    alt yes no
    bar yes no
    fri yes no
    *DATA*
    YES,yes,no,no,yes,some,high,no,yes,french,0-10
    NO,yes,no,no,yes,full,low,no,no,thai,30-60
    YES,no,yes,no,no,some,low,no,no,burger,0-10
    so basically store the classes in the first line then in the next lines before *DATA* read in each item separately and then after *DATA* read in everything separately...

    i have the code working ok...only problem is right now in my code i do this:
    Code:
    while (in.peek() != '\n')  //where 'in' is the ifstream operator
    {
       //read each word from the line by using in >> smth;
    }
    this is not very effective because let's say in the file if before a newline character at the end of a line i have a space and then a newline, then my code runs into problems...

    is there a way to say that do this until the end of the line...like eof is there something like eol (end of line) so i can use while !eol???

    thanks a lot...

    Farooq

    P.S.
    another question is regarding repositioning of the pointer...i want to store the pointer position at the line startting alt and then go until the line starting fri and count the number of lines (i.e. no. of lines between the first line and the line starting *DATA*)

    after counting i want to start at alt again so i can start reading in the data...how to do this?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One idea is to use getline() to read each line into a string.
    Code:
       string line;
       while (getline(in,line))
       {
          //Do stuff
       }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    ok so i guess i could do this:
    Code:
    string word, line;
    in >> word;
    while (strcmp(word, "*DATA*") != 0)  //till you don't get *DATA*
    {
       getline (in, line);
       //process the line
       in >> word;
    }
    i have two questions here:
    1) how would i process the line so that i get the different words from it...e.g if u take the line:
    Code:
    alt yes no
    then word would have alt in it...and line would have: "yes no"
    or would line have: " yes no"

    after this how would i extract yes and no from line?

    2) i still need to count the number of lines between the *CLASS* line and the *DATA* line BEFORE I do the step in (1)...how can i do that?

    Regards,

    Farooq

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >while (strcmp(word, "*DATA*") != 0) //till you don't get *DATA*
    If you are using the string class, you don't need strcmp():
    Code:
    if (word == "*DATA*")
    1) You could use find() to look for a space, then use substr() to extract the word. substr() gets part of a string:
    string item = "red car";
    substr(item,0,3)
    would return "red".

    2) Look for *CLASS*, when you find it, start reading lines until you see *DATA*. Each time you read a line, increment your line count.
    Last edited by swoopy; 11-04-2004 at 02:26 PM.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    for the second part of your post...after i have done what you say...how do i go back to the original point...not start of file but after that line...
    i know i can use a function to store the pointer location...but how do i set the pointer location to the earlier point later on...?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i know i can use a function to store the pointer location...but how do i set the pointer location to the earlier point later on.

    seekg() should do the trick. Use tellg() to save the file position for later.
    Code:
    pos_type position = in.tellg();
    .
    .
    .
    //Now go back to saved position in file.
    in.seekg(position);
    Or you could always just read the file twice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  2. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  3. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  4. Read problems
    By xddxogm3 in forum C++ Programming
    Replies: 10
    Last Post: 10-05-2003, 12:17 AM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM