gpt4 book ai didi

c - 您好,我正在使用 JDoodle 练习 C 编程,我创建了一个指向 .text 文件的 fpointer,如何打开这个 .text 文件并查看数据?

转载 作者:太空宇宙 更新时间:2023-11-04 06:46:18 27 4
gpt4 key购买 nike

我在 jdoodle 中写了这段代码,屏幕是空白的。如何打开 .text?它去哪儿了?

int main () {

FILE * fpointer = fopen ("inventory.txt", "w");

fprintf (fpointer,
"A432LIPG, Lipgloss\n A442LIPG, Lipgloss \n C465LIPG, Lipgloss");

fclose (fpointer);

return 0;
}

最佳答案

如果我没理解错的话你需要这样的东西

#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(void)
{
FILE * fpointer = fopen ( "inventory.txt", "w" );

if ( fpointer != NULL )
{
fprintf ( fpointer, "A432LIPG, Lipgloss\n A442LIPG, Lipgloss \n C465LIPG, Lipgloss" );

fclose( fpointer );
}
else
{
puts( strerror( errno ) );
}

fpointer = fopen ( "inventory.txt", "r" );

if ( fpointer != NULL )
{
enum { N = 100 };
char s[N];

while ( fgets( s, sizeof( s ), fpointer ) != NULL )
{
s[strcspn( s, "\n" )] = '\0';

puts( s );
}

fclose( fpointer );
}
else
{
puts( strerror( errno ) );
}

return 0;
}

关于c - 您好,我正在使用 JDoodle 练习 C 编程,我创建了一个指向 .text 文件的 fpointer,如何打开这个 .text 文件并查看数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57591797/

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