标签:put std pen for string 文件中 failed har 磁盘读写
有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件C中。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
int main(void)
{
FILE *fp;
char strA[100], strB[100];
if((fp = fopen("a.txt","w")) == NULL)
{
printf("error, create a.txt failed");
exit(0);
}
fprintf(fp,"qwertyuiop\0");
fclose(fp);
if((fp = fopen("a.txt","r")) == NULL)
{
printf("error, read a.txt failed");
exit(0);
}
fgets(strA, 100, fp);
fclose(fp);
if((fp = fopen("b.txt","w")) == NULL)
{
printf("error, create b.txt failed");
exit(0);
}
fprintf(fp,"asdfghj\0");
fclose(fp);
if((fp = fopen("b.txt","r")) == NULL)
{
printf("error, read b.txt failed");
exit(0);
}
fgets(strB, 100, fp);
fclose(fp);
printf("strlenA is %d\n, strlenB is %d\n", strlen(strA), strlen(strB));
strcat(strA,strB);
char tmp;
printf("strlenA is %d\n, strlenB is %d\n", strlen(strA), strlen(strB));
for(int i=(strlen(strA)-1); i>0; i--)
{
for(int j=0; j<i; j++)
{
if(strA[j] > strA[i])
{
tmp = strA[i];
strA[i] = strA[j];
strA[j] = tmp;
}
}
}
if((fp = fopen("c.txt","w")) == NULL)
{
printf("error, create c.txt failed");
exit(0);
}
fputs(strA, fp);
fclose(fp);
return 0;
}
标签:put std pen for string 文件中 failed har 磁盘读写
原文地址:https://www.cnblogs.com/embeddedking/p/9694040.html