编写一个控制台程序,打开一个txt文件,输入20个整数,通过数组编程的方式,对其从小到大排序,将排序的结果保存到“result.txt”中。
这个程序有问题,求大神帮帮忙啊
#include "stdafx.h"
#include "iostream.h"
#include "string.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
#include "fstream.h"
//using namespace std;
#define M20
int main(int argc, char* argv[])
{
fstream file1; // 定义一个fstream类的对象用于读
file1.open("1.txt", ios::in);
if (!file1)
{
cout<<"1.txt不能打开!\n";
return 1;
}
fstream file2; // 定义一个fstream类的对象用于写
file2.open("result.txt", ios::out | ios::trunc);
if (!file2)
{
cout<<"result.txt不能创建!\n";
file1.close();
return 1;
}
int k=0;
char ch;
int a[M];
while (!file1.eof())
{
file1.read(&ch, 1);
cout<<ch;
int atoi(const char *nptr);
for(int k=0;k<20;k++)
{
a[k]=atoi(&ch);
}
}
cout<<a[k]<<endl;
int max;
for (int i=0; i<M-1; i++)
{ // 外循环
max = a[i]; k = i;
for (int j=i+1; j<M; j++)
{ // 内循环
if (max<a[j])
{ max = a[j]; k = j; }
}
a[k] = a[i]; a[i] = max;
}
char str[M];
for(int u=0;u<M;u++)
{
sprintf(&str[u],"%d",a[u]); //sprintf函数
}
str[u]='\0';
cout<<str<<endl;
for(int q=0;q<M;q++)
{
cout<<str[q]<<endl;
file2.write(&str[q],1);
}
file2.close(); // 不要忘记文件使用结束后要及时关闭
file1.close();
return 0;
}