Currently I've successfully write to my file some random numbers between 0 to 10 with this code(below is just some sample code to demonstrate the problem):
for (int i = 1; i <= size; i++)
{
type = rand () % 3;
switch (type)
{
case 0: afile << rand () % 10;
break;
case 1: afile << rand () % 10;
afile << "\t\t";
afile << rand () % 10;
break;
case 2: afile << rand () % 10;
afile << "\t\t";
afile << rand () % 10;
afile << "\t\t";
afile << rand () % 10;
/*afile << "8";
afile << "\t";
afile << "7";
afile << "\t";
afile << "2";*/
}
afile << "\t\t" << "// Possible type " << i << endl;
}
then my afile
look like this after executing the code:
8 // Possible type 1
1 7 // Possible type 2
4 0 3 // Possible type 3
The problem occur when I read the data from this file and output to another file:
int type;
while (afile >> type)
{
if(type == 0)
{
afile >> .........;
..........
}
else if(type == 1) {.........}
else if(type == 2) {.........}
}
}
......................................
My output file stop reading after the first line since it's also read the comment which is invalid data to process, if I delete the comment then everything is working fine. So how can I overcome this situation? Thank you.
Tony D already provided a reasonable answer as I see, but I thought I'd add a code of my own too, since I already wrote and tested it.
The below is pretty much self-explanatory for anybody speaking C++, it's basically what Tony proposed, but with a twist - getting the data line by line, making use of
std::stringstream
,but then also making use of the binary nature of the data OP uses. The data there is either a valid integer, or a comment. Or in other terms, either a valid integer, or not. So in the code below when a valid conversion of the data from the stream to integer can't be made - the rest of the line is treated as a comment.edit: ...actually, while it was a somewhat valid solution, I modified the code to incorporate a bit saner approach - one that skips a comment (denoted by#
or//
to show two ways to do it) but still lets us decide what to do on a malformed value. This does not allow for a45fubar
to pass as45
and then badfubar
, which was a problem with the previous code, but allows for45//comment
to be interpreted correctly.I still think just outright cutting out
\/\/.*?
is a better approach though. The point of this answer though is to be a bit different. ;)An example run:
You've a couple reasonable options:
read the entire line into a
std::string
, scan for and remove any comment, then create astd::istringstream
from whatever's left and stream from that to extract the non-comment valuesbefore reading values, use
>> std::ws
andafile.peek()
to see if the next character is a'/'
: if so skip until you reach a newline.The former is a useful technique to get used to in C++ (helps when you want to report line numbers with data issues), and looks like this:
There are a couple of ways to do this.
Skipping the rest of a line after you find a quote (Faster)
Basically what you would do here, is read the file line by line in a loop. When you hit two characters "//". You'd call "break;" and skip to the next line.'
Some untested dummy code:
Removing Quotes First (slower)
Here is a solution for removing quotes (one liners "//" and multi-liners "/**/") from a file. Basically, you would run this against the file you are processing before you start reading it for the number data.
http://www.cplusplus.com/forum/beginner/80380/