标签: ifstream

istream :: getline返回类型

istream::getline方法返回什么?

我问,因为我已经看到循环文件,它应该这样做:

while ( file.getline( char*, int ) )
{
    // handle input
}
Run Code Online (Sandbox Code Playgroud)

什么回来了?

c++ file-io getline istream ifstream

17
推荐指数
3
解决办法
2万
查看次数

ifstream seekg有什么问题

我正在尝试寻找并重新读取数据.但代码失败了.

代码是

std::ifstream ifs (filename.c_str(), std::ifstream::in | std::ifstream::binary);

std::streampos pos = ifs.tellg();

std::cout <<" Current pos:  " << pos << std::endl;

// read the string
std::string str;
ifs >> str;

std::cout << "str: " << str << std::endl;
std::cout <<" Current pos:  " <<ifs.tellg() << std::endl;

// seek to the old position
ifs.seekg(pos);

std::cout <<" Current pos:  " <<ifs.tellg() << std::endl;

// re-read the string
std::string str2;
ifs >> str2;

std::cout << "str2: (" << str2.size() << ") " << …
Run Code Online (Sandbox Code Playgroud)

c++ ifstream seekg

17
推荐指数
2
解决办法
2万
查看次数

ifstream :: rdbuf()实际上做了什么?

我有以下代码,它工作得很好(除了它很慢的事实,但我并不在乎).将infile的全部内容写入outfile似乎并不直观.

// Returns 1 if failed and 0 if successful
int WriteFileContentsToNewFile(string inFilename, string outFilename)
{
    ifstream infile(inFilename.c_str(), ios::binary);
    ofstream outfile(outFilename.c_str(), ios::binary);

    if( infile.is_open() && outfile.is_open() && infile.good() && outfile.good() )
    {
        outfile << infile.rdbuf();

        outfile.close();
        infile.close();
    }
    else
        return 1;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

任何见解?

c++ ifstream

16
推荐指数
2
解决办法
2万
查看次数

C++:将cin分配给ifstream变量?

你知道stdin由文件名" - "指定的常见stdio习语,例如

if ((strcmp(fname, "-"))
    fp = fopen(fname);
else
    fp = stdin;
Run Code Online (Sandbox Code Playgroud)

使用ifstream实例执行此操作的最佳方法是什么?我收到了一些代码,它有ifstream一个类的一部分,我想添加代码来做同等的事情,比如:

if ( filename == "-")
    logstream = cin;  // **how do I do this*?*
else
    logstream.open( filename.c_str() );
Run Code Online (Sandbox Code Playgroud)

c++ iostream ifstream

16
推荐指数
1
解决办法
1万
查看次数

ifstream :: is_open vs ifstream :: fail?

读Savitch的问题在C++中求解,std::ifstream::fail被示出作为一个例子,以检查是否一个文件已被正确地打开(ifstreamofstream).

我之前使用过,就像我第一次看到的那样,std::ifstream::is_open执行相同的检查.

哪个'更好'的做法?

或者在尝试打开后直接调用任何一个的情况下,它是否没有实际区别?

c++ ifstream

16
推荐指数
2
解决办法
9351
查看次数

为什么我不能用`fstream`的实例初始化对`ofstream` /`ifstream`的引用?

介绍

void  read_foo (std::ifstream& out);
void write_foo (std::ofstream& out);
Run Code Online (Sandbox Code Playgroud)

我有这两个函数,其中一个应该从一个文件读取,另一个应该写入一个.

一切都有下面的片段;

std::ifstream ifs ("filename.txt");
read_foo  (ifs);
Run Code Online (Sandbox Code Playgroud)

std::ofstream ofs ("filename.txt");
write_foo (ofs);
Run Code Online (Sandbox Code Playgroud)

问题

但是,如果我尝试使用a std::fstream,那么我可以使用相同的流调用这两个函数,它不会编译,并且编译器会发出大量错误消息.

  • 为什么我不能绑定fstreamifstream&,或ofstream&

Foo.cpp中

#include <fstream>

void  read_foo (std::ifstream& out);
void write_foo (std::ofstream& out);

int main () {
  std::fstream fs ("filename.txt");

   read_foo (fs);
  write_foo (fs);
}
Run Code Online (Sandbox Code Playgroud)

错误(S):


foo.cpp: In function ‘int main()’:
foo.cpp:9:16: error: invalid initialization of reference of type ‘std::ifstream& {aka std::basic_ifstream<char>&}’ from expression of type ‘std::fstream {aka std::basic_fstream<char>}’ read_foo …

c++ fstream reference ifstream ostream

15
推荐指数
1
解决办法
5280
查看次数

在命中文件末尾后回滚ifstream对象

有一个包含几个字符的文本文件(比方说10),您可以尝试从中读取1000个字符.

char *buf = new char[1000];
ifstream in("in.txt");
in.read(buf, 1000);
Run Code Online (Sandbox Code Playgroud)

当然,这将设置eofbit标志(以及failbit),但是,您将能够获得所需的字符.

现在,假设您想再次读取该文件(从头开始):

in.seekg(0);        // Sets input position indicator. 
in.read(buf, 100);  // Try to read again.
Run Code Online (Sandbox Code Playgroud)

如果你打电话,这不起作用

int count = in.gcount()  // Charecters readed from input.
Run Code Online (Sandbox Code Playgroud)

你会注意到的count == 0.这意味着它根本没有任何内容.

因此问题是:如何在文件结束后重新打开文件?

c++ io ifstream

15
推荐指数
1
解决办法
1万
查看次数

如何在C++中读取格式化数据?

我格式化了如下数据:

Words          5
AnotherWord    4
SomeWord       6

它在一个文本文件中,我使用ifstream来读取它,但是如何将数字和单词分开?这个单词只包含字母,单词和数字之间会有一些空格或标签,不确定多少.

c++ string iostream ifstream string-parsing

14
推荐指数
1
解决办法
2万
查看次数

std :: ifstream明显慢于FILE吗?

我被告知我的库比它应该慢,大约30+次解析特定文件(文本文件,大小326 kb)太慢.用户建议可能是我正在使用std::ifstream(大概是代替FILE).

我宁愿不盲目改写,所以我想我先在这里查看,因为我的猜测是其他地方的瓶颈.我读逐个字符,所以我使用的唯一的功能是get(),peek()tellg()/seekg().

更新:

我描述了,并且输出令人困惑 - gprof似乎并没有想到花了这么长时间.我重写了程序,首先将整个文件读入缓冲区,然后加速大约100倍.我认为问题可能tellg()/seekg()是花了很长时间,但gprof可能由于某种原因无法看到.在任何情况下,即使对于此大小,ifstream不会缓冲整个文件.

c++ optimization file-io ifstream

12
推荐指数
1
解决办法
1万
查看次数

C++从文件流中读取unsigned char

我想从二进制文件中读取无符号字节.所以我写了下面的代码.

#include <iostream>
#include <fstream>
#include <vector>
#include <istream>

std::string filename("file");
size_t bytesAvailable = 128;
size_t toRead = 128;

std::basic_ifstream<unsigned char> inf(filename.c_str(), std::ios_base::in | std::ios_base::binary) ;
if (inF.good())
{
    std::vector<unsigned char> mDataBuffer;
    mDataBuffer.resize(bytesAvailable) ;
    inF.read(&mDataBuffer[0], toRead) ;
    size_t counted = inF.gcount() ;
}
Run Code Online (Sandbox Code Playgroud)

这导致读取始终为0字节,如计数变量所示.

网上似乎有一些引用说我需要设置语言环境才能使其正常工作.如何做到这一点对我来说并不清楚.

相同的代码使用数据类型'char'而不是'unsigned char'

上面使用unsigned char的代码似乎可以在Windows上运行,但无法在colinux Fedora 2.6.22.18中运行.

为了让它适用于linux,我需要做些什么?

c++ file-io stream ifstream

12
推荐指数
2
解决办法
5万
查看次数