我做的二进制文件处理,并在我的算法,我想知道实际类型pos_type和off_type计算文件的大小或寻求一个给定的位置(例如当tellg和seekg).
当计算文件的我只是大小static_cast的pos_type一个int64_t,它似乎很好地工作.
怎么样seekg?传递int64_t给它是否安全?
有没有办法制作 pos_type 和 off_type 成为 int64_t,也许使用 traits?
我想消除这个可怕的演员并找到一种符合C++标准的方法.
更新:另见
我想我会直接进入它并从代码开始:
#include <iostream>
#include <fstream>
#include <string>
class test : public std::ofstream
{
public:
test(const std::string& filename) { this->open(gen_filename(filename)); };
test(const test&) = delete;
//test(test&& old) = default; // Didn't compile
test(test&& old) {};
private:
std::string gen_filename(const std::string& filename)
{ return filename + ".tmp"; }
};
int main()
{
auto os = test("testfile");
os << "Test1\n";
os << "Test2\n";
}
Run Code Online (Sandbox Code Playgroud)
基本上,我需要返回一个流.当然你不能复制一个ofstream,所以我在类测试中摆弄代码,然后我按照你的预期编译和工作(在gcc 4.5上).
但我感觉不好这只是因为我的编译器在"auto os = test()"上做了"返回值优化"(RTO).的确,如果修改为以下内容:
int main()
{
auto os = test("testfile");
os << "Test1\n";
auto os2 = std::move(os); …Run Code Online (Sandbox Code Playgroud) 我用谷歌搜索了这个,但我仍然对如何使用它感到困惑.我正在制作文件管理器,我希望能够将文件复制并粘贴到新目录中.我知道要复制我需要使用file.copy(),但我不知道如何在我的代码中实现它.
我想用fstream做这个.
有谁知道为什么文件没有打开?我也试过把"infile.txt"放在程序的文件夹和调试文件夹中,但我用来检查打开错误的方法都触发意味着它无法打开.我知道我可以硬编码位置,但我不想.
我听说你应该这样做,stringobj.c_str()但我不知道这是否准确?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
string fileloc = "infile.txt";
infile.open(fileloc);
if (!infile)
{
cout << "open fail 1" << endl;
}
bool fail = infile.fail();
if (fail)
{
cout << "open fail 2";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 尽管有很多关于 reinterpret_cast 主题的文章,以及它有多糟糕,但我仍然对避免它的最佳方法感到困惑,尤其是在处理诸如从 fstream 读取和写入之类的函数时。所以,这是我的困境......
假设我们有一个整数数组,我们想用文件中的一些数据填充它。
std::ifstream iFile( ... );
// presume that the type of this array is not a matter of choice
int *a = new int[ 100 ];
Run Code Online (Sandbox Code Playgroud)
我们可以阅读一些不同的演员表:
iFile.read( (char *)a, sizeof( int ) * 100 );
iFile.read( reinterpret_cast< char * >( a ), sizeof( int ) * 100 );
iFile.read( static_cast< char * >( static_cast< void * >( ( a ) ), sizeof( int ) * 100 );
Run Code Online (Sandbox Code Playgroud)
第一个(C 风格)已经过时,我们在 C++ 中引入了新的风格转换,这是有充分理由的。第二个是不可移植的,不提供任何保证。第三个写起来很乏味,破坏了乐趣。
有什么替代方法吗,我应该怎么做?
编辑:
目标是使代码尽可能可移植且符合标准。
我真的需要你的帮助.我似乎无法在C++中进行文件操作.我使用fstream进行一些文件操作,但是当我编译它时,会出现一个错误:
|63|error: no matching function for call to 'std::basic_fstream<char>::open(std::string&, const openmode&)'|
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
以下是源代码的一部分:
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
inline int exports()
{
string fdir;
// Export Tiled Map
cout << "File to export (include the directory of the file): ";
cin >> fdir;
fstream fp; // File for the map
fp.open(fdir, ios::app);
if (!fp.is_open())
cerr << "File not found. Check the file a file manager if it exists.";
else
{
string creator, map_name, date;
cout << "Creator's name: …Run Code Online (Sandbox Code Playgroud) 我在类中有一个成员变量,其类型是ofstream和一个包含字符串参数的构造函数:
class dogs
{
public:
ofstream dogsFile;
dogs(string location)
{
}
};
Run Code Online (Sandbox Code Playgroud)
出现以下错误:
错误 2 错误 C2280: 'std::basic_ofstream>::basic_ofstream(const std::basic_ofstream> &)' : 尝试引用已删除的函数 c:\users\pc\documents\visual studio 2013\projects\database\database\数据库.cpp 26 1 数据库
我再次尝试了这段代码,但我没有使用字符串,而是使用了 char*:
class dogs
{
public:
ofstream dogsFile;
dogs(char* location)
{
}
};
Run Code Online (Sandbox Code Playgroud)
错误消失了。为什么?为什么字符串会出错?
编辑:这是整个代码:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class dogs
{
ofstream dogsFile;
public:
dogs(string location)
{
}
};
int main(int argc, _TCHAR* argv[])
{
dogs dog = dogs("dog.bin"); …Run Code Online (Sandbox Code Playgroud) 我是 C++ 的新手。我有一个简单的问题要问您std::fstream::X and std::ios::XC++ 中的打开文件模式有什么区别?
哪里 x可以in, out, ate, trunk, ate?
这是更多示例:
fs.open(filename.c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
Run Code Online (Sandbox Code Playgroud)
对比
fs.open(filename.c_str(), std::ios::in | std::ios::out | std::ios::app);
Run Code Online (Sandbox Code Playgroud)
这两者之间有什么区别?
请不要以复杂的方式回答,因为我是 C++ 初学者。
我需要使用一些 C++ 代码来读取制表符分隔的文本文件。该文件包含三列,第二列包含带空格的字符串。以下是该文件的一些示例。
1 hellow world uid_1
2 good morning uid_2
Run Code Online (Sandbox Code Playgroud)
以下是我需要用来读取文件的C++。但是,当遇到字符串中的空格时,它无法正确读取文件。
关于修改 while 循环以使其工作有什么建议吗?我对 C++ 不熟悉。请提供详细代码。谢谢!
#include <Rcpp.h>
#include <iostream>
#include <fstream>
#include <string>
std::ifstream infile (file_name.c_str());
int row = -1;
std::string col;
std::string uid;
while (infile >> row >> col >> uid) {
### operations on row, col and uid ####
}
Run Code Online (Sandbox Code Playgroud) 此C++代码旨在将文件中的数据从67~69移动到70~72:
#include <fstream>
#include <iostream>
int main() {
std::fstream file("test", std::ios::out);
file.close();
file.open("test", std::ios::binary | std::ios::in | std::ios::out);
for (char i = 0; i < 127; ++i)
file.write(&i, 1);
file.seekp(70);
file.seekp(-3, std::ios::cur);
char s[100];
for (int i = 0; i < 100; ++i)
s[i] = '\0';
file.read(s, 3);
for (int i = 0; i < 3; ++i)
std::cout << (int)s[i] << " ";
std::cout << std::endl;
file.write(s, 3);
file.seekp(-3, std::ios::cur);
file.read(s, 3);
for (int i = 0; i < 3; …Run Code Online (Sandbox Code Playgroud) c++ ×10
fstream ×10
gcc ×2
c++11 ×1
char ×1
codeblocks ×1
file ×1
parameters ×1
stl ×1
string ×1
syntax-error ×1
windows ×1