Thread: Parsing Data in C++

  1. #1
    Registered User
    Join Date
    Jan 2002

    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

    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++
    }//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.

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

    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    class vertices
    	class vertex
    		string name;
    		int val;
    		friend istream& operator >>(istream& is, vertex& ve)
    			return is >> >> ve.val;
    		friend ostream& operator <<(ostream& os, const vertex& ve)
    			return os << << ' ' << ve.val;
    	vertex source;
    	vertex adj[2];
    	friend istream& operator >>(istream& is,vertices& ve)
    		string 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, 06:43 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10: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