I want to load integers from a text file into a class or structure. It should jsut read all lines and load them into it. What is aa easy code for this? I want to keep it simple and easy. Also it should be easy to modify, like modify it according to the exact format of the input of integers.. 1,2,3,4,5 1 2 3 4 5 etc. I suppose the exact format makes a difference and it may need to be modified depending on what format the input will be. I have looked at some things, but I don't understand it yet.

5 Years
Discussion Span
Last Post by Torf
  1. Read line into buffer.
  2. Use strtol() to read string into number (integer) variable.


char buffer[1024];
char* p = 0;
int value = 0;
istrm.getline(buffer, 1024);
value = (int) strtol(buffer, &p, 10);

istrm is an input stream that you can read a line from.
The strtol() function will set the pointer 'p' to point to where the
conversion stopped due to an invalid numerical character. You can start
scanning from there on the next call to strtol().

Edited by Dani: Formatting fixed


Thank you. It's not clear to me where the input is saved, though. Value?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.