String File Input

This is a discussion on String File Input within the C++ Programming forums, part of the General Programming Boards category; I am trying to read a file to create the structures for a small 2d game. I am reading in ...

  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
    32,832
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03: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, 10:54 AM

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