fstream help in Console C++
I am reading from a text file which containts integers AND strings which are all mixed up. I need to sort all the stuff into an array of ints and an array of string. I am using fstream.h. So, i have a loop like:
while (!file.eof())
{
// Here is where i want to read stuff in
}
But i need a way of distinguishing whether what i am about to read in is of type int or string. Can ne1 help? All i can think of is reading it as a string then testing the ascii value of the first letter of that string to c whether its a number and using the atoi function to convert it to an int, and dump it in the array of ints or if it is not a number dump it in an array of strings. The obvious draw back to this method is what if one of my strings is like, "101Dalmations" and its just a stupid method. Again, ne ideas will be greatly aprreciated!
If its a file that is written by you, you should design your files and not make them messy.
If youre reading a file that can be anything, I dont know
If youre reading a file that can be anything, I dont know
The file is NOT written by me. Its an assignment in my AP Computer Science class and our teacher said that we cannot re write the file. If i could have i would have.
The file is NOT written by me. Its an assignment in my AP Computer Science class and our teacher said that we cannot re write the file. If i could have i would have.
uhm... There was a function IS_CHAR() or IS_INT() or something, I DONT KNOW FOR SURE! but I thought there was one
What you are talking about is a parser. May find some stuff about it on the web.
Off the top of my head something like this would do the trick:
I realise I just did ya homework but hey I''m feeling generous and not particularly pedagogical.
P.S. I think the way i think the way I converted from a string to an int was somewhat lame, anyone know a better way?
Off the top of my head something like this would do the trick:
#include <iostream>#include <sstream>#include <fstream>#include <vector>using namespace std;main(){ string ThisItem; vector<int> IntArray; vector<std::string> StringArray; ifstream File("test.txt"); if( File.fail() ) { cout << "Could not open file" << endl; return; } while( !File.eof() ) { File >> ThisItem; if( ThisItem.find_first_not_of("0123456789") != string::npos ) { // ThisItem contains non-numeric characters so treat as a string StringArray.push_back(ThisItem); } else { // all characters are numeric so treat as an int stringstream ss; int Temp; ss << ThisItem; ss >> Temp; IntArray.push_back(Temp); } } // verify cout << "Strings:" << endl; for( vector<string>::size_type i = 0; i < StringArray.size(); ++i) { cout << StringArray[i] << endl; } cout << "Ints:" << endl; for( vector<int>::size_type i = 0; i < IntArray.size(); ++i) { cout << IntArray[i] << endl; }}
I realise I just did ya homework but hey I''m feeling generous and not particularly pedagogical.
P.S. I think the way i think the way I converted from a string to an int was somewhat lame, anyone know a better way?
You could read it as a string first, and then try to convert it to an int.
//... std::string str; the_ifstream >> str; int number=convert_to_number(str); if (conversion_succeeded) { do_something(number); } else { do_something(str); }//...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement