By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

C++ I/O for parsing a simple file

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
Jul 23 '05 #2

P: n/a
Nafai wrote:
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.
Jul 23 '05 #3

P: n/a
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ó:
Nafai wrote:

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.

Jul 23 '05 #4

P: n/a
> 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.
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.