So I am starting to create this program to read a text file and copy the text to another file until it reaches the line that is </settings>. I thought I was doing well but everything copies. I am not sure what I am missing and was hoping someone could help. I also tried messing with my if statement but when I changed the != to == only the stuff after </settings> copied. I know the program is not complete as I plan to do this a couple times then save the new file so it's not finished yet.
My question would be in line 49 what does "rdbuf" contain?
Next question is what is the exact input file that you are using. Without seeing what you have to work with it makes it hard to understand your code and to test the program.
Renaming a ".xml" file to a ".txt" file is unnecessary and a ".xml" file is a text tile. What would make more sense would be to choose a different output file name and then, maybe, at the end of the program rename the file.
Good job on defining the file stream variables and opening the files then checking if they opened.
After that I am thinking that this might work as a start:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
std::string line{ "</settings>" };
std::string strInput;
while (std::getline(inf, strInput))
{
if (strInput == line)
{
;// <--- Do Something
}
else
{
outf << strInput << '\n';
}
}
There is no need for line.assign("</settings>"); as you can do this when you define the variable. Unless you want the practice. Also line = "</settings>"; would also work.
Thank you Handy Andy!!! I am back on track. I like your suggestion on the renaming at the end or even creating a new one and not having so change the xml to a txt so will most likely incorporate it with the final version. Here is what I have and it is all working now so I can continue:
Right now the program is working great with the code below. However, I am looking for a way to possibly compare the lines in the newlines.txt and make sure they aren't duplicated in what was already printed to the output file just so there isn't duplicated settings. Any suggestions?
This does work in preventing duplicated lines however, it is totally messing up the orders. It is putting everything in order alphabetically instead of keeping the format which is required.
Expanding on seeplus's code.
Note that you don't need to use double backslashes to separate files.
A forward slash works just fine, even for Windows (I believe).
By golly I think we have something here! Now to read up and understand why it is working so well. It's not enough for me that it works but I need to understand it LOL :)
This will actually be added to a larger project that I will be developing which will include a user interface to allow for options and changes so more to come.
The basis behind this is that a set can't contain duplicates. If you try to insert the same item to a set it will be rejected. The return value indicates whether the insertion was successful (not duplicate) or failed (duplicate). There is also multiset which can contain duplicate values. set maintains its data as a sorted tree for quick insert/find. That's why using my original code you got the new file in sorted order. As that wasn't acceptable, dutch's solution uses a vector to store the items in order if a set insertion succeeded (ie not duplicate).
In any scenario in which non-duplicates are required, think set (or map if an associated value is also needed).
One more question, I want to include 2 not equal </settings>. One being "</settings>" and the other being " </settings>" (with a TAB space in front of it). I was thinking:
1 2 3 4 5 6
for (string line; getline(in_config, line) && line != "</settings>" , " </settings>"; ) {
constauto& result = slines.insert(line);
if (result.second)
vlines.push_back(line);
but it doesn't seem to work. The reason is there might be a TAB space in front and want to account for it.
for (string line; getline(in_config, line) && line != "</settings>" && line != "'\t'</settings>"; ) {
constauto& result = slines.insert(line);
if (result.second)
vlines.push_back(line);
and:
1 2 3 4
for (string line; getline(in_config, line) && line != "</settings>" || "'\t</settings>"; ) {
constauto& result = slines.insert(line);
if (result.second)
vlines.push_back(line);
Oh I know you said it wasn't tested. I have no issues testing things as they are suggested. The best part about this project is I have no deadline as it is helping me learn.
Now I understand what you are saying as it wasn't inserted into the vector, but now I am being told line is undefined and VS is expecting a ; in there but when I get the code to check out it still drops the stuff from clientconfig and only does the newlines. I am trying to see if there is something I am not seeing in the line that needs to be formatted differently.