Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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,此函数的作用是获取一个参数作为文件指针,将所有文件放入结构分析图中,然后将其写入另一个文件。目前数据只包含 a、 这个词,但它也应该包含a吗 #include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <string.h> #include <errno.h> #include <ctype.h> #include "anagrams.h" vo

此函数的作用是获取一个参数作为文件指针,将所有文件放入结构分析图中,然后将其写入另一个文件。目前数据只包含 a、 这个词,但它也应该包含a吗

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


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';

        strncpy(a.word,word,sizeof(word));
        //lowercase word
        int j=0;
        while (word[j]) {
            tolower(word[j]);
            j++;
        }

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

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

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

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

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括“anagrams.h”
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';
strncpy(字,字,字的大小);
//小写字
int j=0;
while(单词[j]){
托洛尔(字[j]);
j++;
}
/*使用qsort函数对数组进行排序*/
qsort(单词,strlen(单词),1,charCompare);
strncpy(a.sorted,word,sizeof(word));
fwrite(&a,1,sizeof(word),anagramsFile);
i++;
}
fclose(dict);
fclose(anagramsFile);
}
数据:

sizeof(word)
返回缓冲区的完整大小,因此每次写入缓冲区的完整长度。您可能希望使用
strlen()
或类似工具来只写入实际使用的缓冲区部分

sizeof(word)
返回缓冲区的完整大小,因此每次写入缓冲区的完整长度。您可能希望使用
strlen()
或类似工具来只写入实际使用的缓冲区部分

char word[SIZE];
...
fwrite(&a,1,sizeof(word),anagramsFile);