Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-在换行符中写入纯文本文件结构变量_C_File_Struct_Newline - Fatal编程技术网

C-在换行符中写入纯文本文件结构变量

C-在换行符中写入纯文本文件结构变量,c,file,struct,newline,C,File,Struct,Newline,我正在尝试将struct数据类型的变量写入一个文件(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_lo

我正在尝试将struct数据类型的变量写入一个文件(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;
}

我能做什么?谢谢大家!

这才是你真正需要的

#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;
}
#包括
#包括
int main()
{
类型定义结构
{
char-cp[1+1+4];
字符id[1+1+2];
字符相关[1+1+6];
字符数字[1+1+1];
}日志;
/*我为终止的空字节“\0”添加了“1”,为“\n”新行添加了另一个“1”*/
文件*fl;
记录新的日志;
fl=fopen(“配置数据”,“w”);
如果(fl==NULL)
返回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”,新的日志id);
printf(“相关:”);
fgets(新日志相关、sizeof(新日志相关)、标准DIN;
fprintf(fl,“%s”,新的日志相关);
printf(“数字:”);
fgets(新对数位数、sizeof(新对数位数)、标准DIN);
fprintf(fl,“%s”,新的对数位数);
fclose(fl);
返回0;
}

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

写入数据的方式是错误的,如果使用
fwrite()
,则在不插入任何换行的情况下将整个数据块写入文件。如果您想插入换行符,那么您应该使用类似于
fprintf()
的方法,或者在
fwrite()之间手动插入换行符,如果您的数据是

9001
00
128930
2
结构typedef应该是

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

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

我做了一些更改。卢卡是对的,但这只有在我使用
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;
}
#包括
#包括
int main()
{
类型定义结构
{
char-cp[5];
字符id[3];
字符相关[7];
字符数字[2];
}日志;
文件*fl;
记录新的日志;
如果(!(fl=fopen(“config.dat”,“w”))
{ 
printf(“打开文件时出错。\n”);
}
其他的
{
printf(“\nRoducir codigo postal(5个数字):”;
scanf(“%s”,new_log.cp);
printf(“行政管理介绍(2个数字):”;
scanf(“%s”,新的日志id);
printf(“相关数字导言(7个数字):”;
scanf(“%s”,新日志相关);
printf(“apuesta简介”);
scanf(“%s”,新的日志数字);
fprintf(fl,“%s%s%s”,new_log.cp,new_log.id,new_log.related,new_log.digit);
}
fclose(fl);
返回0;
}

您永远不会打开
fl
文件。请把它修好。我错删了那一行。你写文件的方式是错误的。以这种方式写入的数据不可能获得新行……永远不要使用
get()
。永远不要使用
get()
。它是如此致命以至于(最终)被从标准C中删除。第一个被利用的互联网蠕虫
get()
(谷歌搜索“莫里斯互联网蠕虫”)。从标准C或POSIX中使用,但请注意,它们包括结果中的换行符,而
gets()
没有。在某些系统上,您可能可以使用
get_s()
(Microsoft systems;也是C11附录K,但通常不可用)。谢谢您。我通过
scanf
更改了
get
,在这种情况下,这是我找到的第一个解决方案。也许从字面上理解这个问题你是对的,但是考虑到这个人正在学习c,\n在这种情况下并不相关,我的意思是\n既不是由gets输入的,也不应该存储在结构中。我同意null termintaion字节对于beginers来说是一个非常常见的问题。