infile String

This is a discussion on infile String within the C++ Programming forums, part of the General Programming Boards category; hey guys, I have a question to ask I am going to take in a file with a list of ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    9

    infile String

    hey guys,
    I have a question to ask

    I am going to take in a file with a list of ip addresses


    ex 130.5.3.6
    128.10.5.0

    i need a way to take in the entire line then break up the string into the 4 strings for each octet.

    That would mea the string of "130.5.3.6"

    will be broken up into
    string1 = "130"
    string2 = "5"
    string 3 = "3"
    string 4 "6"

    can you guys figure out a way to do that

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    My preferred method of splitting string is the Boost.StringTokenizer library.

    http://www.boost.org/
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Boost is cool. If you feel that it is overkill, you could use a stringstream to parse the string, and/or use getline with '.' as the delimiter.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    Im using a getline with a '.' as the delimiter but I don't know how to get the rest of the ip address.

    I can get the first octant but not the other 3

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Use ignore().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    Code:
    while(! in.eof() ) 
    	{
    		string line = "";
    		getline(in,line);
    
    		int i = 0;
    
    		while(line[i] != '.')
    		{	
    			oct1 = oct1 + line[i];
    			i++;
    		}
    		cout<<oct1<<"   ";
    		i++;
    	              
                                    while(line[i] != '.')
    		{
    			oct2 = oct2 + line[i];
    	                                i++;
    		}
    		cout<<oct2<<"   ";
    		i++;
    
    		while(line[i] != '.')
    		{
    			oct3 = oct3 + line[i];
    			i++;
    		}
    		cout<<oct3<<"   ";
    		i++;
    
    		while(i != line.length())
    		{
    			oct4 = oct4 + line[i];
    			i++;
    		}
    		cout<<oct4<<endl;
    
    	}
    This is the code I have so far. It takes in each octed but it only does it for one line. Why is that? shouldn't it be doing it for every line(every IP Address)?
    Last edited by carisma36; 12-07-2004 at 06:31 PM.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    I formatted the code better, Sorry.

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I don't see why it would stop at one line, it might depend on the input file you are using.

    I did notice that you are using eof() to control your outer while loop, which won't work in this case because eof() will return true only after your attempt to read using getline fails and after your code attempts to use the data that wasn't read in. Change your while to:
    Code:
    string line;
    while(getline(in,line))
    ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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