【问题标题】:C++ Program crashes while reading from file using fstream fin使用 fstream fin 从文件读取时 C++ 程序崩溃
【发布时间】:2014-04-08 04:45:31
【问题描述】:

编程课的最终项目将于明天到期,感谢任何帮助,在接受文件名后,此模块中的程序崩溃。崩溃是指它输出“此应用程序已请求运行时以不寻常的方式终止它”,然后是通常的窗口"CotD.exe has stopped working"

void load(vector<Fish>& stock)
{
    char c;
    do {
        cout << "Welcome to Catch of The Day, enter (f) to choose a file to load from, otherwise enter anything else to load from default file.\n";
        cin >> c;
        if (c == 'f' || c == 'F')
        {
            cout << "Enter file name\n";
            cin >> file;
        }
        ifstream fin(file.c_str());
        if (fin.fail())
        {
            cout << "Could not open " << file << " Check the directory location of CotD.exe and try again\n";
        }
        else
        {
            while (!fin.eof())
            {
                Fish f;
                string blank;
                fin >> f.amt;
                fin >> f.prc;
                fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                getline(fin, blank);
                stock.push_back(f);
            }
            fin.close();
            break;
        }
    } while (true);
}

编辑其他相关代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;
//
string file = "default.txt"; //Global variable used to store name of save file. 
//It is global so that load() and save() can both access it.
struct Fish
{
    string type;
    double amt;
    double prc;
    double val;
};
void addType(vector<Fish>&);
void editStock(vector<Fish>&); 
void sortBy(vector<Fish>&);
void sortAsc(vector<Fish>&,char);
void sortDesc(vector<Fish>&,char);
void display(vector<Fish>&);
int search(vector<Fish>&);
void save(vector<Fish>&);
void load(vector<Fish>&);
string getType();
int dispType(string,vector<Fish>&);
int find(string,vector<Fish>&);
double getAmt();
void delType(string,vector<Fish>&);
void menu(vector<Fish>&);
double getPrc();

int main(){
    std::vector<Fish> stock;
    load(stock);
    menu(stock);
    save(stock);
    cout<<endl<<endl
        <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
        <<"|Thank you for using Catch of the Day|\n"
        <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    system("Pause");
    return 0;

}

我最近写了这个程序,看起来和我很相似,运行完美我看不出有什么区别:

void load(vector<string>& names)
{
    string file, name, bad;
    while (true)
    {
        cout << "Input file name\n";
        getline(cin, file);
        ifstream fin(file.c_str());
        if (fin.fail())
        {
            cout << "Could not open " << file << ", try again.\n";
        }
        else break;
    }
    ifstream fin(file.c_str());
    while (!fin.eof())
    {
        fin >> bad;
        fin >> name;
        cout << "\"" << name << "\"" << endl;

    }
    system("Pause");
    fin.close();

    ifstream fin(file.c_str());
    while (!fin.eof())
    {
        getline(fin, name);
        names.push_back(name);
    }
    system("Pause");
    fin.close();
    cout << "Names added to list\n";
}

【问题讨论】:

  • 您应该测试您的流输入是否有效(例如 if (std::cin >> file) ... else ...),而不是像这样测试 eof - eof 仅在 a 之后设置操作失败。有数百个关于此的 stackoverflow 问答 - 例如。 stackoverflow.com/questions/21647/… 另外,学习调试器,这样你就知道哪一行失败了,变量内容是什么。
  • 其余代码在哪里? file 是什么? Fish 长什么样子?

标签: c++ crash ifstream


【解决方案1】:

我已经编辑了你的代码,这是我得到的:

    void load(vector<Fish>& stock)
  {
char c;
do {
    cout << "Welcome to Catch of The Day, enter (f) to choose a file to load from, otherwise enter anything else to load from default file.\n";
    cin >> c;
    if (c == 'f' || c == 'F')
    {
        cout << "Enter file name\n";
        cin >> file;
    }
    ifstream fin(file.c_str());
    if (fin.fail())
    {
        cout << "Could not open " << file << " Check the directory location of CotD.exe and try again\n";
    }
    else
    {
        Fish f;
        string blank;
        if (fin>>f.amt)
        {
           if (fin>>f.prc)
           {
            getline(fin,blank);
            stock.pushback(f);
           }
        }

        fin.close();
        break;
    }
} while (true);
    }

当然,这是不知道文件中的内容以及Fish到底是什么,所以我不知道这是否是您要查找的内容。

编辑:如果您可以包含该文件,或者只是一个“鱼”的一部分,因为我认为这就是文件的内容,那么提供帮助会容易得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多