I'm writing a program which needs to read a text file and check first line of the text file for a number between 0 to 10. I have come up with a couple of solutions but there is still a problem:
How I read the file:
const string FileName= argv[1];
ifstream fin(argv[1]);
if(!fin.good()){
cout<<"File does not exist ->> No File for reading";
exit(1);
}
getline(fin,tmp);
if(fin.eof()){
cout<<"file is empty"<<endl;
}
stringstream ss(tmp);
first I used atoi:
const int filenum = atoi(tmp.c_str());
if(filenum<1 || filenum>10){
cout<<"number of files is incorrect"<<endl;
//exit(1);
}
If the first line is a character, change it to zero But I want to call an exception and terminate the program.
Then I used isdigit
but my entry is a string and it does not work with string.
Finally I used each character in string but still does not work.
stringstream ss(tmp);
int i;
ss>>i;
if(isdigit(tmp[0])||isdigit(tmp[1])||tmp.length()<3)
{}