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

标签 c file fopen fgets

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

相关文章:

c - 如何在 C 中读取或捕获 Ctrl+某个键或 Alt+某个键?

java - OpenGL 灯光示例无法在 LWJGL 中使用等效代码

php - SQL 结果作为 txt.file 由 php

c - 为什么尝试在 main 中调用 void 函数时类型不兼容

c - 线程内递归

arrays - 通过 HTTP 将文件下载到 C# 中的字节数组中?

java - 如何在 android 中备份电池插头上的数据?

java - Java 中打开的文件太多

c - fopen 在 Linux 中失败,但在 Windows 中工作

c - NULL 不初始化