我用jdoodle编写了这个代码,屏幕是空白的。我怎样才能打开文本?它去哪儿了?
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;
}