Hello,

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.

Recommended Answers

All 2 Replies

  1. Read line into buffer.
  2. Use strtol() to read string into number (integer) variable.

Example:

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().

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.