ตัวอย่างการใช้สิทธิ 99 C
ชื่อเรื่อง: มีสองดิสก์ไฟล์ A และ B แต่ละร้านของเขาตัวอักษรขอข้อมูลจากทั้งสองเอกสารร่วมกัน (ในลำดับตัวอักษร) ส่งออกไปยังไฟล์ใหม่ใน C.
การวิเคราะห์โครงการ: คุณจำเป็นต้องสร้าง a.txt และ b.txt
เนื้อหา a.txt ของไฟล์:
123
b.txt เนื้อหาของแฟ้ม:
456
รหัสที่มา:
// Created by www.w3big.com on 15/11/9. // Copyright © 2015年 本教程. All rights reserved. // #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { FILE*fa,*fb,*fc; int i,j,k; char str[100],str1[100]; char tem; if((fa=fopen("A.txt","r"))==NULL) // A.txt 文件需要存在 { printf("error: cannot open A file!\n"); exit(0); } fgets(str,99,fa); fclose(fa); if((fb=fopen("B.txt","r"))==NULL) // B.txt 文件需要存在 { printf("error: cannot open B file!\n"); exit(0); } fgets(str1,100,fb); fclose(fb); strcat(str,str1); for(i=strlen(str)-1;i>1;i--) for(j=0;j<i;j++) if(str[j]>str[j+1]) { tem=str[j]; str[j]=str[j+1]; str[j+1]=tem; } if((fc=fopen("C.txt","w"))==NULL) // 合并为 C.txt { printf("error: cannot open C file!\n"); exit(0); } fputs(str,fc); fclose(fc); system("pause"); return 0; }
หลังจากทำงานเอาท์พุทตัวอย่างข้างต้นเปิด c.txt อ่านดังนี้
123456