C编程输出文本文件 [英] C Programming output text file

查看:89
本文介绍了C编程输出文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始编程,并且有一个初学者的问题: 我想更好地了解fprint()函数的工作原理,因为有时当我用它创建一个文本文件时,我意识到文件的类型多种多样(例如,只读,追加和写入).而且,当我想用​​循环创建的文件上写内容时,添加内容的顺序似乎会改变

Hi I just started programming and have a beginner question: I would like to better understand how the fprint()function work because sometimes when I create a text file with it, I realized there are various types of file eg.(read only, append and write). And when I want to write on the file I created with a loop, the order in which the content is added seem to change when I do

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

如果是

file = fopen(name,"w");

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

So what's the most convenient way to create a text file? Thank you!

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

So say I want to write all the words from a trie tree, the order in the text file would be different than just replacing fprint() with print() I have a global node for the tree and a node pointer pointing to it for other functions

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

函数是:

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];
};

最后是插入功能

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

推荐答案

给出输入文件:

elephant
rhinoceros
mouse

man0.txt中的输出为:

elephant 0 0
mouse 0 0
rhinoceros 0 0

这并不令人兴奋.每个单词都始于其自己的节点.

This isn't dreadfully exciting; each word starts at its own node.

类似地,输入:

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()不能接受任何参数.使用递归函数会使生活异常困难.所显示的代码将节点传递给函数-以及要写入的文件流.它使用"w"而不是"a+"打开文件.由于从未读取过文件,因此不需要+;使用"a"而不是"w"表示信息是从上一次运行追加到文件中的.

It seems that the task specified that printResults() could take no arguments. That makes life extraordinarily difficult with a recursive function. The code shown passes a node to the function — and also the file stream to write to. It uses "w" to open the file rather than "a+". Since the file was never read, the + was not needed; using "a" instead of "w" means that the information was appended to the file from the previous run.

全球人数过多;当我开始的时候还有更多. k仍不应是全局变量,但我尚未将其删除.

There are too many globals; there were more when I started. k should not still be a global, but I've not removed it yet.

这篇关于C编程输出文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆