Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

bai tap c nhap du lieu vao file

// XuLyFile.cpp : Defines the entry point for the console application.

// LH: 01698316659

#include "stdafx.h"

#include "stdio.h"

#include "stdlib.h"

typedef struct Link{

int data;

Link*next;

}Link;

Link*pHead=NULL;

FILE*pfile;

char*ptenfile="Data.TxT";//Dinh nghia con tro ten file

void them_dau_link(int dau);

void them_cuoi_link(int cuoi);

void ghi_file();

void doc_file();

void nhap();

{

int chon=0;

printf("Nhap 1 de tao moi danh sach.

Nhap 2 de them phan tu va'o danh sa'ch cu~.

Nhap bat ky neu muon ket thuc.");

printf("

? ");

scanf( "%d",&chon);

switch(chon)

{

case 1:

pfile=fopen(ptenfile,"w");//Tao moi file

fclose(pfile);

nhap();

ghi_file();

break;

case 2:

doc_file();

nhap();

pfile=fopen(ptenfile,"w");//Tao moi file

fclose(pfile);

ghi_file();

}

}//Ket thuc ham chinh

void them_dau_link(int dau)//Bat dau ham them du lieu vao dau link

{

Link*moi; /* Con tro toi phan tu moi */

if((moi =(Link*)malloc(sizeof(Link))) != NULL)

{

moi->data=dau;

moi->next=pHead;

pHead=moi;

}

else

printf(" %d khong the them. Bo nho day!

");//Ket thuc them vao dau

}//Ket thuc ham them_dau_link

void them_cuoi_link(int cuoi) //Bat dau ham them cuoi

{

Link*moi;

Link*truoc;

Link*timCuoi;

if((moi=(Link*)malloc(sizeof(Link)))!=NULL)

{

moi->data=cuoi;

moi->next=truoc=NULL;

timCuoi=pHead;

while(timCuoi!=NULL)

{

truoc=timCuoi;

timCuoi=timCuoi->next;

}

if(truoc==NULL)

pHead=moi;

else

truoc->next=moi;

}

else

printf("Bo nho day!Khong the them!!!

");

} //Ket thuc ham them cuoi

void ghi_file()//Bat dau ham ghi data vao file

{

Link*batdau=pHead;

while(batdau!=NULL)

{

if((pfile=fopen(ptenfile,"a"))!=NULL)

fprintf(pfile,"%d

",batdau->data);

else

{

printf("Loi! Khong ghi duoc du lieu vao file

");

return;

}

batdau=batdau->next;

}

fclose(pfile);

}//Ket thuc ham ghi data vao file

void doc_file()//Bat dau ham doc data tu file vao link

{

int value;

if((pfile=fopen(ptenfile,"r"))!=NULL)

{

fscanf(pfile,"%d",&value);

while(!feof(pfile))

{

them_cuoi_link(value);

fscanf(pfile,"%d",&value);

}

fclose(pfile);

}

else

{

printf("Khong mo duoc tep hoac tep khong ton tai!

");//?????????????????

return;

}

} // Ket thuc ham doc

void nhap()//Bat dau ham nhap du lieu de them vao link

{

char dk='y';

while(dk=='y'||dk=='Y')

{

printf("Nhap gia tri can them: ");

fflush(stdin);

scanf("%d",&temp);

them_dau_link(temp);

do

{

printf("Ban muon them tiep khong(Y/N)?");

fflush(stdin);//Xoa bo nho nhap truoc do

scanf("%c",&dk);

}

while(dk!='y'&&dk!='Y'&&dk!='n'&&dk!='N');

}

} //Ket thuc ham nhap

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: #lạp#trinh