Okay, I'm thoroughly confused. My professor assigned a homework assignment which requires me to read an input file, assign text in the input file to variables, then write them to an output file. I'm currently working on opening the input file and assigning text to the variables, but I don't know how to properly use getline() while still ignoring whitespace. Could someone please help me?
So, if this is my input file (ifile.txt):
1 2 3 4 5 6 7
|
2.5 2.0 2.0
2.0 3.0 4.0
5.0 5.0 5.0
1 2 3
|
How can I read each line and assign each number to a variable while ignoring the whitespace?
If line represents the line being read and ifile is the input file name:
getline(ifile, line);
would read the entire line from my understanding.
I've read using "<<" or ">>" ignores whitespace so that'd be the way to do it but I'm not sure how to implement that for getline..
I've also read (from this stackoverflow post:
https://stackoverflow.com/questions/20045726/skip-whitespaces-with-getline)
that this would help me:
http://www.cplusplus.com/reference/istream/ws/
But I don't understand how I'm supposed to use that in my own code..
Full Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include <string>
#include <fstream>
#include <sstream> // Not sure how to use this yet, if I even need it..
using namespace std;
int main() {
ifstream fin;
ofstream fout;
string ifile, ofile, line;
// Asking File Names:
cout << "Enter the input file name: ";
cin >> ifile;
cout << "Enter the output file name: ";
cin >> ofile;
// Opening the Files:
fin.open(ifile);
fout.open(ofile);
// Work In Progress..
getline(ifile, line); // makes a variable for the entire read line
//cin line >> n1 >> n2 >> n3; // Just a guess. Idk..
cout << "Debug: line is " << line << endl;
// Closing Files:
ifile.close();
ofile.close();
// General Stuff:
cin.ignore(1000, '\n');
cin.get();
return 0;
}
|
Help would be greatly appreciated! I'd ask my professor but every time people ask my professor questions his answer is "look it up"..
Thanks!