How to read characters and integers from a file?

So, I have a data file that looks something like this:

x + y + z

30 45 50

10 20 30

The only characters I needed were operators, so "+" + "I was able to use file.get() to successfully get these characters and put them in an array. The problem is that I need to get the next line of numbers and assign their values โ€‹โ€‹are x, y z. I know that I cannot use .get (), I would have to use getline . Should I eliminate file.get() and use getline instead for the first part as well?

I looked at some of the questions posted here, but none of them looked like mines. Note. I really use these values โ€‹โ€‹for another part of my program, I just used cout to check if my values โ€‹โ€‹read correctly. Here is my previous code:

 main(int argc, char *argv[]) { int a=0; int n; fstream datafile; char ch; pid_t pid; int a, b, c, result; string line; datafile.open("data1.txt"); if(datafile) { for(int i=0; i <9; i++) { datafile.get(ch); if (ch == '*'||ch == '/'||ch == '+'||ch == '-') { operations[a] = ch; cout<<operations[a]; a++; } } } else cout<<"Error reading file"; } 

So this is how I started getting the first line of the file at the beginning. It worked the way I wanted it, it might not be the most beautiful coding, but it worked. However, I tried to get the rest of the file, this time using getline , but instead of getting numbers, I was getting a bunch of random nonsense / numbers. I know, if I use getline , the first line cannot be in my loop. I know that I get the numbers.

  while(getline(datafile, line)) { istringstream ss(line); ss >> x >> y >> z; cout<<x<<""<<y<<""<<z; } 
+4
source share
2 answers

Will there be the following value for the first line, or am I missing something:

 string input; std::getline(datafile, input) for (int i = 0; i < input.size(); i++) if (input[i] == '+' || ...) { operations[a] = input[i]; a++; } 

If you don't want to use getline, you can just read the entire file stream (note that bool is a naive way to handle this problem, I would recommend something more elegant in your actual code):

 bool first = true; string nums; int lines = 0; vector<vector<int>> numlines; vector<int> topush; while (!datafile.eof()) { char ch = datafile.get() if (ch == 12 && first) //I don't know if '\n' is valid, I'd assume it is but here the sure bet first = false; else if (first && (ch == '+' || ...)) { operator[a] = ch; a++; } else if (!first && (ch >= '0' && ch <= '9')) { if (!(datafile.peek() >= '0' && datafile.peek() <= '0')) { numlines[lines].push_back(atoi(nums.c_str()); nums.clear(); if (datafile.peek() == 12) { numlines.push_back(topush); lines++; } } else nums = nums + ch; } 

Honestly, I canโ€™t be sure that the above will work exactly, I would recommend that you just change your code to use getline exclusively. You need to add #include to get atoi.

+2
source

Add this to your code:

 while(!datafile.eof()){ string s; getline(datafile, s); istringstream in(s); string tmp; while(in >> tmp){ int i = stoi(tmp) //Do something with i.... } } 
0
source

Source: https://habr.com/ru/post/1496935/


All Articles