mardi 24 septembre 2019

Ofstream creates just empty file but not writing

I want my arrays to be printed as "testOutput.txt" but when I run the code, it just creates empty file and does not write.

Also, my code makes Segmentation Fault, which I strongly suspect causing the problem now. But I have no idea why it happens, and why there were no error when I tested with std::cout.

I tested those data with std::cout instead of ofstream fout, and it worked well as I expected.

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>

    /*
        Functions are defined here
    */

    int main(int argc, char *argv[])
    {
        int i, j, k, len1, len2, len3, max1, max2, min1, min2, index, input4, input5, input6, c, theDeleted, pos0;
        int* arr1;
        int* arr2;
        int* arr3;
        int* duplicate;

        std::string line;
        std::string test;
        std::string errorMessage = "Error: It is not a numeric input.\n";
        std::stringstream ss;

        std::ifstream fin("testInput.txt");

        std::getline(fin, line);
        ss.str(line);
        ss >> test;
        if (isNumeric(test))
        {
            len1 = std::stoi(test);
            arr1 = new int[len1];
        }
        else
        {
            std::cout << errorMessage;
            return 0;
        }
        ss >> test;
        if (isNumeric(test))
        {
            len2 = std::stoi(test);
            arr2 = new int[len2];
        }
        else
        {
            std::cout << errorMessage;
            return 0;
        }

        std::getline(fin, line);
        ss.clear();
        ss.str(line);
        for (i = 0; i < len1; i++)
        {
            ss >> test;
            if (isNumeric(test))
            {
                arr1[i] = std::stoi(test);
            }
            else
            {
                std::cout << errorMessage;
                return 0;
            }
        }

        std::getline(fin, line);
        ss.clear();
        ss.str(line);
        for (i = 0; i < len2; i++)
        {
            ss >> test;
            if (isNumeric(test))
            {
                arr2[i] = std::stoi(test);
            }
            else
            {
                std::cout << errorMessage;
                return 0;
            }
        }

        ss.clear();
        fin >> input4 >> input5 >> input6;

        if (fin.is_open())
            fin.close();

        std::ofstream fout("testOutput.txt");

        max1 = maxElement(arr1, len1);
        max2 = maxElement(arr2, len2);
        min1 = minElement(arr1, len1);
        min2 = minElement(arr2, len2);
        fout << "1)\narr1: " << max1 << "\narr2: " << max2 << "\n";
        fout << "2)\narr1: " << min1 << "\narr2: " << min2 << "\n";
        fout << "3)\narr1: " << min1 << " " << max1 << "\narr2: " << min2 << " " << max2 << "\n";

        fout << "4)\n";                                                                             //4
        sortAscBub(arr1, len1);
        sortAscBub(arr2, len2);
        fout << "arr1: ";
        for (i = 0; i < len1; i++)
            fout << arr1[i] << " ";
        fout << "\narr2: ";
        for (i = 0; i < len2; i++)
            fout << arr2[i] << " ";
        fout << "\n";

        fout << "5)\n";                                                                             //5
        sortDscSel(arr1, len1);
        sortDscSel(arr2, len2);
        fout << "arr1: ";
        for (i = 0; i < len1; i++)
            fout << arr1[i] << " ";
        fout << "\narr2: ";
        for (i = 0; i < len2; i++)
            fout << arr2[i] << " ";
        fout << "\n";

        fout << "6)\narr3: ";                                                                           //6
    //  merge(arr1, arr2, arr3, len1, len2, len3);
        i = len1 - 1, j = len2 - 1, k = 0;
        len3 = len1 + len2;
        arr3 = new int[len3];
        while (k < len3)
        {
            if (i < 0)
                arr3[k++] = arr2[j--];
            else if (j < 0)
                arr3[k++] = arr1[i--];
            else if (arr1[i] < arr2[j])
                arr3[k++] = arr1[i--];
            else
                arr3[k++] = arr2[j--];
        }
        for (i = 0; i < len3; i++)
            fout << arr3[i] << " ";
        fout << "\n";
        delete[] arr1;
        delete[] arr2;

        index = searchElement(input4, arr3, len3);
        fout << "7)\nIndex: " << index << "\n";                                                     //7

    //  removeDupe(arr3, len3, duplicate, c);
        j = 0, c = 0;
        int check = -1;
        int* temp1;
        int* temp2;
        temp1 = new int[len3];
        temp2 = new int[len3];
        for (i = 0; i < len3; i++)
        {
            if (check == arr3[i])
            {
                if (c=0)
                {
                    temp2[c++] = check;
                }
                else if (check != temp2[c - 1])
                {
                    temp2[c++] = check;
                }
            }
            else
            {
                temp1[j++] = arr3[i];
                check = arr3[i];
            }
        }
        len3 = len3 - c;
        for (i=0; i< len3; i++)
            arr3[i] = temp1[i];
        for (i=0; i<c; i++)
            duplicate[i] = temp2[i];
        delete[] temp1;
        delete[] temp2;
        fout << "8)\nDuplicate Elements: ";                                                         //8
        for (i = 0; i < c; i++)
            fout << duplicate[i] << " ";
        fout << "\narr3: ";
        for (i = 0; i < len3; i++)
            fout << arr3[i] << " ";
        fout << "\n";

        theDeleted = delIndex(input5, arr3, len3);
        fout << "9)\nElement: " << theDeleted << "\narr3: ";                                            //9
        for (i = 0; i < len3; i++)
            fout << arr3[i] << " ";
        fout << "\n";

        pos0 = searchRecur(input6, arr3, 0, len3);
        insert0(pos0, arr3, len3);
        fout << "10)\narr3: ";                                                                          //10
        for (i = 0; i < len3; i++)
            fout << arr3[i] << " ";
        fout << "\n\n";

        delete[] arr3;
    //  delete[] duplicate;

        fout.flush();

        if (fout.is_open())
            fout.close();

        return 0;
    }

I expect the output file with few numbers in it, but the file is empty.

Aucun commentaire:

Enregistrer un commentaire