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; }
source share