Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problem with cross-platform io su txt file

Reply
Thread Tools

problem with cross-platform io su txt file

 
 
Giff
Guest
Posts: n/a
 
      07-09-2008
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

 
Reply With Quote
 
 
 
 
Giff
Guest
Posts: n/a
 
      07-09-2008
Giff wrote:

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


of course, *is* is a istream object.
 
Reply With Quote
 
 
 
 
Giff
Guest
Posts: n/a
 
      07-09-2008
Giff wrote:
> 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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-10-2008
On Jul 9, 10:07 pm, Paavo Helde <(E-Mail Removed)> wrote:

[...]
> 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.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Giff
Guest
Posts: n/a
 
      07-10-2008
Paavo Helde wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file dgcosgrave@gmail.com Python 8 12-19-2012 06:29 PM
Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Jochen Brenzlinger Java 7 09-15-2011 01:23 AM
How to convert a .txt file extension to a .xls file extension? Steve ASP .Net 3 08-25-2006 05:43 PM
Opening a txt file to view ( i.e. readme.txt) Sameen C++ 2 08-29-2005 03:14 PM
File Access error - writing to .txt file John Carnahan ASP .Net 2 07-18-2003 10:35 PM



Advertisments