rt,想设计一种排序算法,输入in.txt中的一个十个数(1 2 3 4 5 6 7 8 9 0),经过直接插入排序再输出到out.txt中。望高手解答,越简单越好只用vc,在线等

解决方案 »

  1.   

    用CStdioFile好像更好一点
    CStdioFile file;
    file.Open(...,...);
    CString strLine(_T(""));
    CString strText(_T(""));
    while(file.ReadString(strLine))
    {
      strText += strLine + _T("\r\n");
    }
    file.Close();
      

  2.   

    到不是作业,还有楼上的,那种没学过啊,还是用CFile,哪位大神给个具体的,我参考一下,感激不尽
      

  3.   

    CStdioFile::ReadString非常好用,一次读入一行数据,就是在VC上实现
      

  4.   

    文本文档,用CStdioFile::ReadString更方便至于CFile的用法,MSDN上就有
    //example for CFile::Read
    extern CFile cfile;
    char pbuf[100];
    UINT nBytesRead = cfile.Read( pbuf, 100 );
      

  5.   

    #include "stdafx.h"void InsertSort(int a[], int len)
    {
    int tmp = 0;
    int i = 0;
    int j = 0;
    for(i=1; i<len; i++)
    {
    tmp = a[i];
    for(j=i-1; j>=0; j--)
    {
    if(tmp < a[j])
    {
    a[j+1] = a[j];
    }
    else
    {
    break;
    }
    }
    a[j+1] = tmp;
    }
    }int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
    try
    {
    CFile file(_T("F:\\11.txt"), CFile::modeReadWrite);
    DWORD nLen = file.GetLength();
    char* buf = new char[nLen+1];
    memset(buf, 0, (nLen+1)*sizeof(char)); file.Read(buf, nLen); #define MAX_COUNT 10
    int num[MAX_COUNT] = {0};
    char seps[] = " \r\n";
    char* token = strtok(buf, seps);
    int nIndex = 0;
    while(NULL != token)
    {
    if(nIndex >= MAX_COUNT)
    {
    break;
    }
    num[nIndex++] = atoi(token);
    token = strtok(NULL, seps);
    }

    delete[] buf;
    buf = NULL; // Insert sort
    InsertSort(num, nIndex); CString strTmp(_T(""));
    CString strText(_T(""));
    for(int i=0; i<nIndex; i++)
    {
    strTmp.Format(_T("%d "), num[i]);
    strText += strTmp;
    }
    file.SeekToBegin();
    file.Write(strText, strText.GetLength() * sizeof(TCHAR));
    file.Close();
    }
    catch (CFileException* e)
    {
    e->ReportError();
    e->Delete();
    }
        return 0;
    }
      

  6.   

    Cannot open include file: 'stdafx.h': No such file or directory这个错误啊
      

  7.   

    你的工程里面没有'stdafx.h'这个头文件,
    换成
    #include <SDKDDKVer.h>
    #include <stdio.h>
    #include <tchar.h>
    这几个头文件吧
      

  8.   

    先读文件 在排序 ,最后输出写入文件。。很简单的
    CFILE类。
    打开文件:CFILE *fp1=fopen("in.txt","r");
    读文件一个数字到数组array中。 fscanf(fp,"%d",&array[i]); i++;  fclose(fp1);
    再排序。
    CFILE*fp2=fopen("out.txt","w+");
    最后写文件 fprintf(fp2,"%d ",array[i]);i++;
    关闭文件   fclose(fp2);     
      

  9.   

    我知道啊,但是还是写不对啊#include <stdio.h>
    void main()
    {
    int i;
    int r[10];
    FILE *fp=fopen("D:\\我的文档\\课程设计\\6.5\\Debug\\in.txt","r");
    for(i=0;i<=10;i++)
    fscanf(fp,"%d",&r[i]);
    i++;
    fclose(fp);
    printf("%d ",r[i]);
    getchar();
    }
    输出不对啊
      

  10.   

    你好,你说的#include <SDKDDKVer.h>
    这个
     <SDKDDKVer.h> 是干什么的,在哪里啊