Thread: question about ignore()????

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    15

    question about ignore()????

    Hi ,
    I have a question about reading in a file. I have a file that I am reading in that has the format:

    setenv HOST1 somehostname1
    setenv PORT1 someportname1
    setenv HOST2 somehostname2
    setenv PORT2 someportname2


    essentially, I need to be able to store these HOSTS names and PORT numbers, I have used a struct to do this with no problem.

    However, I can't figure out how to be able to ignore the "setenv", I tried ignore(), but it did not work.

    I am not sure of how to do this??Do I use getline, or get and then ignore??

    I want to be able to ignore the "setenv", but then be able to read in the "HOST1 somehostname1"...etc

    Thank you for any help anyone can offer. It is appreciated!

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    char input_str[256]; // for example
    char garbage[50];
    char var1[50];
    char var2[50];

    // Normal file reading into "input_str" array, and then:
    sscanf (input_str, "%s %s %s", garbage, var1, var2);
    // now do something with var1 & var2.

    You may also want to use fscanf

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You can either read an entire line with getline and parse it in your progra, or use the >> operator of the file stream to read everything up to whitespace. Ignoring setenv is only a matter of reading and discarding it, then you can move on to the important things.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    15
    Thanks for your help, I'll try it out. Thanks again!

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string buffer;
        ifstream file("c:/temp/file.txt");
        while( getline(file, buffer) )
        {
            cout << buffer << endl;        
        }
          
        system("PAUSE");
    }
    Now it's only a matter of extracting the three words from the buffer variable. Take a look at the string methods. Also you migh want to take a look at atoi.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. question about reading files in
    By smd in forum C++ Programming
    Replies: 11
    Last Post: 08-25-2003, 07:40 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM