简体   繁体   English

将列表写入(txt)文件

[英]fwrite(-ing) a list into a txt.file

I made a function which turns the random boxes(/nodes/links) of a list into an array (consecutive-memory). 我提出其中把一个的随机盒(/节点/链路)的函数list array (连续存储器)。 All of this because I am trying to fwrite this list into a file(.txt) . 所有这一切,因为我想fwrite这个listfile(.txt)

But all that I see when I open the file is the words from the value field and then 39 spaces either blank or with a random character or whatever else. 但是,当我打开文件时,所看到的只是value字段中的单词,然后是39个空格(空白,随机字符或其他字符)。 Can someone tell me why this happens? 有人可以告诉我为什么会这样吗?

How can I get the result, either only the value field of each array element ( list link) or all 3 but by calling fwrite once? 如何仅通过每个数组元素( list链接)的value字段或全部为3的结果通过调用fwrite获得结果?

struct box {
    char value[20] ;
    struct box * next ; 
    int occurs;
} ;

typedef struct box Box;
typedef Box * List;


Box* list_toarray(List mylist){
    List iter=mylist;
    Box *packed;
    Box *dup;
    dup=packed=malloc((sizeof(Box))*lenlist(mylist));
    while(iter!=NULL){
        memcpy(packed,iter,sizeof(Box));
        packed++;//packed[1];
        iter=iter->next;
        if(iter!=NULL)
            (packed-1)->next=packed;
        else
            (packed-1)->next=NULL;
    }
    printf("\nInput list to array(Consecutive blocks of memory) :\n");
    report(dup);
    return dup;
}

void fread_fwrite_lsttoarray(Box *dup){
    FILE *myfile;
    myfile=fopen("fwfr.txt","wt");
    fwrite(dup,sizeof(Box),6,myfile);
    fclose(myfile);
}

Also this is how I call the function : 这也是我所谓的function

fread_fwrite_lsttoarray(list_toarray(newlistasc));

where newlistasc is a random list with struct box elements 其中newlistasc是带有struct box元素的随机list

in function fread_fwrite_lsttoarray() 在函数fread_fwrite_lsttoarray()中

change 更改

fwrite(dup,sizeof(Box),6,myfile);

to

fprintf(myfile, "%s, %d\n", dup->value, dup->occurs);

Do not write elememt next into file, because it's value is meanless outside memory. 不要将elememt next写入文件,因为它的值在内存之外毫无意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM