c语言程序设计 将数据写入指定的txt文件
来源:百度知道 编辑:UC知道 时间:2024/05/30 17:25:36
二叉树的前序遍历
#include"stdio.h"
#include"stdlib.h"
#define maxsize 20
typedef int datatype;
typedef struct node
{
datatype data;
struct node *lchild,*rchild;
} bitree;
bitree *Q[maxsize];
bitree *CREATREE()
{
char ch;
int front,rear;
bitree *root,*s;
root=NULL;
front=1; rear=0;
ch=getchar();
while(ch!='#')
{
s=NULL;
if(ch!='@')
{
s=malloc(sizeof(bitree));
s->data=ch;
s->lchild=NULL;
s->rchild=NULL;
}
rear++;
Q[rear]=s;
if(rear==1) root=s;
else
{
if(s && Q[front])
if(rear%2==0) Q[front]->lchild=s;
else
Q[front]->rchild=s;
if(rear%2==1) front++;
}
ch=getchar();
}
return(root);
}
PREORDER(bitree *t)
{
FILE *fp;
fp=fopen("PREORDER.txt&q
#include"stdio.h"
#include"stdlib.h"
#define maxsize 20
typedef int datatype;
typedef struct node
{
datatype data;
struct node *lchild,*rchild;
} bitree;
bitree *Q[maxsize];
bitree *CREATREE()
{
char ch;
int front,rear;
bitree *root,*s;
root=NULL;
front=1; rear=0;
ch=getchar();
while(ch!='#')
{
s=NULL;
if(ch!='@')
{
s=malloc(sizeof(bitree));
s->data=ch;
s->lchild=NULL;
s->rchild=NULL;
}
rear++;
Q[rear]=s;
if(rear==1) root=s;
else
{
if(s && Q[front])
if(rear%2==0) Q[front]->lchild=s;
else
Q[front]->rchild=s;
if(rear%2==1) front++;
}
ch=getchar();
}
return(root);
}
PREORDER(bitree *t)
{
FILE *fp;
fp=fopen("PREORDER.txt&q
需要操作制定的文件,首先需要获取文件的文件描述符(句柄):fd = fopen("test.txt","w")
使用fprintf(),或者fputs()函数将数据格式化写入该文本
#include<stdio.h>
main()
{
FILE *f;
f=fopen("wenzhang.txt","w");
fprintf(f,"this is a c program !");
fclose(f);
}
fp=fopen("PREORDER.txt","w+");
改为 fp=fopen("PREORDER.txt","a+");