gpt4 book ai didi

C 在 Mac OSX 上不识别双引号

转载 作者:太空宇宙 更新时间:2023-11-04 01:31:29 28 4
gpt4 key购买 nike

我最近被分配了一项任务,即为我的一门课用 C 语言创建一个程序反注释器。虽然这是一个简短的小项目,但我遇到了一个令人头疼的问题。该程序 100% 正常工作,除了它无法识别我的 Mac 计算机上的引号。但是,我的教授在他的 Linux 计算机上对其进行了测试,并且没有任何问题。这是源代码:

#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;
}

当我输入以下内容时(通过终端中的 shell 重定向):

/* this is a test */ this is still a test “ /* still testing */ ”

/* “more testing” */

我得到以下输出:

this is still a test “ ”

而不是:

this is still a test “ /* still testing */ ”

我尝试使用相同的输入测试以下代码,但它没有打印任何内容:

int reader;
while ((reader=getchar()) != EOF) {
if (reader=='\"') printf("true")
}

我对此完全傻眼了,但也许这里有人知道解决方案?

最佳答案

您输入的引号已替换为弯引号。当您在终端中键入引号时,这不应该发生,所以我猜您使用某些文本编辑器创建了一个文本文件,然后将该文本文件提供给您的程序。如果是这样,那么只需编辑您的文本文件,在“系统偏好设置”->“键盘”->“文本”中禁用“智能引号”,将您的引号替换为直引号,然后重试...

关于C 在 Mac OSX 上不识别双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21889349/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com