简体   繁体   English

从文件写入和读取时的C文件IO额外字符

[英]C file IO extra characters when writing and reading from file

Ok so i am writing a C program. 好的,我正在写一个C程序。 The program is simple enough, when i run the program i give it a few parameter Ex ./proj1 cat hat bat, so then it asks me to input a list of words the program gives counts of how many times "cat", "hat", and "bat" occurs in that list. 程序很简单,当我运行程序时,我给它一些参数Ex ./proj1猫帽蝙蝠,所以它要求我输入一个单词列表,程序给出了多少次“猫”,“帽子” “和”bat“出现在该列表中。 I have the program working great. 我的程序运行良好。

Example 
./pro1 cat hat bat
cat
.

(the program recognized a "." as the end of input) (程序识别出“。”作为输入的结尾)

Result:
cat:1
hat:0
bat:0

ok so my program runs perfectly in every test case i can think of, but I have to pass a series of tests that my professor has mas made. 好的,所以我的程序在我能想到的每个测试用例中都能完美运行,但是我必须通过我教授所做的一系列测试。 here is the code of that test. 这是该测试的代码。

char *args[] = {"./main", "cat", "hat","bat",NULL};
char *result[] = {"Looking for 3 words\n",
  "Result:\n",
  "cat:1\n",
  "hat:0\n",
  "bat:0\n"};
FILE *out;
FILE *test;
test=fopen("test","w");
int i;
char *buffer=malloc(100*sizeof(char));

out = fopen("smp0.in", "w");
fprintf(out, "cat\n");
fprintf(out, ".\n");
fclose(out);

freopen("smp0.in", "r", stdin);


freopen("smp0.out", "w", stdout);

quit_if(main(4, args) != EXIT_SUCCESS);

fclose(stdin);
fclose(stdout);

out = fopen("smp0.out", "r");


for (i = 0; i < 5; i++) {
    quit_if(fgets(buffer, 100, out) == NULL);
    quit_if(strcmp(buffer, result[i]));
}


fclose(out);
return EXIT_SUCCESS;
 }

ok so sending the quit_if() is the method that makes it fail. 好的,所以发送quit_if()是使其失败的方法。 specifically 特别

   quit_if(strcmp(buffer, result[i]));

My output when i run the program is exactly as described. 我运行程序时的输出完全如上所述。 But between freopen() diverting stdout to a file and then reading it back it has changed somehow. 但是在freopen()将stdout转移到文件然后再读回来之间它已经发生了某些变化。

  Result:
  cat:1
  hat:0
  batÿ:0

is what the output becomes, but it is not like that before the file write and read, and for some reason it is always that weird y character. 输出变成了什么,但它不像文件写入和读取之前那样,并且由于某种原因它总是那个奇怪的y字符。

any advice would be greatly appreciated. 任何建议将不胜感激。 Sorry for not posting more code but it's because it is a school project. 很抱歉没有发布更多代码,但这是因为它是一个学校项目。 I am confident that it is the test that is wrong in some way and not my code, fixing the test is part of the project as well. 我确信在某种程度上测试是错误的而不是我的代码,修复测试也是项目的一部分。

See this answer to a previous question: 查看上一个问题的答案:

https://stackoverflow.com/a/4906442/2009431 https://stackoverflow.com/a/4906442/2009431

It seems that when the dot is read back in from your stdin file, it has an EOF token appended (makes sense) that would not normally be part of the user's input. 似乎当从stdin文件中读回点时,它附加了一个EOF标记(有意义),通常不会成为用户输入的一部分。 Then, somehow (not sure since we can't see your code) your main() function is appending that EOF character onto "bat" in the form of that weird y character (see linked answer for details on why). 然后,不知何故(不确定,因为我们看不到你的代码)你的main()函数将这个EOF字符以怪异的y字符的形式附加到“bat”(有关原因的详细信息,请参阅链接的答案)。

If I'm right, maybe this could be considered a bug in the test? 如果我是对的,也许这可能被视为测试中的错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM