c++ 如何用 ifstream 读取txt文件的全部内容
答案:5 悬赏:10
解决时间 2021-02-17 19:33
- 提问者网友:我稀罕你
- 2021-02-17 08:10
比如txt文件名为a,该如何实现
最佳答案
- 二级知识专家网友:陪伴是最长情的告白
- 2021-02-17 09:32
#include <fstream>
ifstream fin("a.txt");
以后在程序中用 fin>> 流入变量。
当然a.txt要和exe在同一文件夹。
否则双引号中要加上路径,如c:\a.txt
若不懂,请参考c++文件流。
ifstream fin("a.txt");
以后在程序中用 fin>> 流入变量。
当然a.txt要和exe在同一文件夹。
否则双引号中要加上路径,如c:\a.txt
若不懂,请参考c++文件流。
全部回答
- 1楼网友:滚出爷的世界
- 2021-02-17 13:56
using namespace std;
ifstream infile("a.txt");
if (!infile.bad())
{
// Dump the contents of the file to cout.
cout << infile.rdbuf();
infile.close();
}
- 2楼网友:劳资的心禁止访问
- 2021-02-17 13:18
string fileinput (ifstream &infile)
{
char str[file_length];
int i = 0;
while (!infile.eof ())
{
str[i++] = infile.get ();
};
str[i] = '\0';// end of str signal for c style string
return str;
}
- 3楼网友:懂得ㄋ、沉默
- 2021-02-17 11:56
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifFile( "c:\\a.txt");
if( !ifFile )
{
return -1;
}
ifFile.seekg(0, ios::end);
int iFileSize = ifFile.tellg();
ifFile.seekg( 0, ios::beg );
char *pBuffer = new char[iFileSize];
if ( pBuffer == NULL )
{
return -1;
}
memset( pBuffer, 0, iFileSize );
ifFile.read( pBuffer, iFileSize );
return 0;
}
- 4楼网友:偏爱自由
- 2021-02-17 10:36
把文件名的后缀更改为: a.txt 就可以了。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯