我在 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/