求懂C++的高手们解答
用C++Builder编写程序:
1.读写txt文档
2、统计txt文档中字符个数
[解决办法]
最简单的方法:
TStringList *slt=new TStringList;
slt->LoadFromFile("C:\文档名");
int zfCount=slt->Text.Length();//返回字符数
delete slt;
[解决办法]
是要统计文档中各个字符的分布吗?
void StatChar(const char *szFileName, int nCharCount[256]) {
HANDLE hFile = CreateFile(szFileName, GENERIC_READ, ...);
int nFileSize = GetFileSize(hFile, NULL);
unsigned char *buf = new unsigned char[nFileSize];
ReadFile(hFile, buf, nFileSize, &dwRead, NULL);
CloseHandle(hFile);
memset(nCharCount, 0, 256);
for ( int i=0; i<nFileSize; i++ )
nCharCount[buf[i]]++;
delete [] buf;
}
[解决办法]
少了一个定义
DWORD dwRead;
[解决办法]
读取同目录下的配置文件
[code=C/C++][/code]
ifstream ifs;
AnsiString path = ExtractFilePath(Application->ExeName);//获取路径
AnsiString road = path + "config.ini";
if (!FileExists(road)) return false;
ifs.open((road).c_str());
string s;
while (getline(ifs, s)) {}//按行读取 保存在s中
ifs.close();