c++file读取操作及相关error处理
标签:
file读clearc |
分类: C/CPP |
#include
#include
#include
read_file_by_line() {
fstream infile;
infile.open("filepath");//方法1 open
//fstream infile2("filepath");//方法2 构造
//assert(infile.is_open());//判断文件是否打开
if (infile.is_open()==true)//file exist
{
while(!infile.eof())//未到句尾
{
string nline;
getline(infile,nline, '\n');//从文件中读一行给newline
//myfile.getline (buffer,80);
if(infile.fail()){//读取失败
break;
}
//nline存储等操作
}
} else{
//file not exist
}
infile.close();//关文件
infile.clear();//标志位清零
}
std::ios::clear
若是在没有成功打开文件后仍调用close(),会造成错误。
void clear (iostate state = goodbit);
Set error state flags
Sets a new value for the stream's internal error state
flags.
Public member functions
(constructor) Construct object (public member function )
open Open file (public member function )
is_open Check if file is open (public member function )
close Close file (public member function )
std::ios_base::openmode
member constant opening mode
app (append) Set the stream's position indicator to the end of
the stream before each output operation.
ate (at end) Set the stream's position indicator to the end of
the stream on opening.
binary (binary) Consider stream as binary rather than
text.
in (input) Allow input operations on the stream.
out (output) Allow output operations on the stream.
trunc (truncate) Any current content is discarded, assuming a
length of zero on opening.
std::basic_ios::fail
bool fail() const;
Check whether failbit or badbit is set
Returns true if either (or both) the failbit or the badbit
error state flags is set for the stream.
std::basic_ios::eof
bool eof() const;
Check whether eofbit is set
Returns true if the eofbit error state flag is set for
the stream.
std::basic_istream::getline
basic_istream& getline (char_type* s, streamsize n
);
basic_istream& getline (char_type* s, streamsize n,
char_type delim);
Get line
Extracts characters from the stream as unformatted input and
stores them into s as a c-string,
until either the extracted character is the delimiting
character, or n characters have been written to s (including the
terminating null character).
The delimiting character is the newline character
(widen('\n')) for the first form, and delim for the second:
when found in the input sequence,
it is extracted from the input sequence, but discarded and not
written to s.
The function will also stop extracting characters if the
end-of-file is reached.
If this is reached prematurely (before either writing n
characters or finding delim), the function sets the eofbit
flag.
The failbit flag is set if the function extracts no
characters, or if the delimiting character is not found once
(n-1) characters have already been written to
s.
Note that if the character that follows those (n-1) characters
in the input sequence is precisely the delimiting
character,
it is also extracted and the failbit flag is not set (the
extracted sequence was exactly n characters long).
A null character (char_type()) is automatically appended to
the written sequence if n is greater than zero, even if an empty
string is extracted.
std::basic_fstream::close
void close();
Close file
Closes the file currently associated with the object,
disassociating it from the stream.
Any pending output sequence is written to the file.
If the stream is currently not associated with any file (i.e.,
no file has successfully been open with it), calling this function
fails.
If the operation fails (including if no file was open
before the call),
the failbit state flag is set for the stream (which may
throw ios_base::failure if that state flag was registered using
member exceptions).

加载中…