Thread: formatted file input

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    305

    Angry formatted file input

    ok, ive been working on this for about 3 hours. ive stared at the STL references for about an hour. i know the problem is that i need a good algorithm, so looking in the STL references wont help. anyways, heres teh problem.
    i have a file that is something like this:
    10,5,2,3
    now, i want the parts seperated by the commas in seperate variables(int). the limit is 4 variables(must be compatible with any number u to 4). i have written a function written that takes two std::string and a char then takes everything from the beginning to the comma and stores that in the first string. the other part is put into the second string. the comma is deleted.

    i think that my way of thinking isnt very good for these kinda problems, so any help, or a finger to the right direction will be appreciated.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    This compiles and works in g++.

    Code:
    #include <string>
    #include <vector>
    #include <sstream>
    
    int main() {
    	std::istringstream in("1,1,2,3,5,8,13,21,34"); // replace in with std::ifstream if neccesary
    	std::vector<int> intVec;
    
    	while (!in.eof()) {
    		int temp;
    		in >> temp;
    		intVec.push_back(temp);
    		char eatComma;
    		in >> eatComma;
    	}
    
    	std::cout << "index\tvalue" << std::endl;
    
    	for (size_t i = 0; i < intVec.size(); i++) {
    		std::cout << i << "\t" << intVec[i] << std::endl;
    	}
    
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM