Parsing Data in C++

This is a discussion on Parsing Data in C++ within the C++ Programming forums, part of the General Programming Boards category; hola amigos. this is my goal, i'm reading a file into my program, each line resembles this (its for a ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    63

    Parsing Data in C++

    hola amigos. this is my goal, i'm reading a file into my program, each line resembles this (its for a graph project)

    SOUCE : VertexA 1 VertexB 13 VertexC 8

    where souce is the source vertex and the vertecies after the colon are adjacent vertecies. i am using getline to pull in each line of my input file. then i redirect each 'thing' into a string and the costs into an integer.. fine so long as i don't have more than one vertex after the source, how do i loop to run through til the end of the getline and then pull another line up after that?

    code reads as this so far

    Code:
    while( getline(in, oneLine) )
    {
    
    string source, colon, dest;
    int cost;
    
    in >> source;
    in >> colon;//colon fall through
    //HERE I Want to run my loop til oneLine is gone, but how??
    while(oneLine){//THIS CAUSES ERRORS in G++
    in>>dest;
    in>>cost;
    }//closes while one line
    
    }//closes getline while loop
    this is obviously only to parse the input file and nothign more.. so any/all advice is appreciated.
    SS3X

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You could do something like -

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    
    class vertices
    {
    private:
    	class vertex
    	{
    	private:
    		string name;
    		int val;
    		
    		friend istream& operator >>(istream& is, vertex& ve)
    		{
    			return is >> ve.name >> ve.val;
    		}
    
    		friend ostream& operator <<(ostream& os, const vertex& ve)
    		{
    			return os << ve.name << ' ' << ve.val;
    		}
    		
    
    	};
    
    	vertex source;
    	vertex adj[2];
    
    	friend istream& operator >>(istream& is,vertices& ve)
    	{
    		string line;
    		getline(is,line);
    		stringstream ss;
    		ss << line;
    		string discard;
    		ss >> discard >> discard >> ve.source >> ve.adj[0] >> ve.adj[1];
    		return is;
    	}
    
    	friend ostream& operator <<(ostream& os, const vertices& ve)
    	{
    		return os << ve.source << ' ' << ve.adj[0] << ' ' << ve.adj[1];
    	}
    
    };
    
    
    int main()
    {
    
    	vertices v;
    	cin >> v;
    	cout << v << '\n';	
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Some questions on sscanf and parsing data
    By green2black in forum C Programming
    Replies: 7
    Last Post: 12-02-2008, 08:25 PM
  3. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 07:43 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  5. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM

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