1

I've got a source code file that started as a copy of some sample code from a webpage. It was created and edited under Windows and compiled without any problems.

But under Macs, I get a load of obscure errors, like:

../MyProgram.cpp:1: error: stray '\255' in program
../MyProgram.cpp:1: error: stray '\254' in program
../MyProgram.cpp:1: error: stray '#' in program

../MyProgram.cpp:3:4: error: invalid preprocessing directive #i
../MyProgram.cpp:5:4: error: invalid preprocessing directive #i
../MyProgram.cpp:7:4: error: invalid preprocessing directive #i
../MyProgram.cpp:23: error: missing terminating ' character
../MyProgram.cpp:369:6: error: invalid preprocessing directive #i
../MyProgram.cpp:371:8: error: invalid preprocessing directive #i
../MyProgram.cpp:375:8: error: invalid preprocessing directive #e
../MyProgram.cpp:381:8: error: invalid preprocessing directive #e
../MyProgram.cpp:383:6: error: invalid preprocessing directive #e
../MyProgram.cpp:385:8: error: invalid preprocessing directive #i
../MyProgram.cpp:389:8: error: invalid preprocessing directive #e

../MyProgram.cpp:1: error: 'i' does not name a type
../MyProgram.cpp:53: error: 'V' does not name a type
../MyProgram.cpp:75: error: 'v' does not name a type
../MyProgram.cpp:157: error: 'l' does not name a type
../MyProgram.cpp:169: error: 'l' does not name a type
../MyProgram.cpp:187: error: 'i' does not name a type
../MyProgram.cpp:197: error: 'v' does not name a type

It looks like the problem is with some special characters.

How can I strip them off with a Unix-like command line?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maleev
  • 843
  • 3
  • 12
  • 21
  • under many distros of unix there is a dos2unix utility that should be under /usr/bin. Not sure of OSX has that... – Alex Jul 13 '09 at 18:10
  • http://dos2unix.darwinports.com/ – Jonathan Holloway Jul 13 '09 at 18:15
  • 3
    The issue isn't with special characters; it's with UTF-16: FFFE is your hint. – Williham Totland Jul 13 '09 at 18:17
  • If the strays are in ***decimal*** (hexadecimal 0xFF 0xFE), [UTF-16LE](https://en.wikipedia.org/wiki/UTF-16#Byte-order_encoding_schemes) [BOM](https://en.wikipedia.org/wiki/Byte_order_mark): *"If the 16-bit units use little-endian order ("UTF-16LE"), the BOM is the (hexadecimal) byte sequence FF FE"*. – Peter Mortensen May 15 '23 at 09:09
  • Are ***decimal*** stray numbers a signature of something or expected (GCC normally uses ***octal*** numbers)? Of [Clang](https://en.wikipedia.org/wiki/Clang)? - *"In [Xcode](https://en.wikipedia.org/wiki/Xcode) 3.2 and later, it included the Clang C/C++/Objective-C compiler ... based on LLVM.". [Xcode](https://en.wikipedia.org/wiki/Xcode#3.x_series): "Xcode 3.2 was released with [Mac OS X v10.6](https://en.wikipedia.org/wiki/Mac_OS_X_Snow_Leopard) (Snow Leopard)"*. Mac OS X v10.6 was released 2009-08-28. Though this question was submitted before that date. – Peter Mortensen May 15 '23 at 09:37
  • Related (but is at the current time mostly about actual special characters): *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332/)* – Peter Mortensen May 15 '23 at 09:41

2 Answers2

9

It looks to me as if the file was saved as UTF-16. Opening it in a text editor and re-encoding to UTF-8 should, with some luck, fix the problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Williham Totland
  • 28,471
  • 6
  • 52
  • 68
0

Originally, I was just going say how to remove the \255 and \254 characters, but I agree with the comments. It's in Unicode.

Try to convert it with iconv:

iconv -f iso-8859-1 -t utf-8 infile > outfile

iso-8859-1 is just a guess.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Charlie
  • 644
  • 3
  • 7