问题
I was recently assigned the task of creating a program decommenter in C for one of my classes. While this is a short, simple little project, I have run into an issue that has become a major headache. The program works 100% fine, except it will not recognize quotes on my Mac computer. However, my professor tested it on his Linux computer and it worked with no issue. Here's the source code:
#include <stdio.h>
#define IN 0
#define OUT 1
#define QUOTE 2
int main(int argc, char** argv) {
int state = OUT;
int reader;
while ((reader=getchar()) != EOF) {
if (state==OUT) {
if (reader=='\"') {
state = QUOTE;
}
if (reader=='/') {
if ((reader=getchar()) == '*') { state = IN; continue; }
else putchar('/');
}
if (reader!=EOF) putchar(reader);
} else if (state==QUOTE) {
if (reader=='\"') state = OUT;
putchar(reader);
} else {
if (reader=='*') {
if ((reader=getchar()) == '/') state = OUT;
}
}
}
return 0;
}
When I enter the following input (via shell redirection in the terminal):
/* this is a test */ this is still a test “ /* still testing */ ”
/* “more testing” */
I get the following as output:
this is still a test “ ”
Rather than:
this is still a test “ /* still testing */ ”
I tried testing the following bit of code, using the same input, but it did not print anything:
int reader;
while ((reader=getchar()) != EOF) {
if (reader=='\"') printf("true")
}
I'm totally dumbfounded by this, but perhaps someone here knows the solution?
回答1:
The quotes that you type in have been replaced by curly quotes. This should not happen when you type quotes in terminal, so I guess you created a text file, using some text editor, and then feed that text file to your program. If it is so, then simply edit your text file, disable "smart quotes" in System Preferences -> Keyboard -> Text, replace your quotes by straight quotes, then try again...
回答2:
Well, you're feeding your program LEFT DOUBLE QUOTATION MARK (“) and RIGHT DOUBLE QUOTATION MARK (”) instead of the QUOTATION MARK (").
来源:https://stackoverflow.com/questions/21889349/c-not-recognizing-double-quotes-on-mac-osx