C 我的read_lines()函数在调用时未按预期运行。有没有办法修复我当前的实现?
奇怪的是,第30行的printf语句也没有运行。当我运行lldb调试器时,read_lines()中的所有printf语句和while循环都是根据箭头指示器执行的,但没有任何输出。但是,当read_lines()函数中的代码代替read_lines()函数(第11行)调用时,它会按预期运行并输出C 我的read_lines()函数在调用时未按预期运行。有没有办法修复我当前的实现?,c,macos,stdout,C,Macos,Stdout,奇怪的是,第30行的printf语句也没有运行。当我运行lldb调试器时,read_lines()中的所有printf语句和while循环都是根据箭头指示器执行的,但没有任何输出。但是,当read_lines()函数中的代码代替read_lines()函数(第11行)调用时,它会按预期运行并输出 #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { // proto
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
// prototyped my functions
void open_stream();
void read_lines();
open_stream(); // runs as expected
read_lines(); // does not run as expected
return 0;
}
// this function runs as expected
void open_stream() {
printf("Executes\n");
FILE *fp;
fp = freopen("file.txt", "w", stdout);
printf("This text is redirected to file.txt.\n");
printf("This is also redirected.\n");
fclose(fp);
}
// this function doesn't run as expected
void read_lines()
{
printf("Does not execute\n"); // doesn't print
FILE *fp;
int c;
fp = fopen("file.txt","r");
if(fp == NULL) {
perror("Error in opening file.");
}
while(1) {
c = fgetc(fp); // doesn't seem to work
if( feof(fp) ) {
break ;
}
printf("%c", c); // nothing gets printed
}
fclose(fp);
}
预期产出:
$ ./file_scratch
Executes
Does not execute
This text is redirected to file.txt.
This is also redirected.
实际产量:
$ ./file_scratch
Executes
其他信息:
open_stream()和read_lines()函数似乎相互干扰。我可以在注释open_stream()时运行read_lines()(前提是我保留了file.txt)。我的猜测是,我没有正确处理/关闭freopen(),可能stdout也受到了影响。我尝试了不同的方法来处理这件事,但没有效果。我错过了什么?我知道我可以简单地使用fopen()、fprintf()等。但是我在这里尝试的操作是否有一个正确的实现?如果对它所指向的文件调用了fclose(),则需要重新打开基于标准输出的标准输出。有人指出,最好使用fclose(stdout)关闭文件句柄(stdout)代码>而不是文件指针。然后用freopen(“/dev/tty”,“a”,stdout”)重新打开文件句柄
返回标准输出的正常功能。可以找到与此主题相关的更多有用信息
您关闭了输出文件,因此程序不再有可供printf
使用的stdout
。将来发布代码时,请删除行号。为什么希望“此文本重定向到file.txt?”要显示在终端中而不是文件中?您是否查看了file.txt
中的内容?freopen(“file.txt”,“w”,stdout)代码>将标准输出更改为指向文件,这样在该文件之后打印的所有内容都将在程序的整个剩余部分中转到该文件中,但是如果关闭该文件,则无法输出任何内容。
$ ./file_scratch
Executes
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
// prototyped my functions
void open_stream();
void read_lines();
open_stream(); // now runs as expected
read_lines(); // now runs as expected as well
return 0;
}
// this function runs as expected
void open_stream() {
printf("Executes\n");
FILE *fp;
fp = freopen("file.txt", "w", stdout);
printf("This text is redirected to file.txt.\n");
printf("This is also redirected.\n");
fclose(stdout); // using this instead of fclose(fp);
freopen("/dev/tty", "a", stdout); // reopening stdout after closing it
}
// this function now runs as expected
void read_lines()
{
printf("Executes this time\n"); // now it prints
FILE *fp;
int c;
fp = fopen("file.txt","r");
if(fp == NULL) {
perror("Error in opening file.");
}
while(1) {
c = fgetc(fp); // now it works
if( feof(fp) ) {
break ;
}
printf("%c", c); // gets printed now
}
fclose(fp);
}
Executes
Executes this time
This text is redirected to file.txt.
This is also redirected.