C语言如何在递归函数中打印到文件而不覆盖它?
我有多个递归函数。他们将结果打印多次。我需要创建一个包含所有这些输出的文件。 打印的功能有:作废打印序、作废打印序、作废打印序 输入示例:1(案例选项预排序),1 2 3 4 5 q(插入到树中的数字,q结束扫描); 输出:1 2 3 4 5,我也需要将其打印到文件中 我的代码:C语言如何在递归函数中打印到文件而不覆盖它?,c,function,recursion,binary-tree,C,Function,Recursion,Binary Tree,我有多个递归函数。他们将结果打印多次。我需要创建一个包含所有这些输出的文件。 打印的功能有:作废打印序、作废打印序、作废打印序 输入示例:1(案例选项预排序),1 2 3 4 5 q(插入到树中的数字,q结束扫描); 输出:1 2 3 4 5,我也需要将其打印到文件中 我的代码: #include<stdlib.h> #include<stdio.h> #include<conio.h> #include<time.h> #include<p
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<pthread.h>
struct node1
{
int key1;
struct node1 *left1, *right1;
};
// A utility function to create a new BST node1
struct node1 *newnode1(int item)
{
struct node1 *temp1 = (struct node1 *)malloc(sizeof(struct node1));
temp1->key1 = item;
temp1->left1 = temp1->right1 = NULL;
return temp1;
}
// A utility function to do inorder traversal of BST
void inorder(struct node1 *root1)
{
if (root1 != NULL)
{
inorder(root1->left1);
printf("%d ", root1->key1);
inorder(root1->right1);
}
}
/* A utility function to insert1 a new node1 with given key1 in BST */
struct node1* insert1(struct node1* node1, int key1)
{
/* If the tree is empty, return a new node1 */
if (node1 == NULL) return newnode1(key1);
/* Otherwise, recur down the tree */
if (key1 < node1->key1)
node1->left1 = insert1(node1->left1, key1);
else
node1->right1 = insert1(node1->right1, key1);
/* return the (unchanged) node1 pointer */
return node1;
}
/* Given a non-empty binary search tree, return the node1 with minimum
key1 value found in that tree. Note that the entire tree does not
need to be searched. */
struct node1 * minValuenode1(struct node1* node1)
{
struct node1* current = node1;
/* loop down to find the left1most leaf */
while (current->left1 != NULL)
current = current->left1;
return current;
}
/* Given a binary search tree and a key1, this function deletes the key1
and returns the new root1 */
struct node1* deletenode1(struct node1* root1, int key1)
{
// base case
if (root1 == NULL) return root1;
// If the key1 to be deleted is smaller than the root1's key1,
// then it lies in left1 subtree
if (key1 < root1->key1)
root1->left1 = deletenode1(root1->left1, key1);
// If the key1 to be deleted is greater than the root1's key1,
// then it lies in right1 subtree
else if (key1 > root1->key1)
root1->right1 = deletenode1(root1->right1, key1);
// if key1 is same as root1's key1, then This is the node1
// to be deleted
else
{
// node1 with only one child or no child
if (root1->left1 == NULL)
{
struct node1 *temp1 = root1->right1;
free(root1);
return temp1;
}
else if (root1->right1 == NULL)
{
struct node1 *temp1 = root1->left1;
free(root1);
return temp1;
}
// node1 with two children: Get the inorder successor (smallest
// in the right1 subtree)
struct node1* temp1 = minValuenode1(root1->right1);
// Copy the inorder successor's content to this node1
root1->key1 = temp1->key1;
// Delete the inorder successor
root1->right1 = deletenode1(root1->right1, temp1->key1);
}
return root1;
}
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
void print_preorder(node * tree)
{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(node * tree)
{
if (tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right); // i want to print this as a tree
}
}
void print_postorder(node * tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
}
if(val < (*tree)->data)
{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}
void main()
{
int a;
int searcharg;
int deletearg;
char option;
printf("1:PreOrder\n2:InOrder\n3:PostOrder\n4:search\n5:Delete Node\nYour Option: ");
scanf("%c", &option);
clock_t tic = clock();
node *root;
node *tmp;
//int i;
root = NULL;
struct node1 *root1 = NULL;
/* Inserting nodes into tree */
while (scanf("%d", &a) && a!='q'){insert(&root,a); root1 = insert1(root1, a);}
// these
/* Printing nodes of tree */
switch(option)
{
case '1':
printf("Pre Order Display\n");
print_preorder(root); // i want to print this as a tree
break;
case '2':
printf("In Order Display\n");
print_inorder(root); // i want to print this as a tree
break;
case '3':
printf("Post Order Display\n");
print_postorder(root); // i want to print this as a tree
break;
case '4':
scanf("%d", &searcharg);
tmp = search(&root, searcharg);
if (tmp)
{
printf("The searched node is present = %d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}
break;
default:
printf("Error! operator is not correct\n");
break;
case '5':
// printf("scan");
// scanf("%d", &deletearg);
root1 = deletenode1(root1, 5);
inorder(root1);
}
// i want to print this as a tree
/* Deleting all nodes of tree */
deltree(root);
clock_t toc = clock();
printf("\nElapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
getch();
}
#包括
#包括
#包括
#包括
#包括
结构节点1
{
int键1;
结构节点1*left1,*right1;
};
//用于创建新BST节点1的实用程序函数
结构节点1*newnode1(int项)
{
结构节点1*temp1=(结构节点1*)malloc(sizeof(结构节点1));
temp1->key1=项目;
temp1->left1=temp1->right1=NULL;
返回temp1;
}
//用于按顺序遍历BST的实用函数
void顺序(结构节点1*root1)
{
if(root1!=NULL)
{
顺序(root1->left1);
printf(“%d”,root1->key1);
顺序(root1->right1);
}
}
/*在BST中插入具有给定键1的新节点1的实用函数*/
结构节点1*insert1(结构节点1*node1,int键1)
{
/*如果树为空,则返回新节点1*/
if(node1==NULL)返回newnode1(key1);
/*否则,将从树上再次出现*/
如果(键1键1)
node1->left1=insert1(node1->left1,键1);
其他的
node1->right1=insert1(node1->right1,键1);
/*返回(未更改的)节点1指针*/
返回节点1;
}
/*给定一个非空的二叉搜索树,以最小值返回node1
在该树中找到key1值。请注意,整棵树没有
需要搜索*/
结构节点1*minValuenode1(结构节点1*node1)
{
结构节点1*当前=节点1;
/*向下循环以查找最左边的1叶*/
while(当前->左1!=NULL)
当前=当前->左1;
回流;
}
/*给定一个二叉搜索树和一个key1,此函数将删除key1
并返回新的root1*/
结构节点1*deletenode1(结构节点1*root1,int-key1)
{
//基本情况
if(root1==NULL)返回root1;
//如果要删除的key1小于root1的key1,
//然后它位于left1子树中
如果(键1键1)
root1->left1=deletenode1(root1->left1,键1);
//如果要删除的key1大于root1的key1,
//然后它位于右1子树中
else if(key1>root1->key1)
root1->right1=deletenode1(root1->right1,键1);
//如果key1与root1的key1相同,那么这就是节点1
//删除
其他的
{
//节点1只有一个子节点或没有子节点
如果(root1->left1==NULL)
{
结构节点1*temp1=root1->right1;
免费(root1);
返回temp1;
}
else if(root1->right1==NULL)
{
结构节点1*temp1=root1->left1;
免费(root1);
返回temp1;
}
//有两个子节点的节点1:获取有序的后继节点(最小的)
//在右侧1子树中)
结构节点1*temp1=minValuenode1(root1->right1);
//将顺序继承者的内容复制到此节点1
root1->key1=temp1->key1;
//删除顺序继承项
root1->right1=deletenode1(root1->right1,temp1->key1);
}
返回根1;
}
结构bin_树{
int数据;
结构bin_树*右,*左;
};
typedef结构bin_树节点;
无效插入(节点**树,int val)
{
节点*temp=NULL;
如果(!(*树))
{
temp=(node*)malloc(sizeof(node));
临时->左=临时->右=空;
温度->数据=val;
*树=温度;
返回;
}
if(val<(*树)->数据)
{
插入(&(*树)->左,val);
}
else if(val>(*树)->数据)
{
插入(&(*树)->右侧,val);
}
}
无效打印\u预订单(节点*树)
{
如果(树)
{
printf(“%d\n”,树->数据);
打印预订单(树->左);
打印预订单(树->右);
}
}
无效打印顺序(节点*树)
{
如果(树)
{
按顺序打印(树->左);
printf(“%d\n”,树->数据);
按顺序打印(树->右);//我想把它打印成树
}
}
作废打印\u后订单(节点*树)
{
如果(树)
{
打印邮政订单(树->左);
打印邮政订单(树->右);
printf(“%d\n”,树->数据);
}
}
void deltree(节点*树)
{
如果(树)
{
deltree(树->左);
deltree(树->右侧);
自由(树);
}
}
节点*搜索(节点**树,int val)
{
如果(!(*树))
{
返回NULL;
}
if(val<(*树)->数据)
{
搜索(&((*树)->左),val;
}
else if(val>(*树)->数据)
{
搜索(&((*树)->右),val;
}
else if(val==(*树)->数据)
{
返回*树;
}
}
void main()
{
INTA;
int searcharg;
int-deletearg;
字符选项;
printf(“1:PreOrder\n2:InOrder\n3:PostOrder\n4:search\n5:Delete Node\n您的选项:”);
scanf(“%c”和选项(&O);
时钟=时钟();
节点*根;
节点*tmp;
//int i;
root=NULL;
结构节点1*root1=NULL;
/*将节点插入到树中*/
而(scanf(“%d”,“a”)和&a!='q'){insert(&root,a);root1=insert1(root1,a);}
//这些
/*打印树的节点*/
开关(选件)
{
案例“1”:
printf(“预订单显示”);
print_preorder(root);//我想将其打印为树
打破
案例“2”:
printf(“顺序显示\n”);
print_inoder(root);//我想把它打印成一棵树
打破
案例“3”:
printf(“订单后显示”);
print_postorder(root);//我想把它打印成一棵树
打破
案例“4”:
scanf(“%d”、&searcharg);
tmp=搜索(&root,searcharg);
如果(tmp)
{
printf(“搜索到的节点存在=%d\n”,tmp->d
./a.out >> your_filename
FILE * g_my_file = fopen("my_file.txt", "wt");
fprintf(g_my_file, "%d\n", tree->data);
fclose(g_my_file);
fp = fopen("file.txt","a");
//write dome data here this will be appended to the end of the file
fclose(fp);