标签: ifstream

使用C++ ifstream提取运算符>>从文件中读取格式化数据

根据我的学习,我正在尝试使用c ++ ifstream及其operator >>使用下面的代码从文本文件中读取数据.文本文件outdummy.txt具有以下内容:

just dummy
Hello ofstream
555
Run Code Online (Sandbox Code Playgroud)

我的问题是如何将文件中存在的char数据读入char数组或字符串.如何在下面的代码中使用ifstream :: operator >>来完成此操作.

#include <iostream>
#include <fstream>

int main()
{
    int a;
    string s;
    char buf[100];
    ifstream in("outdummy.txt",ios_base::in);


    in.operator>>(a); //How to read integer? How to read the string data.??

    cout << a;

    in.close();
    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ operators ifstream

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

如何加快逐行读取ASCII文件的速度?(C++)

这里有一些代码,在做了一些测量之后是一个相当大的瓶颈:

//-----------------------------------------------------------------------------
//  Construct dictionary hash set from dictionary file
//-----------------------------------------------------------------------------
void constructDictionary(unordered_set<string> &dict)
{
    ifstream wordListFile;
    wordListFile.open("dictionary.txt");

    std::string word;
    while( wordListFile >> word )
    {
        if( !word.empty() )
        {
            dict.insert(word);
        }
    }

    wordListFile.close();
}
Run Code Online (Sandbox Code Playgroud)

我正在读大约200,000字,这在我的机器上大约需要240毫秒.ifstream这里使用效率高吗?我可以做得更好吗?我正在阅读有关mmap()实现的内容,但我并不是100%理解它们.输入文件只是带有*nix行终止的文本字符串.

编辑:建议替代方案的后续问题:任何替代方案(减去增加流缓冲区大小)是否意味着我编写了一个解析每个字符的解析器?我有点像流的简单语法,但如果我需要速度,我可以重新写一些更细节的东西.将整个文件读入内存是一个可行的选择,它只有2mb左右.

编辑#2: 我发现对我的减速是由于设置插入,但对于那些仍然有兴趣加快逐行文件IO的人,请在这里阅读答案并查看Matthieu M.的关于这个主题的继续.

c++ optimization file-io ifstream

11
推荐指数
1
解决办法
6664
查看次数

如何在C++中手动读取PNG文件?

便携式网络图形概述

任何给定PNG文件的总体布局如下所示:

文件头:8字节签名.

Chunks:数据块,从图像属性到实际图像本身.


问题

我想用C++读取PNG文件而不使用任何外部库.我想这样做是为了更深入地理解PNG格式和C++编程语言.

我开始使用fstream逐字节读取图像,但我无法通过任何PNG文件的标头.我尝试使用read( char*, int )将字节放入char数组,但read在标头之后的每个字节都失败.

如上所示,我认为我的程序总是被文件结束1A字节所占用.我正在使用Windows 7开发Windows 7和Linux机器.


一些我的(旧)代码

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstddef>

const char* INPUT_FILENAME = "image.png";

int main()
{
  std::ifstream file;
  size_t size = 0;

  std::cout << "Attempting to open " << INPUT_FILENAME << std::endl;

  file.open( INPUT_FILENAME, std::ios::in | std::ios::binary | std::ios::ate );
  char* data = 0;

  file.seekg( 0, std::ios::end );
  size = file.tellg();
  std::cout << "File …
Run Code Online (Sandbox Code Playgroud)

c++ file-io png fstream ifstream

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

将二进制文件读入vector <char>读取少于完整文件

我有二进制文件,其内容我试图读入一个向量.所有文件都是相同的大小,但使用下面的代码,最终的矢量大小总是比文件大小小一点,并且它与文件不同(但每个文件都相同).我对这里发生的事感到困惑......

#include <fstream>
#include <vector>
#include <iostream>
#include <iterator>
int main(int argc, char *argv[]) {
  std::string filename(argv[1]);

  // Get file size
  std::ifstream ifs(filename, std::ios::binary | std::ios::ate);
  int size = (int)ifs.tellg();
  std::cout << "Detected " << filename << " size: " << size << std::endl; // seems correct!

  // Load file
  ifs.seekg(0, std::ios::beg);
  std::istream_iterator<char unsigned> start(ifs), end;
  std::vector<char unsigned> v;
  v.reserve(size);
  v.assign(start, end);

  std::cout << "Loaded data from " << filename << ", with " << v.size() << " elements" << …
Run Code Online (Sandbox Code Playgroud)

c++ vector ifstream

11
推荐指数
1
解决办法
879
查看次数

如何使用unicode filename c ++读取二进制文件?

在我正在研究的项目中,我处理了很多字符串操作; 从二进制文件中读取字符串及其编码(可以是单字节或双字节).本质上,我读取字符串值为vector<char>,读取编码,然后将所有字符串转换为wstring,以保持一致性.

这工作得相当好,但文件名本身可以是双字节字符.我完全不知道如何实际打开输入流.在CI中会使用_wfopen函数传递wchar_t* path,但wifstream似乎表现不同,因为它专门用于从文件中读取双字节字符,而不是从具有双字节文件名的文件中读取单个字节.

这个问题的解决方案是什么?

编辑:搜索网络,看起来在标准C++中根本不支持这一点(例如,请参阅此讨论).但是我想知道C++ 11是否真的在这方面添加了一些有用的东西.

c++ unicode ifstream c++11

10
推荐指数
1
解决办法
1537
查看次数

在'if'条件中定义fstream

答案中有以下代码:

if (std::ifstream input("input_file.txt"))
  ;
Run Code Online (Sandbox Code Playgroud)

这似乎很方便,将'input'变量的范围限制在确认有效的位置,但VS2015和g ++似乎都没有编译它.它是某些编译器特定的东西还是需要一些额外的标志?

在VS2015中,IDE突出显示"std :: ifstream"和"input_file.txt"以及最后一个括号."std :: ifstream"标记为"错误:此处不允许使用函数类型".

VS2015 C++编译器给出以下错误:

  • C4430缺少类型说明符 - 假设为int.注意:C++不支持default-int
  • C2059语法错误:'('

c++ fstream if-statement ifstream ofstream

10
推荐指数
2
解决办法
1147
查看次数

在函数中返回ifstream

这对你来说可能是一个非常无聊的问题:如何(如果可能的话)我可以从函数中返回ifstream吗?

基本上,我需要从用户那里获取数据库的文件名,如果不存在具有该文件名的数据库,那么我需要为用户创建该文件.我知道怎么做,但只是要求用户在创建文件后重新启动程序.我希望尽可能避免给用户带来不便,但下面的函数不能在gcc中编译:

ifstream getFile() {
    string fileName;
    cout << "Please enter in the name of the file you'd like to open: ";
    cin >> fileName;
    ifstream first(fileName.c_str());
    if(first.fail()) {
        cout << "File " << fileName << " not found.\n";
        first.close();
        ofstream second(fileName.c_str());
        cout << "File created.\n";
        second.close();
        ifstream third(fileName.c_str());
        return third; //compiler error here
    }
    else
        return first;
}
Run Code Online (Sandbox Code Playgroud)

编辑:对不起,忘了告诉你编译器错误在哪里以及是什么:

main.cpp:45: note: synthesized method ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)’ first required here 
Run Code Online (Sandbox Code Playgroud)

编辑:我更改了函数以返回指针而不是Remus建议,并将main()中的行更改为"ifstream database =*getFile()"; 现在我再次收到此错误,但这次是在main()中的行:

main.cpp:27: note: synthesized …
Run Code Online (Sandbox Code Playgroud)

c++ fstream ifstream ofstream

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

ifstream :: read不会告诉它真正读取了多少字节?

我正在ifstream::read用来读文件,

ifstream ifs("a.txt");
char buf[1024];
ifs.read(buf, 1024);
Run Code Online (Sandbox Code Playgroud)

但是a.txt的大小可能小于1000 bytes,所以我怎么知道从中读取了多少字节ifs

c++ ifstream readfile

9
推荐指数
1
解决办法
7226
查看次数

ifstream.eof() - 在真实结束之前到达文件末尾

我有一个大约11.1G的二进制文件,其中存储了Kinect的一系列深度帧.此文件中有19437个帧.要每次读取一帧,我在fstream中使用ifstream,但它在文件的真实结束之前达到eof.(我只得到了前20帧,并且由于eof标志,函数停止了)

然而,所有的帧可以通过读取FREAD标准输入输出来代替.

谁能解释这种情况呢?感谢您在我的问题上花费宝贵的时间.

这是我的两个功能:

// ifstream.read() - Does Not Work: the loop will stop after 20th frame because of the eof flag
ifstream depthStream("fileName.dat");
if(depthStream.is_open())
{
  while(!depthStream.eof())
  {
    char* buffer = new char[640*480*2];
    depthStream.read(buffer, 640*480*2);

    // Store the buffer data in OpenCV Mat

    delete[] buffer;
  }
}

// fread() - Work: Get 19437 frames successfully
FILE* depthStream
depthStream = fopen("fileName.dat", "rb");
if(depthStream != NULL)
{
  while(!feof(depthStream))
  {
    char* …
Run Code Online (Sandbox Code Playgroud)

c++ ifstream fread

9
推荐指数
1
解决办法
5401
查看次数

ifstream和ofstream或fstream使用in和out

处理文件时,首选以下两个例子中的哪一个?一个人提供比另一个更好的表现吗?有什么不同吗?

ifstream input("input_file.txt");
ofstream output("output_file.txt");
Run Code Online (Sandbox Code Playgroud)

VS

fstream input("input_file.txt",istream::in);
fstream output("output_file.txt",ostream::out);
Run Code Online (Sandbox Code Playgroud)

c++ fstream ifstream ofstream

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