It just seemed over the top at first because I felt like it shouldn't need to do this "translation" back and forth, and instead should have a way to set the "reading position" back to the beginning of the line, if it doesn't encounter a "comment line" (if that didn't make sense, don't worry about it). But something built-in like that wouldn't serve a broad use.
@yay295, sorry peek wouldn't work very well unless the # is at the very beginning, the code I have just sees if the hash is anywhere in the line.
Okay, almost done here, just got a really stupid problem happening, I'm sure there's some easy fix...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main()
{
std::string filename;
std::string equals;
std::string path;
std::ifstream f("find_comment.txt");
std::string line;
while (getline(f, line))
{
if (line.length() > 0 && line.find('#') != std::string::npos)
{
std::cout << "#some unread comment" << std::endl;
}
else
{
std::stringstream stream;
stream << line;
stream >> filename;
stream >> equals;
stream >> path;
std::cout << filename << " " << equals << " " << path << std::endl;
}
}
}
|
1 2 3 4 5 6 7 8 9
|
//file
#a category of config files
file.dat = ./data/some_file.dat
file2.dat = ./data/file2.dat
file3.dat = ./data/file3.dat
#another category
enemy.png = ./images/monster.png
player.png = ./images/hero.png
|
The problem here is that, in the program, the output repeats the printing of "file3.dat = ./data/file3.dat" for every line in between that and the "#another category" line. Meaning that it keeps taking in the stream data from the previous line, I'm not sure why that's happening. For example, if I have it so there's 10 blank lines in between "file3.dat..." and "#another category", it will print "file3.dat = ./data/file3.dat" 10 times because of something the stringstream is doing.
EDIT:
I changed the else statement to
else if (line.length() > 0)
. Seems to work fine now. Thanks everyone.
...gah still doesn't work if there's whitespace like spaces.