磁盘读写,合并排序字符串
Posted embeddedking
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了磁盘读写,合并排序字符串相关的知识,希望对你有一定的参考价值。
有两个磁盘文件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 ");
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 ");
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
, strlenB is %d
", strlen(strA), strlen(strB));
strcat(strA,strB);
char tmp;
printf("strlenA is %d
, strlenB is %d
", 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;
}
以上是关于磁盘读写,合并排序字符串的主要内容,如果未能解决你的问题,请参考以下文章