Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 当我看到代码正确运行时,为什么代码仍然将字符串打印到另一个文件中?_C - Fatal编程技术网

C 当我看到代码正确运行时,为什么代码仍然将字符串打印到另一个文件中?

C 当我看到代码正确运行时,为什么代码仍然将字符串打印到另一个文件中?,c,C,我仔细地调试了它,看到代码通过这个命令运行 else if (vowel == smallv) { fprintf (tempfilesmall, "\n%s",str); } 但实际上,该代码将某些内容打印到tempfilebig 我的意见是: happy good bad bad bad bad bad hehe “坏”应该打印到临时小值文件,而不是打印到临时大值文件。 这很奇怪,有人知道发生了什么吗 tempfilebig = freopen ("tempbig.txt","w

我仔细地调试了它,看到代码通过这个命令运行

else if (vowel == smallv)
{
    fprintf (tempfilesmall, "\n%s",str);
}
但实际上,该代码将某些内容打印到tempfilebig

我的意见是:

happy good bad bad bad
bad bad
hehe
“坏”应该打印到临时小值文件,而不是打印到临时大值文件。 这很奇怪,有人知道发生了什么吗

tempfilebig = freopen ("tempbig.txt","w",stdout);
tempfilesmall = freopen ("tempsmall.txt","w",stdout);

freopen
返回值是流,在本例中为
stdout
。因此,您实际上会在以下几行中覆盖
文件
指针:

tempfilebig = freopen ("tempbig.txt","w",stdout);
tempfilesmall = freopen ("tempsmall.txt","w",stdout);
既然您想基本清除这些文件并重新打开它们,那么就这样做吧

tempfilebig = freopen ("tempbig.txt","w",tempfilebig);
tempfilesmall = freopen ("tempsmall.txt","w",tempfilesmall);

freopen
返回值是流,在本例中为
stdout
。因此,您实际上会在以下几行中覆盖
文件
指针:

tempfilebig = freopen ("tempbig.txt","w",stdout);
tempfilesmall = freopen ("tempsmall.txt","w",stdout);
既然您想基本清除这些文件并重新打开它们,那么就这样做吧

tempfilebig = freopen ("tempbig.txt","w",tempfilebig);
tempfilesmall = freopen ("tempsmall.txt","w",tempfilesmall);

@WeatherVane你现在可以看到代码段。@WeatherVane你现在可以看到代码段了。这意味着我应该忽略“freopen”(“tempsall.txt”,“w”,stdout)”之前的“tempfilesmall=”?@MichaelCruz是的,但之后你会意识到这些行没有任何作用,因为它们实际上是将stdout重定向到你的文件,但是您不向stdout写入任何内容,因此它们毕竟是毫无意义的。我真正想做的是使用freopen清除tempsall中已经存在的内容,这样我就可以为它写入新值。然后保留行并用FILE pointer变量替换stdout。编辑答案以反映这一点,这意味着我应该忽略“freopen”(“tempsall.txt”,“w”,stdout)”之前的“tempfilesmall=”?@MichaelCruz是的,但在此之后,您会意识到这些行没有任何作用,因为它们实际上会将stdout重定向到您的文件,但是您不向stdout写入任何内容,因此它们毕竟是毫无意义的。我真正想做的是使用freopen清除tempsall中已经存在的内容,这样我就可以为它写入新值。然后保留行并用FILE pointer变量替换stdout。编辑答案以反映这一点