gpt4 book ai didi

C编程输出文本文件

转载 作者:太空宇宙 更新时间:2023-11-04 02:31:35 25 4
gpt4 key购买 nike

嗨,我刚开始编程,有一个新手问题:我想更好地了解 fprint() 函数的工作原理,因为有时当我用它创建文本文件时,我意识到有多种类型的文件,例如(只读、追加和写入)。当我想在我用循环创建的文件上写入时,添加内容的顺序似乎在我这样做时发生了变化

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

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

file = fopen(name,"w");

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

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

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

最佳答案

经过讨论 chat ,我们想出了:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

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

关于C编程输出文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42522720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com