Thread: Input stream format

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    Input stream format

    I need to write some code that will read in some numbers in various formats. The problem is that there are a few different ways the numbers can be arranged.

    n1 n2 n3

    or

    n1/n2 n3/n4 n5/n6

    or

    n1/n2/n3 n4/n5/n6 n7/n8/n9

    This is relatively easy to solve with scanf but as my project is in C++ and I'm using streams everywhere else, I would like to solve this using streams as well.

    I would appreciate some help on how to solve this problem. Thanks.

    P.S. In case anybody is curious the above is a graphics format. We can either have 3 vertices alone, 3 vertices along with their corresponding texture coordinates, or 3 vertices along with their texture coordinates and normals.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    >>n1/n2 n3/n4 n5/n6
    Code:
    	ifstream file("test.txt");
    	vector<pair<double,double> > values;
    	typedef vector<pair<double,double> >::const_iterator citer;
    	double val1 = 0,val2 = 0;
    	char c = 0;
    
    	while(!file.fail())
    	{
    		file >> val1 >> c;
    		if(file.fail() || c != '/')
    			break;
    		file >> val2;
    		values.push_back(make_pair(val1,val2));
    	}
    
    	for(citer i = values.begin();i != values.end();++i)
    		cout << i->first << '/' << i->second << '\n';

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  2. reterning a reference to the input stream
    By indigo0086 in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2006, 10:29 AM
  3. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  4. reading from input stream
    By Micko in forum C++ Programming
    Replies: 8
    Last Post: 05-01-2005, 05:50 PM
  5. Getting an input stream to stop creating a file
    By Stevek in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2003, 04:45 PM