我可以在二进制中打开的文件中使用fput和fgets吗

发布于 2025-02-04 13:49:54 字数 718 浏览 2 评论 0原文

文件中的数据是随机的,字母和数字。

/**** Program to APPEND one file after another file ****/

#include <stdio.h>
#include <fcntl.h>

int main()
{
    FILE *fp, *fc;
    char data[200];

    fp = fopen("students.DAT", "ab");

    if (fp == NULL)
    {
        printf("Can't open file");
        exit(1);
    }

    fc = fopen("pr1.DAT", "rb");

    if (fc == NULL)
    {
        printf("Can't open file");
        exit(2);
    }

    fseek(fp, 0, SEEK_END);

    while (fgets(data, 199, fc) != NULL)
    {
        fputs(data, fp);
    }
    fputs("\n", fp);

    return 0;
}

在此程序中使用fputsfgets是否使用? 如果不善待使用什么,为什么fgets/puts不对?

The data in file is random and both alphabet and numbers.

/**** Program to APPEND one file after another file ****/

#include <stdio.h>
#include <fcntl.h>

int main()
{
    FILE *fp, *fc;
    char data[200];

    fp = fopen("students.DAT", "ab");

    if (fp == NULL)
    {
        printf("Can't open file");
        exit(1);
    }

    fc = fopen("pr1.DAT", "rb");

    if (fc == NULL)
    {
        printf("Can't open file");
        exit(2);
    }

    fseek(fp, 0, SEEK_END);

    while (fgets(data, 199, fc) != NULL)
    {
        fputs(data, fp);
    }
    fputs("\n", fp);

    return 0;
}

Is the use of fputs and fgets in this program OK?
If not kindly tell the what to use and why fgets/puts is not ok?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佼人 2025-02-11 13:49:54

在二进制模式下,您应该使用fread而不是fgetsfwrite而不是fputs。不同之处在于,如果您使用fwrite函数,则应提供作为参数:

  1. 指向数组的指针(在您的情况下数据),
  2. 数组的每个元素的大小在字节中的每个元素中的
  3. 元素数量元素数量的数量元素
  4. 数量

In binary mode you should use fread instead of fgets and fwrite instead of fputs. The difference is if you use fwrite function you should provide as arguments:

  1. pointer to the array ( in your case data)
  2. size of each element of the array in bytes
  3. number of elements in the array
  4. pointer to FILE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文