The difference of '\n' in windows and linux.

Reply

Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

The difference of '\n' in windows and linux.

 
0
  #1
Nov 9th, 2007
Hello there. Is there anyway that you can convert the '\n' in windows which is 2bytes to the '\n' 1byte so that the linux could read it?
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

Re: The difference of '\n' in windows and linux.

 
0
  #2
Nov 9th, 2007
In addition, does the fread() function can use wchar_t* type as buffer?
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: The difference of '\n' in windows and linux.

 
0
  #3
Nov 9th, 2007
Is there anyway that you can convert the '\n' in windows which is 2bytes to the '\n' 1byte so that the linux could read it?
Sure. There is a little Unix utility called dos2unix. It is found on most *nix systems.

To do it in code, just set your output file mode to "binary". Doing so turns off the '\n' to "\r\n" translations, so when you write your file you get exactly what you write --single newline and all.

In addition, does the fread() function can use wchar_t* type as buffer?
No, it uses char. Sorry.

However, there are usually functions to convert a binary string to a unicode string...
(I don't know what they are though.)

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: The difference of '\n' in windows and linux.

 
0
  #4
Nov 9th, 2007
converting ascii text files is trivial; you have a variety of tools. eg. tr, perl, awk, vi etc.in fact anything that can replace a cr-lf sequence with just a cr. eg.
awk '{ sub("\r$", ""); print }' winfile.txt > unixfile.txt
you could also use a utility called tofrodos which is available on a number of systems (under different names like "todos", "fromdos", "dos2unix", "unix2dos" etc.)
http://kb.iu.edu/data/acux.html
http://www.thefreecountry.com/tofrodos/

if the files are in unicode, these simple techniques will not work. unicode has a variety of newlines: CR (000D), LF (000A), CRLF (000D,000A), NEL (0085), FF (000C), LS (2028), and PS (2029). for a perl hack see http://www.onlamp.com/pub/a/onlamp/2...es.html?page=3. it is easy in c++ to write a function that processes input character by character and does the necessary translations.

fread and fwrite are to i/o what memcpy is to memory. it reads or writes bytes into untyped memory (void*) without any formatting or interpretation. the stream should be opened in binary ("b") mode to prevent newline character translations by the underlying layers. for example,
  1. int array[100] ;
  2. // to write an array (binary) into afile
  3. fwrite( array, sizeof(int), 100, file );
  4. // to read the array (binary) from a file
  5. fread( array, sizeof(int), 100, file );
  6.  
  7. struct some_struct mystruct ;
  8. // write
  9. fwrite( &mystruct, sizeof(struct some_struct), 1, file ) ;
  10. // read
  11. fread( &mystruct, sizeof(struct some_struct), 1, file ) ;
using these functions correctly is difficult; mainly because they do not distinguish between end of file and error. use of feof and ferror is required to determine what has occurred.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC