C++ I/O for parsing a simple file

N

Nafai

Hello!

I'd be very grateful if you can give some advice to me. I explain my
problem:

I want to read a file whose structure I need to be like this:

#comments...
value1,value2,value3,...,valueN # comments...
#comments...
value1,value2,value3,...,valueN # comments...
....
value1,value2,value3,...,valueN # comments...

So what I want is to be able to read a file in which there are some
lists of values separated by new line. Each value is separated by a
comma. And there may be comments at the beggining or at the end of any line.

I can't use getline(...,...,'\n') because I have to take care of '#'
I can't use getline(...,...,'\#') because comments are not compulsory.

So what do you suggest?

thanks.
 
V

Val

|
| I can't use getline(...,...,'\n') because I have to take care of '#'
| I can't use getline(...,...,'\#') because comments are not compulsory.
|

Yes you can. Then check for the existance of "#". It it exists, remove it and everything that comes after that.
 
A

Alvin Beach

Nafai said:
Hello!

I'd be very grateful if you can give some advice to me. I explain my
problem:

I want to read a file whose structure I need to be like this:

#comments...
value1,value2,value3,...,valueN # comments...
#comments...
value1,value2,value3,...,valueN # comments...
...
value1,value2,value3,...,valueN # comments...

So what I want is to be able to read a file in which there are some
lists of values separated by new line. Each value is separated by a
comma. And there may be comments at the beggining or at the end of any
line.

I can't use getline(...,...,'\n') because I have to take care of '#'
I can't use getline(...,...,'\#') because comments are not compulsory.

So what do you suggest?

thanks.

I would use an fstream and read a line at a time (so basically the
getline()). Then I would check the first character, if it's a # then the
line is a comment. If not, the the line contains values a possible comment.
How you handle when a line doesn't start with a # can be done 101 different
ways.
 
N

Nafai

I would use an fstream and read a line at a time (so basically the
getline()). Then I would check the first character, if it's a # then the
line is a comment. If not, the the line contains values a possible comment.
How you handle when a line doesn't start with a # can be done 101 different
ways.

I asked this question because there are many files with this structure
and I thought there would be a "standard" way of doing sth like this. I
can do it my way but I would like to hear some tips or frequent ways of
doing this.

Thanks.
 
N

Nafai

I asked this question because there are many files with this structure
and I thought there would be a "standard" way of doing sth like this. I
can do it my way but I would like to hear some tips or frequent ways of
doing this.

Thanks.


Alvin Beach escribió:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top