Public Group

# Help with using ifstream.

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

## Recommended Posts

I am reading a file in the format of:
"i10 i20 i30 d40 d50 i60 i70 i80"

I need to seperate the chars 'i' and 'd' from the int values, so I can use the chars to tell me what should be done with the int values. I have been able to do this using the getline() function and iterating through testing every value. However, I was told there was a better way to do this using the format:

while(inf >> command >> value){
..
}

I haven't been able to get this to work. I can only get it to do the while loop when I remove the value portion( while(inf >> command) ).
Here is what I have so far:

#include <iostream> #include <fstream> using namespace std; int main() { char command; int value; ifstream inf; inf.open("File1.dat", ifstream::in); if(!inf.is_open()) { cout << "Failed to open file." << endl; } while(inf >> command >> value) { //Information in loop is for testing if loop is running /*if (command == 'i') cout << command; else { cout << 0; }*/ cout << "Loop running"; } inf.close(); return 0; }

Any help would be appreciated.

##### Share on other sites
Umm... Is your data supposed to be formatted like that? At first your letters ('i' and 'd') are at the beginning of the numbers, but then 60 has an 'i' at the start and the end, and 70 has the 'i' at its end, and 80 has no 'i' or 'd'. I'm assuming it's just a small typo, but I want to make sure before I try anything, 'cause if it's not a typo that changes things.

##### Share on other sites
Sorry, that was a typo. Fixing it now.

##### Share on other sites
Hmmm... this works for me:

 #include <iostream> #include <sstream> int main() { std::stringstream ss; ss << "i10 i20 i30 d40 d50 i60 i70 i80"; char command; int val; while (ss >> command >> val) { std::cout << command << ": " << val << std::endl; } }

What does File1.dat look like?

##### Share on other sites
 char[] param = {' '}; char[] trimparam = {'i', 'd'}; string[] lines = System.IO.File.ReadAllLines(filePath); foreach(string line in lines) { string[] splits = line.Split(param): foreach(string finalstring in splits) { string result = finalstring.Trim(splitparam); } }

##### Share on other sites
hmm. cornstalks, your code works for me too. I looked in my data file and found that there is some text in the first line before it starts listing the 'i's, 'd's and ints. So I typed a bunch of random letters into your code like so: [color=#008800]"testing afdsfasdfgwagsd i10 i20 i30 d40 d50 i60 i70 i80" and it no longer works. I guess that is my problem. Thank you.

edit: just used the inf.ignore() function to ignore the first line of the data file and it all works fine now.

##### Share on other sites
Yes, additional text that breaks the formatting convention at the beginning of the file would cause the while loop to evaluate to false. Essentially, when you try to parse the int, the ifstream just sees text and no int, so it fails. Skipping the text is the right idea.

@TheTroll: That's great and all, but... the last time I checked ifstream was C++, not C#. That, and I think "splitparam" should be "trimparam", the program parses strings and not ints, and the command is not parsed.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5
khawk
11

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633677
• Total Posts
3013281
×