C not recognizing double quotes on Mac OSX

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 12:49:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!