Segmentation Fault 11 When Using File I/O C++(使用文件 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
这篇关于使用文件 I/O C++ 时的分段错误 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用文件 I/O C++ 时的分段错误 11
基础教程推荐
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
