分段故障11使用文件I / O C ++时 [英] Segmentation Fault 11 When Using File I/O C++

查看:106
本文介绍了分段故障11使用文件I / O C ++时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Mac上有一个关于Code Blocks和Xcode的问题。每次我在代码块上运行代码我收到分割故障11,当我尝试Xcode我接收线程1:exc_bad_access(代码= 1地址= 0x0xffffffff0000000a)。但是,如果我在一个PC上运行这个代码通过代码块运行。有人知道如何解决这个问题,所以我可以在我的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< 文件不存在;
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<< 作者:< booklist [i] .author< endl;
outfile<< Price:<< booklist [i] .price< endl;
outfile<< 卖方:< booklist [i] .seller< endl endl;
i ++;
}
}
}


解决方案>

您的以下行会导致问题

  while(!infile.eof())

因为它取决于文件的大小,但是你的数组大小是固定的,

您应该在程序中使用 std :: vector

  std :: vector< Book>书单

while(/ * check about infile>> x而不是!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<< 作者:< tmp.author<< endl;
outfile<< Price:<< tmp.price<< endl;
outfile<< 卖方:< tmp.seller<< endl endl;
//在将数据填充到
后添加到向量中// struct Book实例tmp
booklist.push_back(tmp);
}

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


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

解决方案

Your following line is causing the problem

 while (!infile.eof())

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).

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

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

这篇关于分段故障11使用文件I / O C ++时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆