å¨cè¯è¨ä¸ï¼å建åé¾è¡¨éè¦ä½¿ç¨å°mallocå½æ°å¨æç³è¯·å
åï¼æ件ç读åéè¦é¦å
使ç¨fopenå½æ°æå¼æ件ï¼ç¶å使ç¨fscanfï¼fgetc, fgetsï¼fprintfï¼fputcï¼fputsçå½æ°è¯»åå½æ°ï¼æå读åå®æ¯è¦ä½¿ç¨fcloseå½æ°å
³éå½æ°ã
ä¸é¢çæºç¨åºå±ç¤ºäºå
³äºåé¾è¡¨å¦ä½ä»æ件ä¸è¯»åæ°æ®åå¾æ件éåå
¥æ°æ®ã
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
typedef struct node {
int data;
struct node *next;
}node;
//ä»æ件ä¸è¯»åæ°æ®åå
¥é¾è¡¨
node *createlink()
{
node *head =(node*)malloc(sizeof(node));
int t;
node *p;
node *q;
p=q=head;
FILE * r= fopen("input.txt","r");
if(r==NULL)
{
printf("æå¼æ件失败!");
return NULL;
}
while(fscanf(r,"%d",&t)!=EOF)
{
q= (node*)malloc(sizeof(node));
q->data=t;
p->next=q;
p=q;
}
p->next=NULL;
return head;
}
//è¾åºé¾è¡¨å°å±å¹åæ件output.txt
void outlink(node *head)
{
node *p=head->next;
FILE *w =fopen("output.txt","w");
if(w==NULL)
{
printf("æå¼æ件失败!");
return;
}
while(p)
{
//è¾åºé¾è¡¨èç¹æ°æ®å°å±å¹
printf("%d ",p->data);
//è¾åºé¾è¡¨èç¹æ°æ®å°æ件output.txt
fprintf(w,"%d ",p->data);
p=p->next;
}
printf("\n");
fprintf(w,"\n");
fclose(w);
return;
}
int main()
{
node *head;
int n,m;
head=createlink();
outlink(head);
system("pause");
return 0;
}
温馨提示:答案为网友推荐,仅供参考