如何在vc中实现读取和写txt文档 rt,想设计一种排序算法,输入in.txt中的一个十个数(1 2 3 4 5 6 7 8 9 0),经过直接插入排序再输出到out.txt中。望高手解答,越简单越好只用vc,在线等 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用CStdioFile好像更好一点CStdioFile file;file.Open(...,...);CString strLine(_T(""));CString strText(_T(""));while(file.ReadString(strLine)){ strText += strLine + _T("\r\n");}file.Close(); 到不是作业,还有楼上的,那种没学过啊,还是用CFile,哪位大神给个具体的,我参考一下,感激不尽 CStdioFile::ReadString非常好用,一次读入一行数据,就是在VC上实现 文本文档,用CStdioFile::ReadString更方便至于CFile的用法,MSDN上就有//example for CFile::Readextern CFile cfile;char pbuf[100];UINT nBytesRead = cfile.Read( pbuf, 100 ); #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;} Cannot open include file: 'stdafx.h': No such file or directory这个错误啊 你的工程里面没有'stdafx.h'这个头文件,换成#include <SDKDDKVer.h>#include <stdio.h>#include <tchar.h>这几个头文件吧 先读文件 在排序 ,最后输出写入文件。。很简单的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); 我知道啊,但是还是写不对啊#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();}输出不对啊 你好,你说的#include <SDKDDKVer.h>这个 <SDKDDKVer.h> 是干什么的,在哪里啊 MFC自制按钮 想学习密码学方面的知识,大家给推荐一本书吧,要基础而经典的书。谢谢 问两个关于VC6的问题 请教! CSocket问题 初来乍到,提问,关于VC的未来,我没多少分啊,对不起了 谁有OPC2.0的规范我要. 100分求救!为何Java Socket 与 VC Socket连接不上??? 请教高手 当拉动滚动条时,怎样消除屏幕闪烁? 哪个网站有VC++的教程呀? 派生CTreeView的问题 CCmdTarget会过滤消息么,只接受WM_COMMAND消息?
CStdioFile file;
file.Open(...,...);
CString strLine(_T(""));
CString strText(_T(""));
while(file.ReadString(strLine))
{
strText += strLine + _T("\r\n");
}
file.Close();
//example for CFile::Read
extern CFile cfile;
char pbuf[100];
UINT nBytesRead = cfile.Read( pbuf, 100 );
{
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;
}
换成
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
这几个头文件吧
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);
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();
}
输出不对啊
这个
<SDKDDKVer.h> 是干什么的,在哪里啊