【问题标题】:C - Write in plain text file struct variables in newlineC - 在换行符中写入纯文本文件结构变量
【发布时间】:2014-12-13 17:00:27
【问题描述】:

我正在尝试将结构数据类型的变量写入由换行符分隔的文件(config.dat)中,即:

9001
00
128930
2

但我只能写不带空格的:

9001001289302

这是代码:

int main()
{
  typedef struct
  {
    char cp[4];
    char id[2];
    char correlative[6];
    char digit[1];
  } log;

  FILE *fl;
  log new_log;

  fl = fopen("config.dat", "w");

  printf ("\nCP: ");
  gets (new_log.cp);
  printf ("ID: ");
  gets (new_log.id);
  printf ("Correlative: ");
  gets (new_log.correlative);
  printf ("Digit");
  gets (new_log.digit);

  fwrite(&new_log, sizeof(log), 1, fl);

  fclose(fl);

  return 0;
}

我能做什么?谢谢!

【问题讨论】:

  • 你永远不会打开 fl 文件。请解决这个问题。
  • 已修复。我误删了那行。
  • 你写文件的方式不对。这样写的数据不可能换行了……
  • 永远不要使用gets()。永远不要使用gets()。它是如此致命,它(最终)已从标准 C 中删除。第一个利用 gets() 的互联网蠕虫(谷歌搜索“莫里斯互联网蠕虫”)。使用标准 C 中的 fgets() 或 POSIX 中的 getline(),但请注意这些在结果中包含换行符,而 gets() 没有。在某些系统上,您可能可以使用gets_s()(Microsoft 系统;还有 C11 Annex K,但通常不可用。)
  • 谢谢乔纳森。我已将gets 更改为scanf,在这种情况下是我找到的第一个解决方案。

标签: c file struct newline


【解决方案1】:

这是你真正需要的。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    typedef struct
    {
        char cp[1 + 1 + 4];
        char id[1 + 1 + 2];
        char correlative[1 + 1 + 6];
        char digit[1 + 1 + 1];
    } log;
    /* I add `1' for the terminating null byte '\0', and another `1' for the `\n` new line */

    FILE *fl;
    log   new_log;

    fl = fopen("config.dat", "w");
    if (fl == NULL)
        return 1;

    printf ("\nCP: ");
    fgets (new_log.cp, sizeof(new_log.cp), stdin);
    fprintf (fl, "%s", new_log.cp);

    printf ("ID: ");
    fgets (new_log.id, sizeof(new_log.id), stdin);
    fprintf (fl, "%s", new_log.id);

    printf ("Correlative: ");
    fgets (new_log.correlative, sizeof(new_log.correlative), stdin);
    fprintf (fl, "%s", new_log.correlative);

    printf ("Digit: ");
    fgets (new_log.digit, sizeof(new_log.digit), stdin);
    fprintf (fl, "%s", new_log.digit);


    fclose(fl);

    return 0;
}

请注意,我已使用 fgets 来避免缓冲区溢出。

【讨论】:

    【解决方案2】:

    您写入数据的方式是错误的,如果您使用fwrite(),您将完整的数据块写入文件而不插入任何换行符。如果你想插入换行符,那么你应该使用fprintf()之类的东西,或者在fwrite()之间手动插入换行符

    【讨论】:

      【解决方案3】:

      如果你的数据是

      9001
      00
      128930
      2
      

      结构体类型定义应该是

      typedef struct
        {
          char cp[5];
          char id[3];
          char correlative[7];
          char digit[2];
        } log;
      

      例如 9001 将作为 9001\0 存储在 cp 成员中,零字符将在 id 成员上溢出,依此类推,这就是为什么你会得到 9001001289302...

      【讨论】:

      • 也许从字面上看你是对的,但鉴于这个人正在学习 c , \n 在这种情况下不相关,我的意思是 \n 既不是由 gets 输入的,也不应该存储在结构
      • 我同意空终止字节对于初学者来说是一个非常常见的问题。
      【解决方案4】:

      我做了一些改变。 Luca 是对的,但这只有在我使用 fprintf 而不是 fwrite 时才有效:

      #include <stdio.h>
      #include <stdlib.h>
      
      int main()
      {
        typedef struct 
        {
          char cp[5];
          char id[3];
          char correlative[7];
          char digit[2];
        } log;
      
        FILE *fl;
        log new_log;
      
        if (!(fl = fopen("config.dat", "w")))
        { 
          printf ("An error occurred while opening file.\n");
        }
        else
        {
          printf ("\nIntroducir codigo postal (5 digitos): ");
          scanf ("%s", new_log.cp);
          printf ("Introducir identificador de la administracion (2 digitos): ");
          scanf ("%s", new_log.id);
          printf ("Introducir numero correlativo (7 digitos): ");
          scanf ("%s", new_log.correlative);
          printf ("Introducir precio de la apuesta: ");
          scanf ("%s", new_log.digit);
      
          fprintf(fl, "%s %s %s %s", new_log.cp, new_log.id, new_log.correlative, new_log.digit);
        }
      
        fclose(fl);
      
        return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-04
        • 1970-01-01
        • 2014-02-26
        • 1970-01-01
        相关资源
        最近更新 更多