Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将文本文件转换为常量字符指针_C++_Pointers_Io_Iostream - Fatal编程技术网

C++ 将文本文件转换为常量字符指针

C++ 将文本文件转换为常量字符指针,c++,pointers,io,iostream,C++,Pointers,Io,Iostream,我想写一个简单的函数,将文件名作为参数,然后返回一个常量char指针,该指针包含文本文件中的字符 #include <fstream> #include <vector> #include <iostream> #include "text.h" //function to get the size of a file unsigned int Get_Size(const char * FileName){ std::ifstream filesi

我想写一个简单的函数,将文件名作为参数,然后返回一个常量char指针,该指针包含文本文件中的字符

#include <fstream>
#include <vector>
#include <iostream>
#include "text.h"

//function to get the size of a file
unsigned int Get_Size(const char * FileName){
    std::ifstream filesize(FileName, std::ios::in|std::ios::ate);

    unsigned int SIZE = filesize.tellg();

    filesize.close();

    return SIZE;
}

//Takes a file name then turns it into a c-style character array
const char * Get_Text(const char * FileName){

    //get size of the file
    unsigned int SIZE = Get_Size(FileName);


    std::ifstream file(FileName, std::ios::in);

    //I used a vector here so I could initialize it with a variable
    std::vector<char> text(SIZE);

    //here is where I loop through the file and get each character
    //and then put it into the corresponding spot in the vector
    std::streampos pos;
    for(int i = 0; i<SIZE; i++){
            pos=i;
        file.seekg(pos);
        text[i] = file.get();
    }

    //I manually added the terminating Null character
    text.push_back('\0');

    //I set the pointer equal to the address of the first element in the vector
    const char * finalText = &text[0];

    file.close();

    //this works    
    std::cout<<finalText<<std::endl;

    return finalText;
};

int main(){

    //this does not work
    std::cout<<Get_Text("Text.txt")<<std::endl;

    return 0;
}
#包括
#包括
#包括
#包括“text.h”
//函数获取文件的大小
无符号int Get_Size(常量字符*文件名){
std::ifstream filesize(文件名,std::ios::in | std::ios::ate);
unsigned int SIZE=filesize.tellg();
filesize.close();
返回大小;
}
//获取文件名,然后将其转换为c样式的字符数组
常量字符*获取文本(常量字符*文件名){
//获取文件的大小
unsigned int SIZE=Get_SIZE(文件名);
std::ifstream文件(文件名,std::ios::in);
//我在这里使用了一个向量,所以我可以用一个变量初始化它
std::矢量文本(大小);
//这里是我循环遍历文件并获取每个字符的地方
//然后把它放到向量中对应的点上
std::streampos;

对于(inti=0;i您可以做一些有趣的事情并使用mmap()(假定为Linux)将文件映射到虚拟内存中。这将推迟从磁盘实际读取文件的时间点,并节省内存。它也适用于任意长度的文件。

您将返回指向向量基础元素数组的指针。但是,由于向量是局部变量,因此当函数
Get_Text
返回。自动变量超出范围时会被销毁,与它们相关的任何内容也会因此被销毁。返回向量本身或更好的字符串是更好的选择


如果必须返回
char*
,则使用
std::unique_ptr text(新字符[SIZE])
。返回
text.get()
时,在
main
中,您有责任调用
delete[]ptr

为什么要将其标记为
c
?哪个版本的c标准库提供
std::vector
?另外,您返回的数据指向向量内部,但是当函数返回时,向量会被破坏,因此会得到垃圾(和未定义的行为)。您最好将文件读入动态分配的缓冲区(
new string[size]
)并返回该缓冲区。更好的是,只需使用
std::strng
std::getline
std::string::operator+=/code>。