我正在尝试探索 ifstream 类并在下面编写了读取文件的代码Test.txt
'Test.txt' - 内容
This is Line One
This is Line Two
This is Line Three
This is Line Four
This is Line Five
代码编写:
#include <iostream>
#include <fstream>
#include <limits>
using namespace std;
int main()
{
char buff[50];
char ch;
ifstream is("test.txt");
if (!is)
cout << "Unable to open " << endl;
while(is)
{
ch=(char)is.get();
if(ch != EOF)//If EOF is not checked then
//EOF converted as a char is displyed as
// last char of the file
cout << ch;
}
cout << "\n\n###########\n\n";
is.clear(); //clearing ios_base::eofbit which was set
//in previous action
is.seekg(0,ios_base::beg); //Going back to start of File
while(is)
{
is.get(buff,50,'\n');
cout << buff ;
cout << "\n--------------\n";
is.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
//Flushing the is stream as '\n' was left by get fn
}
cout << "\n\n@@@@@@@@@@@@@@\n\n";
is.clear();
is.seekg(0,ios_base::beg);
while(!is.eof())
{
is.getline(buff,50,'\n');
cout << buff;
cout << "\n--------------\n";
//No need to flush the is stream as '\n'
//was extracted and discarded by getline
}
cout << "\n\n$$$$$$$$$$$$$$\n\n";
is.clear();
is.seekg(0,ios_base::end);
int size=is.tellg();
is.seekg(0,ios_base::beg);
cout << "size : " << size << endl;
//char* readBuff = (char *) ::operator new(sizeof(char)*size);
char* readBuff = new char[size];
is.read(readBuff,size);
cout << readBuff;
delete(readBuff);
is.close();
return 0;
}
输出:
Gaurav@Gaurav-PC /cygdrive/d/Trial
$ ./Trial
This is Line One
This is Line Two
This is Line Three
This is Line Four
This is Line Five
###########
This is Line One
--------------
This is Line Two
--------------
This is Line Three
--------------
This is Line Four
--------------
This is Line Five
--------------
@@@@@@@@@@@@@@
This is Line One
--------------
This is Line Two
--------------
This is Line Three
--------------
This is Line Four
--------------
This is Line Five
--------------
$$$$$$$$$$$$$$
size : 92
This is Line One
This is Line Two
This is Line Three
This is Line Four
This is Line Five▒u
有一些问题我想问并得到澄清:
1)当我使用get
如下
while(is)
{
is.get(buff,50,'\n');
cout << buff ;
// cout << "\n--------------\n";
is.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
//Flushing the is stream as '\n' was left by get fn
}
即我注释掉cout << "\n--------------\n";
然后文件被读取为
###########
This is Line Fivee
即它错过了前四行并且只读取了最后一行加上额外的'e'
..无法弄清楚为什么会这样?
2)当我使用getline
如下:
// while(!is.eof())
while(is)
{
is.getline(buff,50,'\n');
cout << buff;
cout << "\n--------------\n";
//No need to flush the is stream as '\n'
//was extracted and discarded by getline
}
即我使用while(is)
而不是while(!is.eof())
- 我得到了输出:
@@@@@@@@@@@@@@
This is Line One
--------------
This is Line Two
--------------
This is Line Three
--------------
This is Line Four
--------------
This is Line Five
--------------
--------------
即在最后一行之后,我得到了额外的两行。再次无法弄清楚为什么会这样?
3)使用read
函数,我得到的大小是92
文件中的字符总数89
包括EOF
,spaces
和'\n'
. 最后一行在文件的最后一个字符后显示两个垃圾字符。为什么会有这样的行为?
cout << "\n\n$$$$$$$$$$$$$$\n\n";
is.clear();
is.seekg(0,ios_base::end);
int size=is.tellg();
is.seekg(0,ios_base::beg);
cout << "size : " << size << endl;
//char* readBuff = (char *) ::operator new(sizeof(char)*size);
char* readBuff = new char[size];
is.read(readBuff,size);
cout << readBuff;
delete(readBuff);
输出:
$$$$$$$$$$$$$$
size : 92
This is Line One
This is Line Two
This is Line Three
This is Line Four
This is Line Five▒u
谢谢
编辑:
根据 Mats Peterson 收到的答复,我尝试了以下代码:
while(is.get(buff,50,'\n'))
{
cout << buff ;
//cout << "\n--------------\n";
is.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
//Flushing the is stream as '\n' was left by get fn
}
cout << "\n\n@@@@@@@@@@@@@@\n\n";
is.clear();
is.seekg(0,ios_base::beg);
// while(!is.eof())
while(is.getline(buff,50,'\n'))
{
cout << buff;
//cout << "\n--------------\n";
//No need to flush the is stream as '\n'
//was extracted and discarded by getline
}
但是得到了输出:
###########
This is Line Fivee
@@@@@@@@@@@@@@
This is Line Fivee
即只读取最后一行...如果我取消注释//cout << "\n--------------\n";
我会得到正确的阅读
@Down投票至少评论是什么让你这样做?我遇到了这个问题,这就是为什么在这里要求从专家那里获得更多见解。