开发者社区> 问答> 正文

C++的Ofstream仅创建空文件但不写入是为什么?

我希望将数组打印为“ testOutput.txt”,但是当我运行代码时,它只会创建一个空文件并且不会写入。

另外,我的代码产生了Segmentation Fault,我非常怀疑现在是引起该问题的原因。但是我不知道为什么会发生,为什么我在用std :: cout测试时没有错误。

我用std :: cout而不是stream fout测试了这些数据,并且效果很好。

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

我希望输出文件中包含几个数字,但该文件为空。

展开
收起
被纵养的懒猫 2019-09-25 15:17:51 3963 0
2 条回答
写回答
取消 提交回答
  • 在 fout.flush() / fout.close / fout 析构之前,不保证内容已经写入文件,而你在后面的程序执行过程中出现了段错误,因些文件就为空了,解决掉段错误,或者每次 fout << 之后都加 fout.flush() 就好

    2020-03-16 15:28:18
    赞同 展开评论 打赏
  • 精于基础,广于工具,熟于业务。

    建议把 ss.clear();这个语句去掉试试。

    2019-09-25 16:27:45
    赞同 展开评论 打赏
问答分类:
C++
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载