相关疑难解决方法(0)

通过ifstream打开文件时如何共享文件删除权限

我想制作一个在打开时可以删除的文件ifstream

我知道使用 Windows API 很容易: CreateFile

CreateFile(...,FILE_SHARE_DELETE,...)
Run Code Online (Sandbox Code Playgroud)

但是当我测试通过 ifstream 打开文件时。

一打开就删不掉

我没有找到任何关于设置属性的文档,比如FILE_SHARE_DELETEon ifstream

我该怎么做才能解决这个问题?

c++ windows winapi file

5
推荐指数
1
解决办法
458
查看次数

c ++如何创建包含大小未初始化字节的std :: string?

我需要一个 std::stringsize字节,在构造它之后,我将在读取之前写入该字符串中的每个字节,因此空初始化字符串是浪费 cpu,这是有效的:

std::string s(size,0);
Run Code Online (Sandbox Code Playgroud)

但它只是有点浪费,它基本上就像calloc()在我需要的时候使用一样malloc(),所以问题是,我如何构造一个 X 未初始化字节的字符串?

(使用 Reserve()+push 不是一种选择,因为我将字符串提供给 C-api 以char*,size进行实际初始化)

编辑:此线程似乎与相同的问题/相关(但使用向量而不是字符串):C++11 中的值初始化对象和 std::vector 构造函数

c++ optimization std stdstring

5
推荐指数
1
解决办法
294
查看次数

将文件读入 std::string 的最有效方法是什么?

我目前这样做,最后转换为 std::string 需要 98% 的执行时间。一定会有更好的办法!

std::string
file2string(std::string filename)
{
    std::ifstream file(filename.c_str());
    if(!file.is_open()){
        // If they passed a bad file name, or one we have no read access to,
        // we pass back an empty string.
        return "";
    }
    // find out how much data there is
    file.seekg(0,std::ios::end);
    std::streampos length = file.tellg();
    file.seekg(0,std::ios::beg);
    // Get a vector that size and
    std::vector<char> buf(length);
    // Fill the buffer with the size
    file.read(&buf[0],length);
    file.close();
    // return buffer as string
    std::string s(buf.begin(),buf.end());
    return s;
}
Run Code Online (Sandbox Code Playgroud)

c++ string file-io

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

使用istream从命名管道读取

是否可以使用流使用c ++(stl)从命名管道(mkfifo)读取 - 因此没有事先char *buffer[MAX_SIZE]为读取操作定义?

我想读取,直到缓冲区结束并将结果放入std::string.

(当前方法:bytes = read(fd, buffer, sizeof(buffer));需要提前分配某种缓冲区.)

c++ linux named-pipes

3
推荐指数
1
解决办法
3487
查看次数

从函数返回后字符串似乎被释放

我目前正在尝试创建一个简单的函数来读取文件的内容并将其存储到 astd::stringchar数组中。我遇到了这样的问题:数据在函数内完全读取,但在调用函数时似乎被删除/释放。

\n

我已将这个问题隔离到一个小程序中:

\n

入口.cpp

\n
#include <string>\n#include <fstream>\n#include <iostream>\n\nconst char* ReadFile(const std::string& path) {\n\n    std::ifstream file;\n\n    try {\n        file.exceptions(std::ifstream::failbit | std::ifstream::badbit);\n        file.open(path);\n\n        if (!file.is_open()) {\n            throw;\n        }\n    }\n    catch (std::exception e) { //file does not exist\n        std::string errmsg = "Unable to open file at path: " + path;\n\n        std::cout << errmsg << std::endl;\n\n        throw new std::exception(errmsg.c_str());\n    }\n\n\n    std::string contents;\n    try {\n        contents = std::string{ std::istreambuf_iterator<char>{file}, {} };\n    }\n    catch (const std::ifstream::failure& e) {\n\n        std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ fstream lifetime ifstream

3
推荐指数
1
解决办法
102
查看次数

fstream到const char*

我想要做的是读取一个名为"test.txt"的文件,然后让文件的内容为const char*类型.怎么会这样做?

c++ fstream

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

Boost字符串替换不会用字符串替换换行符

我正在为我的libspellcheck拼写检查库创建一个函数来检查文件的拼写.它的功能是读取文本文件并将其内容发送到拼写检查功能.为了使拼写检查功能正确处理文本,必须用空格替换所有换行符.我决定为此使用boost.这是我的功能:

spelling check_spelling_file(char *filename, char *dict,  string sepChar)
{

    string line;
    string fileContents = "";
    ifstream fileCheck (filename);
    if (fileCheck.is_open())
    {
        while (fileCheck.good())
            {
                getline (fileCheck,line);
            fileContents = fileContents + line;
        }

        fileCheck.close();
    }
    else
    {
        throw 1;
    }

    boost::replace_all(fileContents, "\r\n", " ");
    boost::replace_all(fileContents, "\n", " ");

    cout << fileContents;

    spelling s;
    s = check_spelling_string(dict, fileContents, sepChar);

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

在编译库之后,我创建了一个带有示例文件的测试应用程序.

测试应用代码:

#include "spellcheck.h"

using namespace std;

int main(void)
{
    spelling s;
    s = check_spelling_file("test", "english.dict",  "\n");

    cout << "Misspelled …
Run Code Online (Sandbox Code Playgroud)

c++ string boost

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

使用 C++ 读取大型(~1GB)数据文件有时会抛出 bad_alloc,即使我有超过 10GB 的可用 RAM

我正在尝试读取大小为 ~1.1GB 的 .dat 文件中包含的数据。因为我是在 16GB RAM 的机器上执行此操作,所以我认为将整个文件一次读入内存不会有问题,只有在处理它之后。

为此,我使用了slurp这个 SO answer 中找到的函数。问题是代码有时(但并非总是)抛出 bad_alloc 异常。查看任务管理器,我发现总有至少 10GB 的可用内存可用,所以我不知道内存会是什么问题。

这是重现此错误的代码

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    ifstream file;
    file.open("big_file.dat");
    if(!file.is_open())
        cerr << "The file was not found\n";

    stringstream sstr;
    sstr << file.rdbuf();
    string text = sstr.str();

    cout << "Successfully read file!\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

什么可能导致这个问题?避免它的最佳做法是什么?

c++ file-io bad-alloc

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

无法将文件读入char数组(一些指针问题,总是如此)

是的,我有这样的问题,因为我总是很难按照指针操作.所以,我有这个简单的代码:

struct myfile {
    char* name;
    char* content;
    long size;
};

myfile this_file;

int main() {
    read();
    return 0;
}

void read() {
    output("Please, specify file name: ");
    cin >> (this_file.name = new char);
    FILE *stream;
    stream = fopen(code.name, "r");
    if (stream != NULL) {
        fseek(stream , 0, SEEK_END);
        myfile.size = ftell(codefile);
        myfile.content = new char[myfile.size];
        fseek(myfile, 0, SEEK_SET);
        if ((fread(myfile.content, 1, myfile.size, stream)) == 0) {
            fclose(codefile);
            cout << "File is empty!\n");
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

它正确获取文件的名称,它获取内容的大小但是当试图为content成员分配空间时程序崩溃,我知道这是一些指针问题,但是,一如既往,不记得/弄清楚是什么是吗.到达此行时崩溃:myfile.content …

c++ arrays crash pointers char

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

在C++中将文本文件粘贴到std :: string的数组/集合中的最佳方法是什么?

C++中将整个文件读入std :: string的最佳方法什么?我想问一个类似的问题,除了我希望我的输出是一个包含文本文件中的行的数组/ STL容器.

C#/ .net有相当有用的File.ReadAllLines()实用程序函数,好的C++(STL)版本会是什么样子?

c++ stl

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