lundi 3 juillet 2017

ofstream crashing immediately on open, segmentation fault (core dumped), returned 139 (0x8B)

I want to preface this with the fact that I'm very early on in learning c++. I'm familiar with fileIO and objects/classes but still don't have a lot of tools, so please be dumb with me.

What I'm working on is a flash card program where I'm saving information to .txt documents and having it reference classes, section, and flash card questions/answers by a marker. I'm having an issue where when I attempt to write to one of the documents, upon opening it as an ofstream it crashes and wipes out the .txt document with it, and despite my best debugging...I can't figure out what the issue is as I don't believe I'm asking it to access anything that does not exist. Here's what my code looks like:

    string title;
    float section;
    cout << "Which section number would you like to add? (This can be a decimal)" << endl;
    cin >> section;
    cout << "What is the section title?" << endl;
    cin.ignore();
    getline (cin, title);

    ofstream Finder;
    Finder.open("flash.txt", ios::out);
    float f = 0;
    float F = 0;
    bool FindIt = false;
    for(int i = 0; i < V.size(); i++)
        {
            string line = "";
            string Line = "";
            string LINE = "";
            int j = i + 1;
            int k = i + 2;
            Finder << V[i] << "\n";


            if( (V[i].substr(0, 1) == "*")&&(V[i].substr(2, V[i].length() - 2) == Class) )
                {
                    FindIt = true;
                    if(V[j].substr(0, 1) == "$")
                        {
                            line = V[j].substr(2, V[j].length() - 2);
                            istringstream stream(line);
                            stream >> f;
                            if(section < f)
                                Finder << "$ " << section << " " << title << "\n";
                            else if(section == f)
                                {
                                    cout << "That section number already exists." << endl;
                                    i = V.size();
                                }
                        }

                    else if (V[j].substr(0,1) == "*")
                        Finder << "$ " << section << " " << title << "\n";
                }

            else if( (V[i].substr(0, 1) == "*")&&(V[i].substr(2, V[i].length() - 2) != Class) )
                FindIt = false;

            cout << FindIt;
            if( (FindIt == true)&&(V[i].substr(0,1) == "$")&&(V[j].substr(0,1) == "$") )
                {
                    line = V[i].substr(2, V[i].length() - 2);
                    istringstream stream(line);
                    stream >> f;
                    Line = V[j].substr(2, V[i].length() - 2);
                    istringstream stream2(Line);
                    stream2 >> F;
                    if((section > f)&&(section < F))
                        {
                            Finder << "$ " << section << " " << title << "\n";
                        }

                    else if( (section == f)||(section == F) )
                            {
                                cout << "That section number already exists." << endl;
                                i = V.size();
                            }
                }


            else if( (FindIt == true)&&(V[i].substr(0,1) == "$")&&(V[j].substr(0,1) == "*") )
                {
                    line = V[i].substr(2, V[i].length() - 2);
                    istringstream stream(line);
                    stream >> f;
                    if(section > f)
                        Finder << "$ " << section << " " << title << "\n";

                    else if(section == f)
                            {
                                cout << "That section number already exists." << endl;
                                i = V.size();
                            }
                    cout << "e" << endl;
                }


            else if( (FindIt == true)&&(V[i].substr(0,1) == "$")&&( (V[j].substr(0, 1) == "@")||(V[j].substr(0, 1) == "%") ) )
                {
                    while( (V[j].substr(0,1) != "$")||(V[j].substr(0,1) != "*") )
                          {
                              i++;
                              Finder << V[i] << "\n";
                          }

                    if(V[j].substr(0,1) == "*")
                        Finder << "$ " << section << " " << title << "\n";

                    else if(V[j].substr(0,1) == "$")
                        {
                            line = V[j].substr(2, V[i].length() - 2);
                            istringstream stream(line);
                            stream >> F;
                            if(section < F)
                                Finder << "$ " << section << " " << title << "\n";

                            else if(section == F)
                            {
                                cout << "That section number already exists." << endl;
                                i = V.size();
                            }
                        }
                    cout << "f" << endl;
                }

        }
    Finder.close();
}

Thanks in advance for your help, I can include the rest of the code for the function if it is necessary.

Aucun commentaire:

Enregistrer un commentaire