创建a1,a2,a3三个文件,向a1,a2中各输入五个字符,再将两组字符串合并并按ascll表排序后输入到a3,但在排序时发生访问冲突,不知道是数组越界还是指针出了问题,求指点!
#include <stdio.h>
#include <string.h>
int main()
{
FILE *p1,*p2,*p3;
int i,j;
char b[2][10],*c;char p[10],(*c1)[10],m;c1=&p;c=&m;
printf("file A:\n");
gets(b[0]);
printf("file B:\n");
gets(b[1]);
p1=fopen("a1","w");
fputs(b[0],p1);
fclose(p1);
p2=fopen("a2","w");
fputs(b[1],p2);
fclose(p2);
strcpy(p,b[0]);
strcat(p,b[1]);
//puts(c1);
for(i=0;i<10;i++)
for(j=i;i<n;j++)
{
if(p[j]<p[i]||p[j]==p[i])
{
*c=*(*c1+j);
*(*c1+j)=*(*c1+i);
*(*c1+i)=*c;
}
}
printf("file C:\n");
puts(p);
putchar('\n');
p3=fopen("a3","w");
fputs(p,p3);
fclose(p3);
}
#include <stdio.h>
#include <string.h>
int main()
{
FILE* p1, * p2, * p3;
int i, j=0;
char b[2][10], * c; char p[11] = {0}, (*c1)[11], m; c1 = &p; c = &m; //p要申请11个空间,存放10个数据后应该为结束符预留一个位置,p要初始化为0
printf("file A:\n");
gets_s(b[0]);
printf("file B:\n");
gets_s(b[1]);
p1 = fopen("a1", "w");
fputs(b[0], p1);
fclose(p1);
p2 = fopen("a2", "w");
fputs(b[1], p2);
fclose(p2);
strcpy(p, b[0]);
strcat(p, b[1]);
//puts(c1);
for (i = 0; i < 10; i++)
for (j = i; j < 10; j++) //i改成j
{
if (p[j] < p[i] || p[j] == p[i])
{
*c = *(*c1 + j);
*(*c1 + j) = *(*c1 + i);
*(*c1 + i) = *c;
}
}
printf("file C:\n");
puts(p);
putchar('\n');
p3 = fopen("a3", "w");
fputs(p, p3);
fclose(p3);
}
试试
a1.txt
a2.txt
a3.txt
你这文件没有后缀名吗?
暂时发现23行有错
#include <stdio.h>
#include <string.h>
int main()
{
FILE *p1,*p2,*p3;
int i,j;
char b[2][10],*c;char p[10],(*c1)[10],m;c1=&p;c=&m;
printf("file A:\n");
gets(b[0]);
printf("file B:\n");
gets(b[1]);
p1=fopen("a1","w");
fputs(b[0],p1);
fclose(p1);
p2=fopen("a2","w");
fputs(b[1],p2);
fclose(p2);
strcpy(p,b[0]);
strcat(p,b[1]);
//puts(c1);
for(i=0;i<10;i++)
for(j=i;j<n;j++)
{
if(p[j]<p[i]||p[j]==p[i])
{
*c=*(*c1+j);
*(*c1+j)=*(*c1+i);
*(*c1+i)=*c;
}
}
printf("file C:\n");
puts(p);
putchar('\n');
p3=fopen("a3","w");
fputs(p,p3);
fclose(p3);
}