Writing an application for a friend to split an .ini file into multiple smaller files.
Everything works except that some lines won't write to a certain filestream (but still write to another unneccesary filestream which is there just for debugging)
Does the filestream for some reason close automatically?
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
string s_load_line;
string s_botname;
string s_write_file_name;
unsignedshortint n_namelength;
fstream fs_writenew; // declaring this now to avoid an error
fstream fs_robo_ini("sample.ini", ios::in); // the big file i'm trying to split
fstream fs_robotemp("newmewfile.txt", ios::out); // a filestream that DOES work, even though i don't need it to
while (fs_robo_ini.good()) // this is true even when i get bad output
{
getline(fs_robo_ini, s_load_line); // get line from the big file
if (s_load_line.substr(0,1) == "[") // if it starts with a bracket, close the current file and start a new one
{
if (fs_writenew.is_open())
{
fs_writenew.close();
}
n_namelength = s_load_line.length();
s_write_file_name = "seen_" + s_load_line.substr(1,(n_namelength-2)) + ".ini"; // extract name in brackets
fstream fs_writenew(s_write_file_name.c_str(), ios::out); // the name of the file is _seen[NAME].ini
fs_writenew << s_load_line; // this works
}
else
{
fs_writenew << s_load_line; // but this doesn't. WHY???
fs_robotemp << s_load_line << '\n'; // but this DOES.. WHY?!?!
}
fs_writenew << '\n';
fs_robotemp << '\n';
}
fs_writenew.close();
fs_robo_ini.close();
fs_robotemp.close();
return 0;
}
sample file
[Artemis]
address=Artemis@FUR-6D9151B4.hr.hr.cox.net
action=quitting IRC with message "Quit: http://www.kiwiirc.com/ - A hand crafted IRC client"
ctime=1441255636
[Kurty]
address=~Kurty@FUR-3251C1B9.hsd1.ga.comcast.net
action=quitting IRC with message "Input/output error"
ctime=1476343809
[MajikBear]
address=~MajikBear@FUR-2CFCE0A1.ok.ok.cox.net
action=joining #^-^
ctime=1481998382
[Kurty|AFK]
address=~Kurty@FDB659B5:4BCC784B:A82A49AC:IP
action=changing nick to Kurty
ctime=1421293849
output:
newmewfile.txt:
address=Artemis@FUR-6D9151B4.hr.hr.cox.net
action=quitting IRC with message "Quit: http://www.kiwiirc.com/ - A hand crafted IRC client"
ctime=1441255636
address=~Kurty@FUR-3251C1B9.hsd1.ga.comcast.net
action=quitting IRC with message "Input/output error"
ctime=1476343809
address=~MajikBear@FUR-2CFCE0A1.ok.ok.cox.net
action=joining #^-^
ctime=1481998382
address=~Kurty@FDB659B5:4BCC784B:A82A49AC:IP
action=changing nick to Kurty
ctime=1421293849
seen_Kurty.ini:
[Kurty]
(missing three lines that should be writing.. in fact they're even writing to another file in an identical and adjacent command, but not to this file)
Does the filestream for some reason close automatically?
Yes it does.
1 2 3
fstream fs_writenew(s_write_file_name.c_str(), ios::out); // the name of the file is _seen[NAME].ini
fs_writenew << s_load_line; // this works
}
this fs_writenew is a new stream that is not related to the fs_writenew from line 12, and this stream is closed at the closing curly brace.
What you wanted to do was to call fs_writenew.open instead of creating a whole new stream: fs_writenew.open(s_write_file_name.c_str(), ios::out);
Or, not reuse stream objects at all: parse the file, and when a new section begins, write out what you accumulated so far. Also, never loop on stream.good() (or, equivalently, !stream.eof()). Also those streams should have been ifstream and ofstream, to avoid needless ios::out etc.. And closing the streams that are about to be closed anyway by a call to close() is bad form.
Thank you so much!
Yes I was declaring another stream instead of opening the one i already had declared.
Not sure what i was thinking heh.. but it works now, thanks again.
Yeah i know this code probably makes people wanna stab themselves in the eyes but i'm just trying to throw something together quickly for one time use.. once it does the job, won't be needed any more.