Thread: String File Input

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    20

    String File Input

    I am trying to read a file to create the structures for a small 2d game.
    I am reading in integer numbers to construct the walls.

    At the moment I am using getline and have each number on a seperate line
    Code:
    100
    100
    200
    200
    But I want to be able to read each box (four walls) on the same line

    Code:
    100 100 200 200
    What syntax can I do this with?

    Below is my current code

    Code:
    int i,j,readAmount;
    	string inputLine;
    	
    	ifstream worldFile;
    	worldFile.open("map.2d",ios::in );
    
    	getline(worldFile,inputLine);
    	readAmount = atoi(inputLine.c_str());
    	for(j=0;j<readAmount;j++)
    	{
    		for(i=0;i<8;i++)
    		{
    			getline(worldFile,inputLine);
    			rectWalls[i] = atoi(inputLine.c_str()); //converts inputLine string Var to integer, via c to c++ conversion
    		}
    		polygon(mapBuffer,4, rectWalls, makecol(255, 0, 0));
    	}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use a string stream.

    Read a whole line.
    Initialise an istringstream foo with your input
    Then do
    while ( foo >> myint )

    to extract each int in turn.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Replies: 14
    Last Post: 01-18-2008, 04:14 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM