Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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++_File_Vector - Fatal编程技术网

C++ 读取文件并将其写入字符向量时出现的分段错误

C++ 读取文件并将其写入字符向量时出现的分段错误,c++,file,vector,C++,File,Vector,下面是我编写的一段代码,用于读取文件并将其存储在char向量中 #include <fstream> #include "Graph.hpp" #include <iostream> using std::ifstream; using std::vector; using std::string; using std::cout; using std::endl; static int const WIDTH = 50; vector<char>* r

下面是我编写的一段代码,用于读取文件并将其存储在char向量中

#include <fstream>
#include "Graph.hpp"
#include <iostream>

using std::ifstream;
using std::vector;
using std::string;
using std::cout;
using std::endl;

static int const WIDTH = 50;

vector<char>* read_file(ifstream*);

int main(){

    ifstream file;
    vector<char>* buf;
    file.open("myfile");
    if(file.is_open()){ 
        buf = read_file(&file);
    }


//  Graph graphObj;
  //  graphObj.populateGraph(buffer);   
}

vector<char>* read_file(ifstream* refFile){
    vector<char>* buffer = new vector<char>();
    int pos = 0;  

    while(!(refFile->eof())){
         refFile->read((((char*)(buffer))+pos),WIDTH); 
         pos += WIDTH;  // update the pos with the number of characters read earlier

 }      
    return buffer;
}
#包括
#包括“Graph.hpp”
#包括
使用std::ifstream;
使用std::vector;
使用std::string;
使用std::cout;
使用std::endl;
静态整型常数宽度=50;
矢量*读取文件(ifstream*);
int main(){
ifstream文件;
向量*buf;
打开(“myfile”);
如果(file.is_open()){
buf=读取文件(&U文件);
}
//图形;
//人口图(缓冲区);
}
矢量*读取文件(ifstream*REF文件){
向量*缓冲区=新向量();
int pos=0;
而(!(refFile->eof()){
REF文件->读取(((字符*)(缓冲区))+pos),宽度);
pos+=WIDTH;//使用前面读取的字符数更新pos
}      
返回缓冲区;
}

代码可以编译,但是由于我不清楚的原因,我得到了一个分段错误。有人能帮我解释为什么我会出现seg故障吗?

您对
std::vector
的处理需要更新

  • 您尚未在
    std::vector
    中分配任何内存来保存任何项目

  • 您使用的是
    buffer
    ,就好像它是指向
    char
    s数组的指针一样

      refFile->read((((char*)(buffer))+pos),WIDTH);
    
    如上所述,将
    buffer
    类型转换为
    char*
    ,是导致未定义行为的原因

  • 您可以通过一次读取一个字符并将其添加到
    std::vector
    中,或读取一个字符数组并将其添加到
    std::vector
    中来解决poth问题

  • 第一种方法

    vector<char>* read_file(ifstream* refFile){
       vector<char>* buffer = new vector<char>();
       int pos = 0;  
    
       int c;    
       while( (c = refFile->get()) != EOF ){
          buffer->push_back(static_cast<char>(c));    
       }      
       return buffer;
    }
    

    现在,您不必担心
    delete
    ing
    buf

    您是否应该使用
    buffer->push_back()
    ?在任何情况下,如果您希望每个条目存储超过1个
    char
    字符,则此向量作为
    char
    向量都有问题。您明显违反了别名。是什么让你相信将
    std::vector*
    转换成
    char*
    是个好主意?也许留着吧。你好像是C出身。是的,vector是连续存储,但它必须被分配,你不能在任何地方这样做。此外,它是一个不能简单地转换为char*的类。另外,不要使用原始指针。只需通过值返回向量,通过引用返回ifstream。我没有在函数中使用“new”关键字分配内存吗?是的,但只针对向量实例。不是为了储存。此外,您不应该在这里使用
    new
    ,而只需在堆栈上分配它并按值返回。当我分配了new关键字时,您为什么说我没有为向量分配内存?@DesirePRG,因为您没有。仅仅因为您(不必要地)
    new
    -ed了一个
    std::vector
    ,并不意味着向量本身在其中分配了一个空间。一个微不足道的例子<代码>结构S{char*p;};S*S=新的S我们只是分配了一个动态S。这并不意味着
    S->p
    神奇地指向有效数据。
    vector<char>* read_file(ifstream* refFile){
       vector<char>* buffer = new vector<char>();
       char temp[WIDTH];
       while( (refFile->read(temp, WIDTH))){
          std::streamsize count = refFile->gcount();
          for (std::streamsize i = 0; i < count; ++i ) {
             buffer->push_back(temp[i]);
          }      
       }      
       return buffer;
    }
    
    vector<char> read_file(ifstream& refFile){
       vector<char> buffer;
       int pos = 0;  
    
       int c;    
       while( (c = refFile.get()) != EOF ){
          bufferpush_back(static_cast<char>(c));    
       }      
       return buffer;
    }
    
    int main(){
    
        ifstream file;
        vector<char> buf;
        file.open("myfile");
        if(file.is_open()){ 
            buf = read_file(file);
        }
    }