file validation

O

ozgun.harmanci

Hi everyone,
I had a big problem with line returns and carriage character
difference in windows and linux. I think what happened was, the scp
program i am using in to send file from windows machine to a linux
machine did not somehow realize that the files i am uploading are
ascii files and sent them as binary. When I tried to use those files,
input to another program, in linux I was getting strange problems with
them until i realized that the line returns and carriages were the
problem.

Is there a way in C++ to determine and tell the user that the line
returns and carriages are compatible with linux or windows to warn
them against input problems? Specifically i am looking for ansi c
function to accomplish this in a robust way so as to have the input
validation correctly set.
Thanks a lot in advance,
Oz.
 
V

Victor Bazarov

ozgun.harmanci said:
[..]
Is there a way in C++ to determine and tell the user that the line
returns and carriages are compatible with linux or windows to warn
them against input problems? Specifically i am looking for ansi c
function to accomplish this in a robust way so as to have the input
validation correctly set.

No. All this you're describing has nothing to do with C++. Besides,
nobody warns anybody when carriage returns are concerned, but instead
the code is usually written in such a way that those things do not
matter, and all files are handled regardless of their originating OS.

V
 
D

David Harmon

On Sat, 10 Nov 2007 17:03:05 -0000 in comp.lang.c++, "ozgun.harmanci"
Is there a way in C++ to determine and tell the user that the line
returns and carriages are compatible with linux or windows to warn
them against input problems? Specifically i am looking for ansi c

Open the file with ios::binary. Read it and count the number of LF
characters that are/are not preceded by CR.
 
A

Alf P. Steinbach

* Victor Bazarov:
ozgun.harmanci said:
[..]
Is there a way in C++ to determine and tell the user that the line
returns and carriages are compatible with linux or windows to warn
them against input problems? Specifically i am looking for ansi c
function to accomplish this in a robust way so as to have the input
validation correctly set.

No. All this you're describing has nothing to do with C++. Besides,
nobody warns anybody when carriage returns are concerned, but instead
the code is usually written in such a way that those things do not
matter, and all files are handled regardless of their originating OS.

I think it is pretty much relevant, a language issue. Because I don't
think we would have had text mode, I don't think it would be impossible
to write a standard C++ 'cat' on systems where it's meaningful, if it
weren't for Windows' line ending convention. And the short of it is
that for standard input there's no standard system-independent way to
turn off that infernal havoc-wreaking band aid, but streams that are
explicitly opened can be opened in binary mode.

Cheers,

- Alf
 
J

James Kanze

I had a big problem with line returns and carriage character
difference in windows and linux. I think what happened was, the scp
program i am using in to send file from windows machine to a linux
machine did not somehow realize that the files i am uploading are
ascii files and sent them as binary. When I tried to use those files,
input to another program, in linux I was getting strange problems with
them until i realized that the line returns and carriages were the
problem.
Is there a way in C++ to determine and tell the user that the line
returns and carriages are compatible with linux or windows to warn
them against input problems? Specifically i am looking for ansi c
function to accomplish this in a robust way so as to have the input
validation correctly set.

When reading a Windows file on Unix, you will usually see an
additional '\r' in front of every new line. Just ignore it.
(If you are reading text, it counts as white space---isspace(
'\r' ) returns true in all the locales I've used---, so in a lot
of cases, it will be ignored automatically.

When reading a Unix file under Windows, you formally have an
illegal text format, which could cause istream to report an
error. In practice, the implementations I've used all treat an
LF which is not preceded by a CR as if it were, and read Unix
files without problems. (This is not the case with some
programs, however. I don't know what compiler they were
compiled with.)

There used to be two programs, unix2dos and dos2unix, under
Unix, which would convert the files "after the fact", if you
ended up with the wrong type. I don't seem to have them on my
Linux here at home, however. Of course, they'd be very easy to
write, as long as you read and write in binary (so that you
control the representation of line endings). But that doesn't
help much in practice, since most of the time, such problems
occur because of a shared file system.
 
B

BobR

David Harmon wrote in message...
On Sat, 10 Nov 2007 17:03:05 -0000 in comp.lang.c++, "ozgun.harmanci"


Open the file with ios::binary.

Or:

std::ifstream in( "file.typ", std::ios_base::in |
std::ios_base::binary );
 
O

ozgun.harmanci

I agree that this question might not be perfectly relevant with this
group but my question was: I am looking for an ansi c/c++ way to deal
with the problem. I think I am going to go with reading file in binary
and checking for CR LF characters.. thanks a lot everyone..
 

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

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top