C编程输出文本文件

嗨,我刚开始编程并有一个初学者问题:我想更好地了解fprint()函数是如何工作的,因为有时当我用它创建文本文件时,我意识到有各种类型的文件,例如。(只读,附加)和写)。 当我想写一个我用循环创建的文件时,添加内容的顺序似乎在我做的时候会改变

file = fopen(name,"a+"); 

如果是的话,我无法在循环中添加所有内容

 file = fopen(name,"w"); 

那么创建文本文件最方便的方法是什么? 谢谢!

所以说我想写一个trie树中的所有单词,文本文件中的顺序不同于用print()替换fprint()我有一个树的全局节点和一个指向它的节点指针function

 struct node *root = (struct node *)malloc(sizeof(struct node)); 

function是:

 void printResult(struct node* r){ struct node *p = r; FILE *file; sprintf(name, "man%d.txt", num); file = fopen(name,"a+"); int i=0; int temp; while(i!=26){ if(p->child[i]==NULL){ i++; continue;} if(p->child[i]->isword==1&&p->child[i]->leaf==1){ word[k]=i+'a'; word[k+1]='\0'; fprintf(file,"%s", word);fprintf(file,"%s"," " ); fprintf(file,"%d", p->child[i]->occurrence);fprintf(file,"%s"," " ); fprintf(file,"%d\n", p->child[i]->super); i++; continue;} if(p->child[i]->isword==0){ word[k]=i+'a'; temp=k; k++; p=p->child[i]; printResult(p); k=temp; p=p->parent; } if(p->child[i]->isword==1&&p->child[i]->leaf==0){ word[k]=i+'a'; word[k+1]='\0'; temp=k; k++; p->child[i]->isword=0; fprintf(file,"%s", word);fprintf(file,"%s"," " ); fprintf(file,"%d", p->child[i]->occurrence);fprintf(file,"%s"," " ); fprintf(file,"%d\n", p->child[i]->super); p=p->child[i]; printResult(p); k=temp; p=p->parent; } i++; }fclose(file); } 

和节点:

 struct node{ struct node * parent; int noempty; int isword; int super; int occurrence; int leaf; struct node * child[26]; }; 

最后是插入function

 struct node* insert(struct node *root,char *c){ int i=0; struct node *temp=root; int l=length(c); while(i!=l){ int index=c[i]-'a'; if(temp->child[index]==NULL){ //New Node struct node *n=(struct node *)malloc(sizeof(struct node)); n->parent=temp; temp->child[index]=n; temp->noempty=1;} //Node Exist if(i!=l&&temp->leaf==1){temp->leaf=0;} temp=temp->child[index]; i++;} if(temp->noempty==0){ temp->leaf=1;} temp->isword=1; return root; }; 

在聊天讨论后,我们想出了:

 #include  #include  #include  #define length(x) strlen(x) struct node { struct node *parent; int noempty; int isword; int super; int occurrence; int leaf; struct node *child[26]; }; static struct node *root = 0; static char word[1024]; static int k = 0; static void printResult(FILE * file, struct node *r) { struct node *p = r; int i = 0; int temp; while (i != 26) { if (p->child[i] == NULL) { i++; continue; } if (p->child[i]->isword == 1 && p->child[i]->leaf == 1) { word[k] = i + 'a'; word[k + 1] = '\0'; fprintf(file, "%s", word); fprintf(file, "%s", " "); fprintf(file, "%d", p->child[i]->occurrence); fprintf(file, "%s", " "); fprintf(file, "%d\n", p->child[i]->super); i++; continue; } if (p->child[i]->isword == 0) { word[k] = i + 'a'; temp = k; k++; p = p->child[i]; printResult(file, p); k = temp; p = p->parent; } if (p->child[i]->isword == 1 && p->child[i]->leaf == 0) { word[k] = i + 'a'; word[k + 1] = '\0'; temp = k; k++; p->child[i]->isword = 0; fprintf(file, "%s", word); fprintf(file, "%s", " "); fprintf(file, "%d", p->child[i]->occurrence); fprintf(file, "%s", " "); fprintf(file, "%d\n", p->child[i]->super); p = p->child[i]; printResult(file, p); k = temp; p = p->parent; } i++; } } static struct node *insert(struct node *root, char *c) { int i = 0; struct node *temp = root; int l = length(c); while (i != l) { int index = c[i] - 'a'; if (temp->child[index] == NULL) { // New Node struct node *n = (struct node *)malloc(sizeof(struct node)); n->parent = temp; temp->child[index] = n; temp->noempty = 1; } // Node Exist if (i != l && temp->leaf == 1) { temp->leaf = 0; } temp = temp->child[index]; i++; } if (temp->noempty == 0) { temp->leaf = 1; } temp->isword = 1; return root; } int main(void) { root = (struct node *)malloc(sizeof(struct node)); memset(root, '\0', sizeof(*root)); char line[1024]; while (fgets(line, sizeof(line), stdin) != 0) { line[strcspn(line, "\n")] = '\0'; printf("[%s]\n", line); root = insert(root, line); } FILE *file; char name[1024]; int num = 0; sprintf(name, "man%d.txt", num); file = fopen(name, "w"); printResult(file, root); fclose(file); return 0; } 

给定输入文件:

 elephant rhinoceros mouse 

man0.txt中的输出是:

 elephant 0 0 mouse 0 0 rhinoceros 0 0 

这并不令人兴奋; 每个单词都从它自己的节点开始。

同样,给出输入:

 boo book booking john tex text 

输出是:

 boo 0 0 book 0 0 booking 0 0 john 0 0 tex 0 0 text 0 0 

似乎任务指定printResults()不能参数。 这使得递归函数的生活exception困难。 显示的代码将节点传递给函数 – 以及要写入的文件流。 它使用"w"打开文件而不是"a+" 。 由于文件从未被读过,所以不需要+ ; 使用"a"而不是"w"表示信息已附加到上一次运行的文件中。

有太多的全局变量; 我开始的时候还有更多。 k不应该仍然是全局的,但我还没有删除它。

什么是创建文本文件最方便的方法?

我想写一个trie树中的所有单词,文本文件中的顺序不同于用print()替换fprint()我有一个树的全局节点和一个指向它的节点指针用于其他函数

为了最方便地将trie写入磁盘,如果你可以将整个 trie称为数组,那么它将大有帮助。 即不要只有一个导致单独分配的root ; 把一切都引用回同一个arrays! 然后就像将节点从数组直接写入磁盘一样简单,只要您不需要为其他实现转换整数,或者为其他实现转换整数,否则……您无需担心差异在a+w之间。

使用单个分配还有其他好处:

  • 一个分配意味着一个免费,这意味着你的代码将更快。 (实际上,不需要分配,如下所示)
  • 缓存一致性; 单个分配可能更好地作为一个条目而不是多个分配进行缓存。 这将以便携方式减少高速缓存未命中,并且因此需要较少的非便携式手动优化。 同样,您的代码会更快。
  • 有多少C标准函数实际在幕后分配内存? 没有,除了内存分配function,当然…… 作为这些function的用户,这对您有何益处? 如果你考虑一下, 你可以编写你的代码malloc -free ,这样调用者决定使用什么类型的分配(无论是自动存储持续时间,静态存储持续时间, malloc / realloc ),类似于scanfstrcatsprintf等。让你选择……这样可以更容易测试,例如你可以检查我的PATRICIA trie测试代码和我的PATRICIA库代码 ,看看我不需要使用mallocfree进行测试,这使得测试看起来更加清晰。 ..更不用说,有些用例, mallocfree都不是最好的选择!

最后一点特别有用,因为它允许调用者将数据作为扩展存储在trie节点中,而不是像你一样在外部指向数据。 这使得编写文件变得更加容易,因为如果所有信息都在那里,您可以在技术上将整个数组一次性地转储到文件中。 深思熟虑:你认为使用RAM作为缓存级别的基于磁盘的trie可能是可行的吗?