使用文件 I/O C++ 时的分段错误 11

我在 Mac 上遇到了 Code Blocks 和 Xcode 的问题.每次在代码块上运行代码时,我都会收到分段错误 11,当我尝试使用 Xcode 时,我会收到线程 1:exc_bad_access (code=1 address=0x0xffffffff0000000a).但是,如果我通过代码块在 PC 上运行此代码,它就会运行.有谁知道如何解决这个问题,以便我可以在我的 Mac 上运行该程序.

I am having an issue with Code Blocks and Xcode on my Mac. Every time I run the code on Code Blocks I receive Segmentation Fault 11 and when I try on Xcode I receive Thread 1: exc_bad_access (code=1 address=0x0xffffffff0000000a). However, if I run this code on a PC via Code Blocks it runs. Does anybody know how to solve this so I can run the program on my Mac.

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

struct Book
{
 string isbn;
 string title;
 string author;
 double price;
 string seller;
};

int main()
{
  Book booklist[100];
  ifstream infile;
  ofstream outfile;

  //int numBooks = 0;
  int i=0;
  char dummy;

  outfile.open("readBooks.txt");
  infile.open("usedBooks.txt");

  if (infile.fail())
  {
     cout << "The file doesn't exist";
     exit(-1);
  }

 else
 {
    //for (i=0; i<100; i++)
    while (!infile.eof())
    {
        getline (infile, booklist[i].isbn);
        getline (infile, booklist[i].title);
        getline (infile, booklist[i].author);
        infile >> booklist[i].price;
        infile.get(dummy);
        getline (infile, booklist[i].seller);

        outfile << "ISBN: " << booklist[i].isbn << endl;
        outfile << "Title: " << booklist[i].title << endl;
        outfile << "Author: " << booklist[i].author << endl;
        outfile << "Price: " << booklist[i].price << endl;
        outfile << "Seller: " << booklist[i].seller << endl << endl;
        i++;
    }
  }
}

推荐答案

您的以下行导致了问题

 while (!infile.eof())

因为它取决于文件的大小,但是您的数组大小是固定的(即 100 是您存储数据的位置).

As it depends on what is the size of the file, however your array size is fixed and(.i.e. 100 where you are storing the data).

你应该在你的程序中使用 std::vector 作为:

You should use std::vector in your program as :

std::vector<Book> booklist;

 while(/*check about infile>>x instead of !infile.eof()*/) {
        Book tmp;
        getline (infile, tmp.isbn);
        getline (infile, tmp.title);
        getline (infile, tmp.author);
        infile >> tmp.price;
        infile.get(dummy);
        getline (infile, tmp.seller);

        outfile << "ISBN: " << tmp.isbn << endl;
        outfile << "Title: " << tmp.title << endl;
        outfile << "Author: " << tmp.author << endl;
        outfile << "Price: " << tmp.price << endl;
        outfile << "Seller: " << tmp.seller << endl << endl;
        //Add into the vector after filling the data into the 
        //struct Book instance tmp
        booklist.push_back(tmp);
    }

编辑是的,我不应该检查 infile.eof() 因为这不是识别文件结尾的可靠方法.我们应该检查 infile>>x 而不是这个.有关更多信息,请参阅 ISOCPP 常见问题解答链接:http://isocpp.org/wiki/faq/input-output#istream-and-eof

EDIT Yes, I we should not check the infile.eof() as this is not reliable way to identify the end of file. Instead of this we should check infile>>x. For more information please refer ISOCPP FAQ link on this: http://isocpp.org/wiki/faq/input-output#istream-and-eof

相关文章