I am using following function:
int getline_count()
{
boost::smatch resultc;
string sLine;
int line_add_tmp;
ifstream infile;
infile.open("scripts.txt", ios_base::in);
if (!infile){
cerr << "scripts.txt could not be opened!" << endl;
}
else {
getline(infile, sLine);
if (boost::regex_match(sLine, c)) {
line_add = 2;
}
else {
line_add = 1;
}
return line_add;
infile.close();
}
}
Intension for the above function is to test if the first line in the file contains '// new' If true, 2 is returned. If false, 1 is returned. This works fine so far.
What I am confused about is that after the run the file scripts.txt is empty. How can that be since
1.) The '// new' line was correctly identified as I am getting '2' returned (running on an empty file returns 1 as expected). So it can't be that during opening the file scripts.txt it was overwritten with an empty file
2.) ifstream was designed to read only
What is it what I am missing?
Edit:
Definition for c is
static const boost::regex
c("^(\\/)(\\/)(new|New| new| New)"); // Regexp for line count