4

SITUATION:

My instructor for my micro-controller class refuses to save sample code to a text file and instead saves it to a word document file instead. When I open up the doc file and copy/paste the code into my IDE "CodeWarrior" it causes errors upon compile time.

I am having to rewrite all the code into a text editor and then copy/paste it into my IDE.

MY UNDERSTANDING:

I was told to always save code as a text file because when you save code as a word document file it will bring in unwanted characters when your copy/pasting the code into your IDE for compiling.

MY QUESTIONS TO YOU:

1.)

Can someone explain this dilemma to me so I can understand it better? I would like to present a better case next time when I receive errors and to also know more about what is happening.

2.)

Is it possible to write a script that will show me all the characters that are being copied and pasted into a file when the code is coming from a word document vs. a text file? In otherwords is there a program that will allow me to see what is going on between copying/pasting code from a word doc file versus a txt file?

Shane Yost
  • 231
  • 6
  • 14
  • 2
    For 1) consider that by default, Word inserts "smart" quotes when you type the `"` or `'` character. The smart quotes are different codepoints from what the plain quotes that your compiler will understand. There are other examples, but I think this one is hit most frequently. – shoover Jan 15 '14 at 19:55
  • 1
    Within Word, perform a "Save as" of the .doc file as a .txt file using type "Plain Text (*.txt)", "Allow character substitution", adjust "End lines with" to "LF only". Then copy/paste from the .txt file. – chux - Reinstate Monica Jan 15 '14 at 20:28
  • Using Word to write code is indeed silly. Any plain code editor worth its hard disk space will run rings around any of Word's "smart" features. What does your professor use it for, grammar checking his sources? – Jongware Jan 15 '14 at 20:34
  • @Jongware I have idea what he is doing. could you perhaps comment on my last question "comment" I posted. – Shane Yost Jan 15 '14 at 21:04

2 Answers2

5

Saving source code as a Word document is just silly. If your instructor is insisting on this, chances are no matter how well-reasoned and thorough your argument, they're not going to listen. They're beyond help.

However, to answer your questions: 1) It depends on what you're pasting the thing into. Programs that copy onto the clipboard usually make the data available in several different formats, ranging from their own internal format to plain ASCII text, to maximize compatibility so that the data can be pasted into pretty much any target program. Most text editors will only accept the plan-text version, in which case no extra characters should be transferred. However if your text editor supports RTF or HTML, this may not be true. I'm not sure what CodeWarrior supports but it is certainly possible.

A workaround if this is the case: First paste into a PURE text editor like Notepad. Then copy from Notepad into CodeWarrior. This should eliminate any hidden formatting. As shoover said above, make sure double-quotes " are really double-quotes and not the fancy left- and right-specific quotes that Word sometimes uses.

  1. Use a hex editor like XVI32 to see the raw contents of the file, including nonprinting characters. Or use a text editor with support for showing nonprinting characters (vi/vim, etc.).
TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • 1
    "Smart hyphen" is another one--a simple dash gets converted to an en-dash; non-breaking spaces another. When in doubt I use GREP: `[^[:ascii]]+` will find *everything* not-ASCII. – Jongware Jan 15 '14 at 20:32
  • Some students in class were using the schools computers "Windows 7" when they were copying and pasting the code into the IDE "Code Warrior". It somehow compiled for them but when I copied and pasted the code on my Mac I got a compile error. Some other people copied the code from their PC and got a compile error also so I know it's not just my Mac. Could someone explain to me what may be causing this. I realize it could be several reasons I would just like to have a better understanding. – Shane Yost Jan 15 '14 at 20:46
  • Also I'm using Parallels which lets me run Windows 7 along side of Mac OS X. Next time someone tells me it's my Mac and my software "Parallels" I would like to state a better case. I just hate it when i'm told my Mac is causing the issue and it's not a Windows problem. – Shane Yost Jan 15 '14 at 20:48
0

I'm studying C and I've just had the same problem. When coping a piece of code from a PDF file and trying to compiling it, gcc would return a serie of errors. Reading the answer above I had an idea: "What if I converted the utf8 into ascii?". Well, I found a website that does just that (https://onlineutf8tools.com/convert-utf8-to-ascii). But instead of also converting the utf8 characters into ascii, it showed them as hexadecimals (Copying from the website to the text editor you can see it better). From there i realised that the problem were mostly the quote marks "". I then copied the ascii "translation" into my code editor (I must add that it worked fine with Sublime, while VScode read the same utf8 code as it was in the original file, even after cp from the website) and replaced all the hex with the actual ascii characters that were needed to compile the code properly. I used the function find and replace from my editor to do it. I must say that it wasn't very fast doing it. But I believe that in some cases, if the code you're trying to copying is too long, doing it the way I've just described could be faster than rewriting the entire code.