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

    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
    But I want to be able to read each box (four walls) on the same line

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

    Below is my current code

    int i,j,readAmount;
    	string inputLine;
    	ifstream worldFile;"map.2d",ios::in );
    	readAmount = atoi(inputLine.c_str());
    			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
    The edge of the known universe
    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, 03:14 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10: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

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