getline导致C 中的分段故障



我当前正在尝试学习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

任何帮助都将不胜感激

我感到很恶心,我没有马上看到这个。

int numLines = lineCount(inFile);

返回文件中正确的行数。bug不在这里,

std::string data[numLines];

不是犹太洁食C ,而是在文件中为文件中的每一行创建一个带有元素的数组。您的程序正在运行,因此得到了支持。不过,更喜欢使用库容器。

同时在readFile ...

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

将尝试将一行读取到data[i]中。读取是否成功,必须有一个data[i]要阅读。上次尝试不会。

逻辑

  1. 在第1行中阅读。成功,
  2. 在第2行中阅读。成功,
  3. 在第3行中阅读。成功,
  4. 在第4行中阅读。失败。但这并不能防止getline查看data的末端string和繁荣(特别是表现为行动繁荣的未定义行为),因为没有一个。

正确的解决方案

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++;
    }
}

no vector允许解决方案

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++;
    }
}

相关内容

  • 没有找到相关文章

最新更新