是否有(跨平台)方法从C++ std :: fstream获取C FILE*句柄?
我问的原因是因为我的C++库接受fstreams并且在一个特定的函数中我想使用接受FILE*的C库.
我听说过流,更具体地说是文件流.
那他们是什么?
它是在记忆中有位置的东西吗?
它是包含数据的东西吗?
它只是文件和对象之间的连接吗?
任何帮助,将不胜感激
我可以从fstream对象获取文件名或路径吗?我查看了方法,fstream但没有找到任何接近它的方法.
Implicit instantiation of undefined template 'std::basic_ifstream<char,std::char_traits<char>>'
#ifndef MAPPER_H
#define MAPPER_H
#include <iostream>
#include <string>
#include <vector>
#include "KeyValue.h"
#include "Parser.h"
using namespace std;
class Mapper
{
public:
Mapper(ifstream& infile);
~Mapper(void);
void loadTokens();
void showTokens();
void map();
void printMap();
void printMap(string map_fileName);
private:
ifstream inFile; //<-- is where the error is happening
vector<string> tokens;
vector<KeyValue> map_output;
Parser* parser;
};
#endif
Run Code Online (Sandbox Code Playgroud)
我甚至尝试过推杆std::ifstream但它仍然无效.
当我#include <fstream>代替的#include <iostream>,我在得到这些错误fstream.tcc和basic_ios.tcc:
'operator=' is a private member of 'std::basic_streambuf<char>' …
我正在使用C++ 在Qt应用程序中实现文件保存功能.
我正在寻找一种方法来检查所选文件是否已经存在,然后写入它,以便我可以向用户提示警告.
我正在使用std::ofstream,我不是在寻找Boost解决方案.
我在C++上编程.在我的代码中,我创建了一个文本文件,将数据写入文件并使用流从文件中读取,在完成序列后,我希望清除txt文件中的所有数据.有人可以告诉我清除txt文件中的数据的命令.谢谢
我想用来fstream读取txt文件.
我正在使用inFile >> characterToConvert,但问题是这省略了任何空格和换行符.
我正在编写加密程序,所以我需要包含空格和换行符.
什么是实现这一目标的正确方法?
我正在尝试使用std :: fstream for io来存档,我想创建该文件(如果它尚不存在).
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
std::cout<<"error"<<strerror(errorno);
Run Code Online (Sandbox Code Playgroud)
我得到这个结果:"没有这样的文件或目录."
在这种情况下如何创建文件?
我是否总是必须为从std::fstream类中实例化的对象指定绝对路径?换句话说,有没有办法指定它们的相对路径,例如项目路径?
我正在做的事情:
我试图将一个矢量分成两个独立的数组.当前int向量包含文本文件中每行的元素.文本文件是随机整数列表.
我打算怎么做:
我目前的想法是创建两个常规int数组,然后迭代整个向量并将n/2个元素复制到每个数组.
我想知道的是:
完成任务的最优雅方式是什么?我有一种感觉,我可以做到这一点,而无需多次迭代矢量.
码:
#include <vector>
#include <fstream>
#include <iterator>
#include <iostream>
using namespace std;
vector<int> ifstream_lines(ifstream& fs)
{
vector<int> out;
int temp;
while(fs >> temp)
{
out.push_back(temp);
}
return out;
}
vector<int> MergeSort(vector<int>& lines)
{
int split = lines.size() / 2;
int arrayA[split];
int arrayB[split];
}
int main(void)
{
ifstream fs("textfile.txt");
vector<int> lines;
lines = ifstream_lines(fs);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢 :)