getline在C ++中导致分段错误?

问题描述 投票:-1回答:1

我目前正在尝试学习C ++。我得到一个.txt文件,每行包含不同的个人数据。我想将这些数据读入一个字符串数组。我没有看到这个功能有什么问题,之前我做过同样的事情,但由于某种原因我收到了分段错误。

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

void readFile(std::istream&, std::string*);
int lineCount(std::istream&);

int main(){
    std::ifstream inFile("input.txt");
    int numLines = lineCount(inFile);
    std::string data[numLines];
    inFile.close();
    inFile.open("input.txt");
    readFile(inFile, data);
    inFile.close();
    return 0;
}

int lineCount(std::istream& inFile){
    std::string line;
    int numLines = 0;
    while(std::getline(inFile, line)){
        numLines++;
    }
    return numLines;
}

void readFile(std::istream& inFile, std::string *data){
    int i = 0;
    while(std::getline(inFile, data[i])){
         std::cout << i << "\n"; //testing values
         std::cout << data[i] << "\n"; //testing values
         i++;
    }
}

这是上面代码的输出。

//Output
//Note, these are fictional people
0
Florence,Forrest,1843 Glenview Drive,,Corpus Christi,TX,78401,10/12/1992,5/14/2012,3.215,127/11/1234,2.5,50
1
Casey,Roberta,3668 Thunder Road,,Palo Alto,CA,94306,2/13/1983,5/14/2014,2.978,95
2
Koch,Sandra,2707 Waterview Lane,Apt 302,Las Vegas,NM,87701,6/6/1972,12/14/2015,2.546,69
Segmentation fault //occurs in while condition

任何帮助,将不胜感激

c++ segmentation-fault getline
1个回答
2
投票

我感到恶心,我没有立刻看到这一点。

int numLines = lineCount(inFile);

返回文件中正确的行数。虫子不在这里,伙计。

std::string data[numLines];

不是kosher C ++,但如果支持,将创建一个包含文件中每一行元素的数组。您的程序正在运行,因此它受支持。不过,更喜欢使用Library Containers

同时在readFile ......

while(std::getline(inFile, data[i]))

将尝试读入一行到data[i]。无论阅读成功与否,都必须有一个data[i]来阅读。最后的尝试不会有。

逻辑是这样的

  1. 读入第1行。成功,所以
  2. 在第2行读。成功,所以
  3. 在第3行读。成功,所以
  4. 在第4行读。失败。但这并没有让getline看不到data的结尾为string和繁荣(特别是undefined behaviour,表现为繁荣),因为没有一个。

The right solution

int main(){
    std::ifstream inFile("input.txt");
    // no longer need. Vector keeps track for us
    // int numLines = lineCount(inFile);
    std::vector<std::string> data;
    // read nothing from file. Don't need to rewind
    readFile(inFile, data);

    // note: files close themselves when they are destroyed.
    //inFile.close(); 
    return 0;
}
void readFile(std::istream& inFile, std::vector<std::string> & data){
    int i = 0;
    std::string line; // line to read into. Always there, so we don't have to worry.
    while(std::getline(inFile, line)){
         std::cout << i << "\n"; //testing values
         std::cout << line << "\n"; //testing values
         data.push_back(line); // stuff line into vector.
         i++;
    }
}

The No vector Allowed Solution

int main(){
    std::ifstream inFile("input.txt");
    int numLines = lineCount(inFile);
    // legal in every C++, but prefer container may want some extra armour 
    // here to protect from numlines 0. 
    std::string * data = new std::string[numlines]; 
    // the following is a faster way to rewind a file than closing and re-opening
    inFile.clear(); // clear the EOF flag
    inFile.seekg(0, ios::beg); // rewind file.
    readFile(inFile, data);
    inFile.close();
    return 0;
}
void readFile(std::istream& inFile, std::string * data){
    int i = 0;
    std::string line; // same as above. line is here even if data[i] isn't
    while(std::getline(inFile, line)){
         std::cout << i << "\n"; //testing values
         std::cout << line << "\n"; //testing values
         data[i] = line; // stuff line into array. Smart compiler may realize it can move
                         //if not, c++11 adds a formal std::move to force it.
         i++;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.