Bài 1.6Viết chương trình cho phép người dùng tạo tệp “Baitho.txt” lưu nội dung một bài thơ có n dòng được nhập vào từ bàn phím, sau đó đọc lại tệp văn bản vừa tạo và hiển thị nội dung lên màn hình.


//Câu 1.6:
#include <stdio.h>
#include <stdio_ext.h>

#define SIZE 256

int main(void)
{
FILE *fp;
char s[SIZE];
int n, i;

//Tao tep baitho.txt
fp = fopen("Baitho.txt","w");
if(fp==NULL)
perror("Khong tao duoc file Baitho.txt\n");
else
{
printf("Nhap vao so dong cua bai tho: ");
scanf("%d", &n);

//Nhap vao bai tho va ghi vao tep
for(i=1;i<=n;i++)
{
printf("Nhap vao dong thu %d: ", i);
__fpurge(stdin);
gets(s);
if(i>1) fputc(10,fp);//xuong dong
//ghi dong da nhap vao tep
fputs(s,fp);
}
fclose(fp);
}
//doc tep bai tho
fp = fopen("Baitho.txt","r");
if(fp==NULL)
perror("Khong doc duoc file Baitho.txt!\n");
else
{
printf("\n\nBai tho da nhap la:\n");
while(!feof(fp))
{
fgets(s,SIZE,fp);
printf("%s", s);
}
fclose(fp);
}

printf("\n\n");
return 0;
}
//===dinh nghia ham===
Coding: Hải Dớ