Editing files using file stream

I have an assignment where I need to open a text file. The content in the file appears as follows:

XXXXXXXX Stuff I Want To Keep X XX
XXXXXXXX Stuff I Want To Keep X XX
XXXXXXXX Stuff I Want To Keep X XX
(Repeats a lot)

The Xs are portions I want to get rid of, and the "stuff I want to keep" is what I want to write into a new file.

Here is my code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

int main() {
	
	ifstream original; // Creating a variable to hold the file to be opened
	string garbage; // Creating a string to hold stuff I do not want
	string goodStuff; // Stuff I do want
	
	original.open("someFile.txt"); // Open someFile.txt and associate with original

	if (!original) {
		cout << "The file could not be opened correctly...  This program will now end." << endl << endl;
		exit(1100);
	}

	original >> garbage; // Store the first portion of the file into a throwaway variable - works because extraction operator ignores leading whitespace, and proceeds until it hits whitespace

	return 0;
}


I'm not sure how to store the stuff I want next. I have put what I don't want into a string, but how do I move forward? Any tips?
Last edited on
Hello User55009,

My first question is can you alter the input file or are you stuck with what you have?

Line 21 is a good start. After that I would read the rest of the line using "std::getline(). And if the format is the same for every line you could use "str.pop_back()" to remove the last characters and spaces you do not need or "str.resize(x)" where "x" is the new length of the string which will drop what you do not need on the end.

For some help:
http://www.cplusplus.com/reference/string/string/

Hope that helps,

Andy
Perhaps the first step is to go through every line and try extract what you need.
1
2
3
4
5
std::string line;
while (! original.eof()) {
	std::getline(original, line);
	/* somehow extract garbage and goodStuff from line */
}
I cannot alter the input file itself directly, because the program I write is meant to open it, and change the format of each of those lines by eliminating the sections I described as not being desired. The format is the same for each line, and I just read up on the pop_back() you mentioned. It seems it only deletes one character from the end of the string. Would I loop it to get it to delete more? I can try the resize as well. I'm sure there are many ways of doing it, so the more I can figure out, the better off I will be. This file stream stuff is just generally confusing for me.
Ihatov, are you saying that another option is to extract up to the last section that I do not need, and then go from there on getting rid of the first portion that I do not need?
Hello User55009,

Yes "pop_back" only removes the last character. if you need more you could use a loop.

After thinking about it using "resize" would be quicker. like goodStuff.resize(goodStuff.length() - 5) should do the trick.

Try them both and see what you think.

Hope that helps,

Andy
Okay, thanks for the help. I'll mark this as solved, but if I have issues implementing your suggestions, I'll probably post back in here.
Hello User55009,

I will be watching.

Andy
Topic archived. No new replies allowed.