ifstream

Hello,

Consider the program successfully opened a txt file. I need to get the integers in the file and make up matrices with them but I need to verify that there is no alphabetic characters in the file.

My question is how can I understand if there are alphabetic characters in the file?

Should I scan the whole file for alphabetic characters, before getting the integers or should I verify it while I get the integers? For example with an if-else statement.

Thanks!
Last edited on
This will depend on the format of your file, but you can try reading the file line by line (putting the data into std::stringstream). Then extract the data from stringstream into int (or whatever integral type you need), and immediately after extracting, check if the stream failed.

e.g.
1
2
3
4
5
6
7
8
9
10
11
std::string hold("");
std::getline(file, hold)

std::stringstream ss;
ss.str(hold);
while(ss.good()){
    int data_point(0);
    ss >> data_point;
   if(!ss.good())
//Do something
}
Topic archived. No new replies allowed.