How to get rd of hidden characters in source code?

I copied a cpp file from the web and tried to compile with g++ in Ubuntu. It gave me a number of compile errors related to hidden characters I don't see. I now don't have access to my Windows 7 machine otherwise I would have solved this problem in no time. I usually copy such texts into a notepad.exe application. It does not tolerate any special characters beyond a limited ASCII set. Then I copy it back. In Ubuntu there is no such simple option.

Any suggestions to resolve this problem?

Thanks, A.
I find it odd that source code would regularly have 'hidden' or other unusual characters in it, particularly if you just cut-n-pasted text from a web page.

In Ubuntu there is no such simple option.
No, the option is simpler.

You can filter characters using the tr filter command.

$ mv foo.cpp foo2.cpp
$ tr -cd '\11\12\40-\176' < foo2.cpp > foo.cpp
$ rm foo2.cpp

http://linux.die.net/man/1/tr

Hope this helps.

[edit] Fixed a typo.
Last edited on
In Ubuntu there is no such simple option


Try running dos2unix on the file

Or for possible notepad replacements - gedit, kate, vi, vim, gvim,....
Any suggestions to resolve this problem?

Step 1: Copy and paste the error that you are getting from the system.
A nice little programming exercise. Write a program to do it. ;-)
Computergeek01, this is the output for you:

1
2
3
4
5
6
7
8
9
10
11
12
 alex@alex-Precision-WorkStation-R5400:~/GFORTRAN-APPS/SPHERE-DESIGN$ g++ glutSolidSphere.cpp
glutSolidSphere.cpp:78:5: error: stray ‘\342’ in program
     int const win_width  = …; // retrieve window dimensions from
     ^
glutSolidSphere.cpp:78:5: error: stray ‘\200’ in program
glutSolidSphere.cpp:78:5: error: stray ‘\246’ in program
glutSolidSphere.cpp:79:5: error: stray ‘\342’ in program
     int const win_height = …; // framework of choice here
     ^
glutSolidSphere.cpp:79:5: error: stray ‘\200’ in program
glutSolidSphere.cpp:79:5: error: stray ‘\246’ in program
 


It is actually a well know problem on the web. You can goodgle for "stray '\200' in program" and see quite a few posts.

Now, I am using gedit for the most part but yesterday I also tried vim. I think vim was thoroughly confused with those hidden characters that I could not close vim but though crushing the terminal. I also tried nano. No help there either.

I will try the dos suggestion. Thanks.

P.S. I also tried to post a URL to the website I got the code from. I cannot find it, believe it or not. I keep finding posts with the same name for the cpp file but the code is different. If I find it I will post it.
Last edited on
@tipaye
GCC doesn't choke on stray CRs.

@Computergeek01
+ 1

@Peter87
Presumes OP both knows how to and wants to spend time doing that.
Why not just use the *nix utility that does it?
GCC doesn't choke on stray CRs.


Didn't think it did. Just didn't want him to fire up vi and then panic when he sees all the "^m"
@ OP: *Facepalm* The ellipses on Lines 78 and 79 are a place holder, not actual C or C++ code. You're supposed to replace those with a function that works with your platform or hard-code it if you're feeling lazy, that is why there are comments at those lines indicating that you should do so.
Last edited on
I just tried duoas' suggestion. An interesting thing happened. First I created two copies of the cpp file. Then I ran the tr command:
 
tr -cd '\200\342' < glutSolidSphere_temp2.cpp > glutSolidSphere_temp.cpp


After the first trial the number of errors declined and I got only three lines instead of many. I decided to repeat the command thinking that perhaps another run would find the remaining stray characters but I changed the command slightly:

tr -cd '\200\342\' < glutSolidSphere_temp2.cpp > glutSolidSphere_temp.cpp

I got a warning:

tr: warning: an unescaped backslash at end of string is not portable

I removed the backlash and tried to run the command again and again to no avail. The command r stopped working although it definitely reduced the number of hidden characters and wiped out \246\ completely.

Thanks, - A.
Last edited on
computergeek01, you are probably talking about these two statements:

1
2
  int const win_width  = …; // retrieve window dimensions from
    int const win_height = …; // framework of choice here 


Thank you for the suggestion. What is the "window" the code is talking about? I am not lazy, just busy but now I am up to my ears in this stuff anyway.

Thanks, - A.
There's nothing wrong with being lazy, my being lazy is what gives me the motivation to automate and script everything. Otherwise I'd be doing a thousand mundane and brain dead tasks a day by hand like my idiot predecessor.

But you are correct, those are the lines that I mean. The "Window" is the drawing context assigned to the process, these variables are associated with the actual visual element of the process. I'm sorry but I'm a Windows guy and I have no idea how to grab this on *nix.
Computgergeek01, thank you for suggestion again. I think you hit the right point. I replaced the ellipses with arbitrary numbers 120 for the width and 50 for the height. All those crazy errors disappeared immediately, so that was the problem. Now I got only one compile error:

1
2
3
4
glutSolidSphere.cpp: In function ‘void display()’:
glutSolidSphere.cpp:98:17: error: ‘swapBuffers’ was not declared in this scope
     swapBuffers();
                 ^


I will try to figure out how I can define it but if you have a thought to share, that will be appreciated also. I have to find this function on the web somewhere.

Thanks, - A.
Last edited on
Yeah, that one is weird because "SwapBuffers()", with a capitol 'S', is a WinAPI function. This is going to be another platform specific one. SFML would be easy enough to integrate into this project, you should consider using that to provide your rendering context.
Thank you again, computergeek01. It bothers me that so much code posted on the web is in fact unusable because a header file is missing or some obscure function is not there, etc. Why do they post the code then?

Let me describe what my ultimate goal is. I know it is doable. I may end up writing this all code myself in Gfortran using formulas that are available in Wikipedia. It is all about projections of the 2-sphere and some functions and figures defined on it. I do a lot of special functions like Spherical Harmonics in relation to figures defined on 2-sphere. The mathematics itself is not hard but I found it difficult to visualize various images I define mathematically placed on the sphere. So, I decided to find a code that will possibly do it for me.

I checked already on SwapBuffers and found out that it is a Windows API, yes. There are also some other sites that delve into it but I could not find anywhere the actual file posted, for the most part it is all some philosophy about it. Also I think it is considered outdated and not supported although I don't know what support the file needs.

If you have any good thoughts, please post them. That idea of yours to remove the ellipses was a winner.

I will try the SFML although I am wary of gamers and such. If this software is favourite of gamers it may be difficult for me to use.

Thanks.

- A.
Last edited on
Topic archived. No new replies allowed.