problem with cross-platform io su txt file

G

Giff

Hi,

I hope this post is not too OT.
I am porting some code from Windows to Linux and I am having a problem
with a routine that skips comments and empty lines in a txt file.
The following code has worked nicely on Windows, but it does not on
Linux, I understand that is because the two OS uses different way of
ending a line.

char next = static_cast<char>(is.peek());

//if the first character of the line is a comment or the endline
while( next == '\n' || next == comment ) {

//ignore the whole line
is.ignore( 10000, '\n' );

//check the next line
next = static_cast<char>(is.peek());

}

How would you suggest I should modify the above function to work under
Linux (or better under any OS)?

Thanks a lot,
G
 
G

Giff

Giff said:
Hi,

I hope this post is not too OT.
I am porting some code from Windows to Linux and I am having a problem
with a routine that skips comments and empty lines in a txt file.
The following code has worked nicely on Windows, but it does not on
Linux, I understand that is because the two OS uses different way of
ending a line.

So, well, very easily, I don't only check for '\n', but also for '\r'.
Now it works.
 
J

James Kanze

[...]
In my code, I always open all files in binary mode. I don't
want the run- time library to silently change the file content
under my feet. In my mind, the "text file mode" is
fundamentally flawed as the so-called text file may easily
originate from another system with other conventions, so the
run-time library level translation is not possible in general
(as demonstrated by OP).

I'm gradually tending in the same direction. Text mode works
well as long as you're not sharing disks; file transfer programs
should take care of the newline mappings. You run into the
problems when sharing disks, however; I often edit under Unix,
but compile and run under Windows.

Of course, '\r' is white space, so in practice, if your code
accepts trailing whitespace in a line (which it should), the
fact that you "see" an extra \r at the end of the line shouldn't
cause any problems.
 
G

Giff

Paavo said:
In my code, I always open all files in binary mode. I don't want the run-
time library to silently change the file content under my feet.

Mmmm, the problem is that I am not the one writing the text files I am
trying to read... I could fix the comments/empty lines issue, but other
parts of the text files also give problems...

Damn.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top