I have : string line; ifstream myfile (\"file1.txt\"); ofstream myfile1 (\"file2
ID: 3681609 • Letter: I
Question
I have :
string line;
ifstream myfile ("file1.txt");
ofstream myfile1 ("file2.txt");
if (myfile.is_open()) {
while ( getline (myfile,line) )
So as to read the first text file line by line, and I have :
int posP = s.find('.,!?');
while (posP < s.length() && s.size() > 40) {
s.insert(posP, 1, ' ');
posP = s.find('.,!?', posP + 1);
}
int pos = s.find(' ');
while (pos < s.length() && s.size() < capacity && s.size() > 40) {
s.insert(pos, 1, ' ');
pos = s.find(' ', pos + 2);
}
Which correctly justifies every line when done line by line, but how do I (in c++) have the string affecting functions work on the text in file1 and then output the result to text file2
Explanation / Answer
for line oriented input, you should read using getline. After that, however, the line has been extracted from the input; to parse it further, you need to initialize an std::istringstream with it, and read from that. Except that your input format seems to be based on columns, so you'd probably use the substr function of std::string to get the individual fields. Once you've got them, you'll want to strip leading and trailing white space, and maybe convert to numeric types. But all of this would logically occur in a user defined operator>> to a Data class. (Similarly, you'd provide a user defined operator<< to write the sorted data out.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.