相关疑难解决方法(0)

将整个ASCII文件读入C++ std :: string

我需要将整个文件读入内存并将其放在C++中std::string.

如果我把它读成a char[],答案很简单:

std::ifstream t;
int length;
t.open("file.txt");      // open input file
t.seekg(0, std::ios::end);    // go to the end
length = t.tellg();           // report location (this is the length)
t.seekg(0, std::ios::beg);    // go back to the beginning
buffer = new char[length];    // allocate memory for a buffer of appropriate dimension
t.read(buffer, length);       // read the whole file into the buffer
t.close();                    // close file handle

// ... Do stuff with buffer here ...
Run Code Online (Sandbox Code Playgroud)

现在,我想做同样的事情,但是使用a std::string而不是a char[] …

c++ string file-io caching standard-library

559
推荐指数
5
解决办法
49万
查看次数

为什么"while(!feof(file))"总是错的?

我看到人们最近在很多帖子中试图读取这样的文件.

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
    char *path = argc > 1 ? argv[1] : "input.txt";

    FILE *fp = fopen(path, "r");
    if( fp == NULL ) {
        perror(path);
        return EXIT_FAILURE;
    }

    while( !feof(fp) ) {  /* THIS IS WRONG */
        /* Read and process data from file… */
    }
    if( fclose(fp) == 0 ) {
        return EXIT_SUCCESS;
    } else {
        perror(path);
        return EXIT_FAILURE;
    }
}
Run Code Online (Sandbox Code Playgroud)

这个__CODE__循环有什么问题?

c file while-loop feof

550
推荐指数
4
解决办法
21万
查看次数

如何在C++中读取和解析CSV文件?

我需要在C++中加载和使用CSV文件数据.此时它实际上只是一个以逗号分隔的解析器(即不用担心转义新行和逗号).主要需求是逐行解析器,每次调用方法时都会返回下一行的向量.

我发现这篇文章很有前途:http: //www.boost.org/doc/libs/1_35_0/libs/spirit/example/fundamental/list_parser.cpp

我从未使用过Boost的精神,但我愿意尝试.但只有在没有更直接的解决方案的情况下,我才会忽视.

c++ csv parsing text

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

标签 统计

c++ ×2

c ×1

caching ×1

csv ×1

feof ×1

file ×1

file-io ×1

parsing ×1

standard-library ×1

string ×1

text ×1

while-loop ×1