我试图std::getline()在我的项目中使用将文本文件读入字符串数组.
这是我的代码:
ifstream ifs ( path );
string * in_file;
int count = 0;
while ( !ifs.eof() )
{
++count;
if ( count == 1 )
{
in_file = new string[1];
}
else
{
// Dynamically allocate another space in the stack
string *old_in_file = in_file;
in_file = new string[count];
// Copy over values
for ( int i = 0 ; i < ( count - 1 ) ; i++ )
{
in_file[i] = old_in_file[i];
}
delete[] old_in_file; …Run Code Online (Sandbox Code Playgroud) 这可能恰好属于Stack Exchange的不同部分,但我不这么认为 - programmers.se更多的是关于其他事情.
回答问题:你可以用std :: ios :: binary做一些你无法在文本模式下做的事情(例如相对搜索),但是我无法在文本模式下找到任何你无法在二进制模式下做的事情 - 甚至使用例如std :: getline()将文件作为文本读取
那我为什么要打开文字呢?作为一个可能相关的问题,为什么不默认打开二进制?那个用例会破坏吗?
编辑其他信息
这是让我问的问题:
我有一个在Windows系统上创建的文件 - 也就是说,行结尾是CR LF.
我正在std::ifstream使用std::ios::binary旗帜打开它
我正在解析文件std::getline并获得我期望的行为 - getline一次读取一行.
系统:Windows 7专业版
编译器:用于MINGW32的g ++
假设我创建了一个像这样写的文件:
std::ofstream my_file("filename", std::ios_base::out | std::ios_base::trunc);
Run Code Online (Sandbox Code Playgroud)
如何确定此文件的权限?我有一个程序在一夜之间运行,每分钟生成一次文件 - 有些是0644,有些是0660,我的代码中没有任何东西可以让它改变.(我已经看到一些实现允许第三个参数来设置文件权限 - 我正在使用gcc 4.1.2,它似乎不支持这个).
我正在使用ifstream在C++中实现一个必须在大文件中搜索的程序(~1TB).但是,读取2GB后失败.有没有办法获得文件位置,即使对于大文件?我编译为32位Windows机器.
std::ifstream f;
f.open( filename.c_str(), std::ifstream::in | std::ifstream::binary );
while(true) {
std::cout << (uint64_t)(f.tellg()) << std::endl;
//read data
}
Run Code Online (Sandbox Code Playgroud) I wish to output data from my program to a text file. Here is a working example showing how I do it currently, where I also include the date/time (I am running Windows):
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;
int main()
{
char dateStr [9];
char timeStr [9];
_strdate(dateStr);
_strtime(timeStr);
ofstream output("info.txt", ios::out);
output << "Start time part 1 " << "\t" << timeStr << " on " << dateStr << "\n";
output << "Start time part …Run Code Online (Sandbox Code Playgroud) 我的代码是这样的:
std::istringstream file("res/date.json");
std::ostringstream tmp;
tmp<<file.rdbuf();
std::string s = tmp.str();
std::cout<<s<<std::endl;
Run Code Online (Sandbox Code Playgroud)
输出是res/date.json,而我真正想要的是这个json文件的全部内容.
就像readome甚至不读.返回0并且不读取任何字符.这有什么不对?
#include <fstream>
#include <iostream>
int main ()
{
std::fstream stream("list.cpp", std::ios::in);
if (stream.good() || !stream.bad() || stream.is_open()) {
std::cout << "Well, stream looks good." << std::endl;
char justOneChar = 'L';
auto ssize = stream.readsome(&justOneChar, 1);
std::cout << ssize << " : " << justOneChar << std::endl;
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
输出:
好吧,流看起来不错.0:L
这个app问题询问和之间的区别,ate答案和cppreference都暗示唯一的区别是,这app意味着在每次写操作之前将写游标放在文件的末尾,而意味着ate将写游标放在文件的末尾仅在打开时才显示该文件。
我实际上看到的(在 VS 2012 中)是指定ate 丢弃现有文件的内容,而app不会(它将新内容附加到以前存在的内容)。换句话说,ate似乎是在暗示trunc。
以下语句将“Hello”附加到现有文件中:
ofstream("trace.log", ios_base::out|ios_base::app) << "Hello\n";
Run Code Online (Sandbox Code Playgroud)
但以下语句将文件的内容替换为“Hello”:
ofstream("trace.log", ios_base::out|ios_base::ate) << "Hello\n";
Run Code Online (Sandbox Code Playgroud)
VS 6.0 的 MSDN 文档暗示这种情况不应该发生(但这句话似乎在 Visual Studio 的更高版本中已被撤回):
ios::trunc:如果文件已经存在,则其内容将被丢弃。如果指定了 ios::out,并且未指定 ios::ate、ios::app 和 ios:in,则隐含此模式。
我试图解析data.txt包含以下形式的十六进制的文件-0x1.0c7474fp+8:
-0x1.e51b67p+0 0x1.0b18ef5p+8 0x1.9c6075p+8 -0x1.190d44p+4
-0x1.e4f7838p+0 0x1.0b6446dp+8 0x1.9d779b8p+8 -0x1.16436ap+3
-0x1.e4555bp+0 0x1.0b6d446p+8 0x1.9e28c4ap+8 -0x1.1f8b0ep+3
-0x1.e3b9598p+0 0x1.0b7982cp+8 0x1.9edf5f8p+8 -0x1.f80bc6p+2
-0x1.e2f5896p+0 0x1.0b70bd7p+8 0x1.9f75c6ap+8 -0x1.07390ep+3
-0x1.e21d2dep+0 0x1.0b5961cp+8 0x1.9fef6e6p+8 -0x1.031012p+3
-0x1.e225b96p+0 0x1.0bbc104p+8 0x1.a12c75cp+8 -0x1.06951cp+3
-0x1.e17ec4cp+0 0x1.0bc35b7p+8 0x1.a1de5a6p+8 -0x1.25f138p+3
-0x1.e0e7f36p+0 0x1.0c140edp+8 0x1.a300c22p+8 -0x1.417644p+3
-0x1.e0e7e0ap+0 0x1.0c7474fp+8 0x1.a43f084p+8 -0x1.359f22p+3
-0x1.e076e6cp+0 0x1.0c93aebp+8 0x1.a50efaap+8 -0x1.00e406p+4
-0x1.e1c339cp+0 0x1.0ec7d62p+8 0x1.ab2777p+8 -0x1.1b6134p+3
-0x1.e0614eap+0 0x1.0e669d1p+8 0x1.ab301b8p+8 -0x1.6a997cp+2
-0x1.e01577p+0 0x1.0e9f2e5p+8 0x1.ac33c36p+8 -0x1.62eb42p+2
-0x1.de237dcp+0 0x1.0df5506p+8 0x1.abd2c42p+8 -0x1.8951c2p+2
Run Code Online (Sandbox Code Playgroud)
我写这段c++代码没有任何成功:
#include <iostream>
#include <fstream>
int main()
{
float a, b, …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个自定义的 std::fstream,它会在读取时对数据进行编码/解码。
template <class T>
class _filebuf : public std::filebuf {
public:
using transform_type = T;
int_type underflow() override {
auto c = std::filebuf::underflow();
return c < 0 ? c : transform.decode(c);
}
int_type overflow(int_type c) override {
return c < 0 ? c : std::filebuf::overflow(transform.encode(c));
}
private:
transform_type transform;
};
template <class T>
class _fstream : public std::iostream {
public:
using buffer_type = _filebuf<T>;
explicit _fstream(const std::string& path, std::ios::openmode openmode)
: std::iostream(0)
{
this->init(&buffer);
buffer.open(path, openmode);
}
private:
buffer_type …Run Code Online (Sandbox Code Playgroud)