FStream读取txt文件
FStream read txt file
我尝试了很多解决方案,但都不起作用,无论是绝对路径还是相对路径。我也更改了目录等等。我的代码总是这样工作的,我不知道哪里出了问题。
我所做的两个例子:
// Read a file into memory
#include <iostream> // std::cout
#include <fstream> // std::ifstream
int main () {
std::ifstream is ("test.txt", std::ifstream::binary);
if (is) {
// Get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length];
std::cout << "Reading " << length << " characters... ";
// Read data as a block:
is.read (buffer,length);
if (is)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << is.gcount() << " could be read";
is.close();
// ...buffer contains the entire file...
std::cout << buffer;
system("PAUSE");
delete[] buffer;
}
return 0;
}
和
char pfad[256]; //The path to the application is stored here.
_getcwd( pfad, 256);
std::string truepfad;
truepfad = pfad;
truepfad.append("\test.txt");
fstream f("C:\Users\Etix\Documents\test.txt");
string s;
if (!f)
std::cout << truepfad;
else
{
std::cout << "open file";
while (getline(f, s)){
std::cout << s;
}
}
我在使用您的代码时遇到的错误是:
Error: Expected a declaration.
Error: This declaration has no storage class or type specifier.
我认为问题可能是你错过了一些#include。我看到你需要#include<string>和其他几个语句,以及第44行的一个不正确的if语句(带有#include<string>)。
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 一种在C++中读取TXT配置文件的简单方法
- 无法打开.txt文件
- 如何使用CLion在Mac上创建一个新的.txt文件
- 我可以读取静态对象中的文件.txt吗?C++
- C++-需要读取存储在文件(.txt)中的时间戳并正确检索
- 具有文件.txt的 C++ 中的设置
- 如何在 CMakeList 中包含 h/hpp 头文件.txt而不是 cpp 或其他 hpp 文件
- 使用函数读取文件.txt中的矩阵
- 任何人都知道如何解决尝试将从MySQL数据库返回的行打印到文本文件(.txt)中的C++
- 读取文件.txt C
- 为什么我的方法不从文件.txt中读取float类型
- 如何读取名称(仅文件.txt)作为参数传递的文件(在任何目录中)?C++
- 读取文件".txt"并将每行的数据分配给循环列表中的新节点C++
- 如何用C++将字符串文件Txt解析成数组
- 如何在c++中读取和编辑文件txt
- 从文件.txt读取字符串数组并使用它来玩康威的生命游戏
- 导入文件txt时出错
- 如何仅读取文件.txt中的数字