Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Nextgenneo

fstream help in Console C++

This topic is 5760 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

  
#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?

Share this post


Link to post
Share on other sites
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);
}
//...

Share this post


Link to post
Share on other sites
Thanks a lot guys! MAD NICE. Both ways are good and i realize that you did my hwk for me, thanks a lot! But at least i learnt how to do it, not jsut blatant copying, (sort of)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!