64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
using namespace std;
int const MAXLINE = 20;
int main(int argc,char **argv)
{
char buff[MAXLINE] = "test.txt";
fstream infile(buff, ios::in);
if ( infile == NULL)
{
cerr << "can't open file " << buff << endl;
exit(0);
}
cout << "open " << buff << " successfully!" << endl;
infile.getline(buff, MAXLINE);
cout << buff << endl;
char ch;
while ( infile.get(ch) ) //读取文件里的内容
{
cout << ch;
}
infile.close();
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int const MAXLINE = 20;
int main(int argc,char **argv)
{
char buff[MAXLINE] = "test.txt";
fstream infile(buff, ios::in);
if ( infile == NULL)
{
cerr << "can't open file " << buff << endl;
exit(0);
}
cout << "open " << buff << " successfully!" << endl;
while ( !infile.eof() )
{
memset(buff, 0, MAXLINE);
infile.getline(buff, MAXLINE);
cout << buff << endl;
}
infile.close();
system("pause");
return 0;
}
_Myt& __CLR_OR_THIS_CALL getline(_Elem *_Str,
streamsize _Count, _Elem _Delim)
{ // get up to _Count characters into NTCS, discard _Delim
_DEBUG_POINTER(_Str);
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
const sentry _Ok(*this, true);
if (_Ok && 0 < _Count)
{ // state okay, use facet to extract
int_type _Metadelim = _Traits::to_int_type(_Delim);
_TRY_IO_BEGIN
int_type _Meta = _Myios::rdbuf()->sgetc();
for (; ; _Meta = _Myios::rdbuf()->snextc())
if (_Traits::eq_int_type(_Traits::eof(), _Meta))
{ // end of file, quit
_State |= ios_base::eofbit;
break;
}
else if (_Meta == _Metadelim)
{ // got a delimiter, discard it and quit
++_Chcount;
_Myios::rdbuf()->sbumpc();
break;
}
else if (--_Count <= 0)
{ // buffer full, quit
_State |= ios_base::failbit;
break;
}
else
{ // got a character, add it to string
++_Chcount;
*_Str++ = _Traits::to_char_type(_Meta);
}
_CATCH_IO_END
}
*_Str = _Elem(); // add terminating null character
_Myios::setstate(_Chcount == 0 ? _State | ios_base::failbit : _State);
return (*this);
}
#include <stdio.h>
FILE *f;
#define MAX_LINE_LEN 256
char ln[MAX_LINE_LEN];
int main() {
f=fopen("test.txt","r");
if (NULL==f) {
printf("Can not open file test.txt!\n");
return 1;
}
while (1) {
if (NULL==fgets(ln,MAX_LINE_LEN,f)) break;
printf("%s",ln);
}
fclose(f);
return 0;
}