麻瓜是什么意思:VC MFC获得一个文本文件的行数
来源:百度文库 编辑:九乡新闻网 时间:2024/05/06 11:21:38
#include "afx.h"
#include "string"
#include "fstream"
#include
using namespace std;
int main()
{
float a[2000];
int i=0,line_num;
fstream file;
file.open(_T("text1.txt"),ios::in);
if (!file)
{
cout<<"cannot open\n";
return 0;
}
string str;
while(getline(file,str))
{
file>>a[i];
i++;
}
line_num=i+1;
file.close();
cout<
cout<<"a["<
return 0;
}
#include#include using namespace std; int main(void) { char txtname[50]; char getCh; const char BR = char(10); //换行 等价于 '\n' unsigned short l = 0; cout<<"请输入你要察看的文件名(全称):"; cin>>txtname; fstream myin(txtname, ios::in | ios::binary); if(NULL == myin) { cout<<"打开文件错误或没有该文件,退出。"<
while(myin.get(getCh)) { if(BR == getCh) l++; } myin.close(); cout<<"文件 "<return 0; }
#include "afx.h"
#include "string"
#include "fstream"
using namespace std;
ifstream outfile;
outfile.open(_T("WestPosition.txt"),ios::in);
string s;
int linenum=0;
if (outfile)
{
while (outfile.getline((char *)s.c_str(),16))
{
linenum++;
}
outfile.close();
CString cstr;
cstr.Format(_T("行数为%d"),linenum);
MessageBox(cstr);
}
else
{
MessageBox(_T("open failed!"));
}
读取一行文本到rString,遇到回车换行符停止读取
CStdioFile::ReadString(LPTSTR lpsz, UINT nMax);
读取一行文本到缓冲区,遇到“0x0D,0x0A”时停止读取,并且去掉硬回车“0x0D”,保留换行符“0x0A”,在字符串末尾添加“\0”(0x00)。nMax个字符里包含0x00这个字符。分析如下:
1)如果nMax <= 字符数,读取(nMax-1)个字符 + 0x00
2)如果nMax = 字符数 + 1,读取nMax个字符 + 0x00
3)如果nMax > 字符数,读取nMax个字符 + 0x0A + 0x00
CStdioFile::ReadString(CString &rString);(重载)
读取一行文本到rString,遇到回车换行符停止读取。回车和换行符不读到rString,而且末尾也没有添加“\0”。