c语言:将一些整数从"data1.txt"和“data2.txt”中读入,排序后输出至"data3.txt"中。
main函数中用了命令行参数data1.txtdata2.txtdata3.txt#include<stdio.h>#include<stdlib.h>/////////...
main函数中用了命令行参数 data1.txt data2.txt data3.txt
#include <stdio.h>
#include <stdlib.h>
//////////////////////////////////////////////////////////////////////////
int process(char* infile1,char* infile2,char* outfile);
// return 0: if everything is ok
// 1: if something is wrong, such as error opening a file
//////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
if (argc<4) { printf("command argument error\n"); system("pause"); exit(1); }
//////////////////////////////////////////////////////////////////////////
if (process(argv[1],argv[2],argv[3])) // argv[1] and argv[2] are input filenames
// argv[3] is output filename
//////////////////////////////////////////////////////////////////////////
{ printf("file processing error\n"); system("pause"); exit(1); }
printf("\n\nDone!\nPlease open file \"%s\" to check.\n",argv[3]);
system("pause"); return 0;
}
///////////////////////////////////////////////////////以上是老师给的主程序,下面是我编的。
#include <stdio.h>
#include <stdlib.h>
int process(char* infile1,char* infile2,char* outfile)
{
FILE *fp1,*fp2,*fp3;int a[200],i,j,k,l,temp,b;
if((fp1=fopen(infile1,"r"))==NULL)
{
printf("can't open the file!\n");
exit(1);
}
if((fp2=fopen(infile2,"r"))==NULL)
{
printf("can't open the file!\n");
exit(1);
}
if((fp3=fopen(outfile,"w"))==NULL)
{
printf("can't open the file!\n");
exit(1);
} //至此打开三个文档
i=0;
while(!feof(fp1))
{
fscanf(fp1,"%d",&a[i]);
i++;
}
while(!feof(fp2))
{
fscanf(fp2,"%d",&a[i]);
i++;
} //将前两个文档的数字放入数组a中
for(k=0;k<i;k++)
{
j=k;
for(l=k+1;l<i;l++)
{
if(a[j]>a[l]){j=l;}
}
if(j!=k)
{temp=a[j];a[j]=a[k];a[k]=temp;}
} //排序
for(j=0;j<i;j++)
{
fprintf(fp3,"%d",a[j]);
} //将排序后的放入另一文档
}
求助错在哪了。。。。谢谢了。 展开
#include <stdio.h>
#include <stdlib.h>
//////////////////////////////////////////////////////////////////////////
int process(char* infile1,char* infile2,char* outfile);
// return 0: if everything is ok
// 1: if something is wrong, such as error opening a file
//////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
if (argc<4) { printf("command argument error\n"); system("pause"); exit(1); }
//////////////////////////////////////////////////////////////////////////
if (process(argv[1],argv[2],argv[3])) // argv[1] and argv[2] are input filenames
// argv[3] is output filename
//////////////////////////////////////////////////////////////////////////
{ printf("file processing error\n"); system("pause"); exit(1); }
printf("\n\nDone!\nPlease open file \"%s\" to check.\n",argv[3]);
system("pause"); return 0;
}
///////////////////////////////////////////////////////以上是老师给的主程序,下面是我编的。
#include <stdio.h>
#include <stdlib.h>
int process(char* infile1,char* infile2,char* outfile)
{
FILE *fp1,*fp2,*fp3;int a[200],i,j,k,l,temp,b;
if((fp1=fopen(infile1,"r"))==NULL)
{
printf("can't open the file!\n");
exit(1);
}
if((fp2=fopen(infile2,"r"))==NULL)
{
printf("can't open the file!\n");
exit(1);
}
if((fp3=fopen(outfile,"w"))==NULL)
{
printf("can't open the file!\n");
exit(1);
} //至此打开三个文档
i=0;
while(!feof(fp1))
{
fscanf(fp1,"%d",&a[i]);
i++;
}
while(!feof(fp2))
{
fscanf(fp2,"%d",&a[i]);
i++;
} //将前两个文档的数字放入数组a中
for(k=0;k<i;k++)
{
j=k;
for(l=k+1;l<i;l++)
{
if(a[j]>a[l]){j=l;}
}
if(j!=k)
{temp=a[j];a[j]=a[k];a[k]=temp;}
} //排序
for(j=0;j<i;j++)
{
fprintf(fp3,"%d",a[j]);
} //将排序后的放入另一文档
}
求助错在哪了。。。。谢谢了。 展开
2个回答
展开全部
//几处小问题,把你原来的注释删了,有注释的地方就是改了的
int process(char* infile1,char* infile2,char* outfile)
{
FILE *fp1,*fp2,*fp3;int a[200],i,j,k,l,temp,b;
if((fp1=fopen(infile1,"r"))==NULL)
{
printf("can't open the file!\n");
return 1; //因为main里要接收返回值,就用return,下同
}
if((fp2=fopen(infile2,"r"))==NULL)
{
printf("can't open the file!\n");
return 1;
}
if((fp3=fopen(outfile,"w"))==NULL)
{
printf("can't open the file!\n");
return 1;
}
i=0;
while(!feof(fp1))
{
fscanf(fp1,"%d",&a[i]);
i++;
}
while(!feof(fp2))
{
fscanf(fp2,"%d",&a[i]);
i++;
}
i--; //注意这个,i要减1的,否则后面会越界
for(k=0;k<i;k++)
{
j=k;
for(l=k+1;l<i;l++)
{
if(a[j]>a[l]){j=l;}
}
if(j!=k)
{temp=a[j];a[j]=a[k];a[k]=temp;}
}
for(j=0;j<i;j++)
{
fprintf(fp3,"%d ",a[j]); //这里用空格分割下,不然数字都是连在一块的
}
fclose(fp1);//关闭文件
fclose(fp2);
fclose(fp3);
return 0; //这个要有
}
int process(char* infile1,char* infile2,char* outfile)
{
FILE *fp1,*fp2,*fp3;int a[200],i,j,k,l,temp,b;
if((fp1=fopen(infile1,"r"))==NULL)
{
printf("can't open the file!\n");
return 1; //因为main里要接收返回值,就用return,下同
}
if((fp2=fopen(infile2,"r"))==NULL)
{
printf("can't open the file!\n");
return 1;
}
if((fp3=fopen(outfile,"w"))==NULL)
{
printf("can't open the file!\n");
return 1;
}
i=0;
while(!feof(fp1))
{
fscanf(fp1,"%d",&a[i]);
i++;
}
while(!feof(fp2))
{
fscanf(fp2,"%d",&a[i]);
i++;
}
i--; //注意这个,i要减1的,否则后面会越界
for(k=0;k<i;k++)
{
j=k;
for(l=k+1;l<i;l++)
{
if(a[j]>a[l]){j=l;}
}
if(j!=k)
{temp=a[j];a[j]=a[k];a[k]=temp;}
}
for(j=0;j<i;j++)
{
fprintf(fp3,"%d ",a[j]); //这里用空格分割下,不然数字都是连在一块的
}
fclose(fp1);//关闭文件
fclose(fp2);
fclose(fp3);
return 0; //这个要有
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询