LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-24-2018, 06:51 AM   #1
BruceV
LQ Newbie
 
Registered: Jan 2016
Posts: 22

Rep: Reputation: Disabled
Problems with getline and Windows generated input


I’m porting a C++ program from Windows to Linux (Debian/GCC). It gets its ‘instructions’ by reading lines from a text control file. The essentials of that part of the code are these. Irrelevant details such as setting string length and file opening are omitted, there is certainly no problem with them.

ifstream ifs ;
string strbuf ;

While (!ifs.eof())
{
(void) getline (ifs, strbuf) ;
Strbuf += “,@” ; // append guard chars
Do more stuff
}

For convenience, I used a control file that had been prepared on Windows Notepad++, and immediately ran into trouble. The guard characters, rather than being appended, were overwriting the first two characters of the control string, as follows:

Line read: CtrlCommand
Want to get: CtrlCommand,@
What I got: ,@rlCommand

It turned out that the problem was the command file format. Linux appears to terminate lines with 0AH, whereas Windows uses 0DH 0AH. A cout of the lines ahead of the guard char insertion didn't show any irregularities.

In practice, I can’t control where control files come from, so my solution has been to write my own getline routine, parsing the lines in binary, dealing correctly with the line terminators, and casting the asciz string to a std::string. So the problem is solved, I just wonder whether anyone can explain how the file format is causing the append to turn into an overwrite.

Also maybe this might help someone else who encounters the problem.

Last edited by BruceV; 07-24-2018 at 07:05 AM.
 
Old 07-24-2018, 11:26 AM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
How are you moving the code from Windows to Linux?

The end-of-line characters are different (Windows uses CRLF, Linux just LF), and must be converted.
Probably the easiest way to do this is with an (S)FTP ASCII transfer, which does just that...converts the line ends...that's the definition of an ASCII transfer.

If you're sneakernetting, run the files(s) through the dos2unix utility:
Code:
dos2unix filename > newfilename
Just re-read...sorry, you already knew about the line end stuff. Note that if you run dos2unix on a file that doesn't need it's line-ends changed, nothing will be changed...so you can run everything through that unconditionally. If you always (ASCII) transfer from Windows via (S)FTP, the line ends will be converted automatically.

The overwriting is caused by the CR. Think of a typewriter (remember those?) being set back to the beginning without advancing the paper...the next line would be typed over the previous one.
A CRLF in a script/program almost always causes the interpreter or compiler to choke.

Last edited by scasey; 07-24-2018 at 11:35 AM.
 
1 members found this post helpful.
Old 07-24-2018, 01:58 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Remove a trailing "\r" from strbuf before you add the ",@".
I am not used to C++ and its string functions; maybe something like
Code:
strbuf.erase(strbuf.find_last_of("\r"));
 
Old 07-24-2018, 11:06 PM   #4
BruceV
LQ Newbie
 
Registered: Jan 2016
Posts: 22

Original Poster
Rep: Reputation: Disabled
Thanks for the responses. The dev system is networked off a PC running Ubuntu. I simply inserted a thumb drive with the Windows file in it, then copy/pasted into Users/Bruce. Then used filezilla to move it onto the dev target. The file transferred with no alterations, the CRLFs were still present, that was the problem.

Thanks also for the suggestions re file conversion. in my situation I have no control over who uses the application, and no doubt some people will do what I did without being aware of the EOL mismatch. That's why I chose a bombproof approach of converting the file format myself.
 
Old 07-24-2018, 11:54 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by BruceV View Post
in my situation I have no control over who uses the application, and no doubt some people will do what I did without being aware of the EOL mismatch. That's why I chose a bombproof approach of converting the file format myself.
Don't doubt that your script does the job. Just feel the need to point out (mostly to others) that you re-invented an existing wheel. dos2unix (and unix2dos) are existing utilities for converting EOL as needed.
 
  


Reply

Tags
file format, getline, windows 10



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
128 bits key generated when 1024 was generated(centos 6.5)using openssl SarahGurung Linux - Security 3 07-29-2014 07:03 AM
Problens with Mercury Installation ricguitar SUSE / openSUSE 3 12-07-2005 07:15 PM
Problens installing mandrake 9.1 gbresc Linux - General 1 10-10-2005 07:59 AM
Et server problens JackieBrown Linux - Games 0 01-19-2005 04:46 PM
cannot print documents generated on Windows by CUPS in FC2 kentri9 Linux - Software 2 11-26-2004 06:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration