Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.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 fwrite函数,给出未知空间的数据_C - Fatal编程技术网

C fwrite函数,给出未知空间的数据

C fwrite函数,给出未知空间的数据,c,C,此函数用于获取一个参数作为文件的指针,并将所有文件放入结构分析图中,然后将其写入另一个文件。现在,每个数据之间都有很大的空间。charCompare运行良好,因为我制作了一个测试文件来测试它 #include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <string.h> #include <errno.h> #include <ctype.h> #

此函数用于获取一个参数作为文件的指针,并将所有文件放入结构分析图中,然后将其写入另一个文件。现在,每个数据之间都有很大的空间。charCompare运行良好,因为我制作了一个测试文件来测试它

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "anagrams.h"
#define SIZE 80

//struct
struct anagram {
char word[SIZE];
char sorted[SIZE];
};

void buildDB ( const char *const dbFilename ){

    FILE *dict, *anagramsFile;
    struct anagram a;

    //check if dict and anagram.data are open
    errno=0;
    dict= fopen(dbFilename, "r");

    if(errno!=0) {
        perror(dbFilename);
        exit(1);
    }

    errno=0;

    anagramsFile = fopen(anagramDB,"wb");

    char word[SIZE];
    char *pos;
    int i=0;

    while(fgets(word, SIZE, dict) !=NULL){

        //get ripe of the '\n'
        pos=strchr(word, '\n');
        *pos = '\0';

        //lowercase word
        int j=0;
        while (word[j]){
            tolower(word[j]);
            j++;
        }

        /* sort array using qsort functions */ 
        qsort(word,strlen(word), sizeof(char), charCompare);

        strncpy(a.sorted,word,sizeof(word));

        fwrite(&a,1,sizeof(struct word),anagramsFile);

        i++;
    }
    fclose(dict);
    fclose(anagramsFile);

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括“anagrams.h”
#定义大小80
//结构
结构字谜{
字符字[大小];
字符排序[大小];
};
void buildDB(const char*const dbFilename){
文件*dict,*anagramsFile;
结构重组图a;
//检查dict和anagram.data是否打开
errno=0;
dict=fopen(dbFilename,“r”);
如果(错误号!=0){
perror(dbFilename);
出口(1);
}
errno=0;
anagramsFile=fopen(anagramDB,“wb”);
字符字[大小];
char*pos;
int i=0;
while(fgets(字、大小、数字)!=NULL){
//熟透“\n”
pos=strchr(单词“\n”);
*pos='\0';
//小写字
int j=0;
while(单词[j]){
托洛尔(字[j]);
j++;
}
/*使用qsort函数对数组进行排序*/
qsort(单词、strlen(单词)、sizeof(字符)、charCompare(字符比较);
strncpy(a.sorted,word,sizeof(word));
fwrite(&a,1,sizeof(struct word),anagramsFile);
i++;
}
fclose(dict);
fclose(anagramsFile);
}
数据:
第10个第1个第2个

可能的原因是传递给的大小参数。从
qsort()
的链接参考页:

size-数组中每个元素的大小(字节)

因此,size参数应该是
1
,它保证是
sizeof(char)
,而不是
sizeof(char*)
,后者可能是
4
8
。发布的代码错误地通知
qsort()
word指向比实际数组大4倍(或
8倍)的数组,并且
qsort()
将访问不应该访问的内存。改为:

qsort(word,strlen(word), 1, charCompare);
另一个可能的原因是此行导致的缓冲区溢出:

strncpy(&a.sorted[i],word,sizeof(word));
i
的每次迭代中都会递增,而
循环中总是写入
sizeof(word)
SIZE
BUFSIZ
的值不会过帐,但即使它们相等,
strncpy()
在第一次迭代后写入的值也会超出
a.sorted
的范围

其他要点:

  • 不能保证读取新行字符,因此请在取消引用之前检查
    strhr()的返回值
  • 返回小写字符,但不更改其参数
  • 为什么要读入临时缓冲区(
    word
    )并进行复制?只需直接读入
    struct
    成员即可

您查看了
dict
的最大行长和
BUFSIZ
的值了吗?您确定segfault位于该行吗?我看到您使用的是strncpy(),它很少是正确的函数。看到我的文章了吗?你能发布
charCompare
函数吗?它仍然给我分段错误。我在它上面运行gdb,它在fgets()@keivn停止,尝试检查
dict!=NULL
而不是查询
errno
.strncpy(a.sorted,word,sizeof(word));我把它改成那个,它仍然给我一个分段错误