Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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
glibc双重免费或损坏(out),无需使用malloc_C_Glibc_Scanf - Fatal编程技术网

glibc双重免费或损坏(out),无需使用malloc

glibc双重免费或损坏(out),无需使用malloc,c,glibc,scanf,C,Glibc,Scanf,我一直在犯这个错误 glibc双重释放或损坏(输出)错误 到目前为止,我读到的所有内容都是由于不正确地使用了malloc,这将指向内存空间,但是,我没有使用malloc。我认为这是关于fscanf是如何接受流的。但是,我在fscanf上读到的任何东西都与我得到的错误无关。这是密码 #include <stdio.h> #include <fcntl.h> #include <sys.stat.h> #inlcud

我一直在犯这个错误 glibc双重释放或损坏(输出)错误 到目前为止,我读到的所有内容都是由于不正确地使用了malloc,这将指向内存空间,但是,我没有使用malloc。我认为这是关于fscanf是如何接受流的。但是,我在fscanf上读到的任何东西都与我得到的错误无关。这是密码

      #include <stdio.h>
      #include <fcntl.h>
      #include <sys.stat.h>
      #inlcude <termios.h>
      #inlcude <string.h>

      #define BAUDRATE B115200
      #define MODEMDEVICE "/dev/ttyS0"

      main()
      {
        int n;

        FILE *file

        file = fopen(MODEMDEVICE, "a");

        if(file == NULL){
                         printf("initiating error\n);
                         return 1;
                         }

           FILE *fp;
           fp = fopen("testfile.txt", "a");

                while(1){
                         fscanf(file, "%02x", &n);
                         fprintf(fp, "%d", n);
                         fclose(fp);
                         fclose(file);
                         }
        }
#包括
#包括
#包括
#内因库德
#内因库德
#定义波特率B115200
#定义MODEMDEVICE“/dev/ttyS0”
main()
{
int n;
文件*文件
file=fopen(MODEMDEVICE,“a”);
if(file==NULL){
printf(“启动错误”\n);
返回1;
}
文件*fp;
fp=fopen(“testfile.txt”,“a”);
而(1){
fscanf(文件“%02x”,&n);
fprintf(fp,“%d”,n);
fclose(fp);
fclose(文件);
}
}

我编写这段代码的目的是从串行端口获取数据流,并将其存储在文本文件中。您知道我为什么会出现此错误吗?

while(1)循环从一个文件读取数据并将其写入另一个文件,然后关闭这些文件。一旦关闭这些文件,您将无法再读取或写入它们,尽管您的
while
循环会再次尝试。

while(1)循环从一个文件读取数据并将其写入另一个文件,然后关闭这些文件。一旦关闭这些文件,您将无法再读取或写入它们,尽管您的
while
循环会重试。

此代码不会编译。它缺少一个“;“

它还以“a”模式打开了两个文件;但您肯定是想打开一个以供阅读


无论如何,错误在于您正在循环中调用fclose。

此代码无法编译。它缺少一个“;“

它还以“a”模式打开了两个文件;但您肯定是想打开一个以供阅读

无论如何,错误在于你在循环中调用fclose。

“我没有使用malloc”-是的,你(间接地)fopen使用它(并且
fclose
使用
free
),因此误用
fopen
可能(并且确实)表现为
malloc
失败。“我没有使用malloc”-是的,你(间接地)--
fopen
使用它(而
fclose
使用
free
),因此
fopen
的误用可能(并且确实)表现为
malloc
故障。