【问题标题】:ofstream creates a file but doesn't writeofstream 创建文件但不写入
【发布时间】:2017-05-10 23:09:58
【问题描述】:

这是代码,我尝试查找解决方案,但我尝试过的事情没有奏效。此外,检查文件是否打开的 main 底部的 if 语句失败并说它无法打开文件,但我不知道如何处理该错误。

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int row = 6;
const int col = 8;
void cellTemp (double check[][col], double);
int main()
{
    ifstream in;
    string input;
    ofstream out;
    string output;
    double grid[row][col];
    double t1, t2, t3, t4, tol;

    cout << "Enter input file name: \n";
    cin >> input;
    cout << "Enter output file name: \n";
    cin >> output;

    in.open(input.c_str());
    out.open(output.c_str());

    in >> t1 >> t2 >> t3 >> t4 >> tol;


    for(int i = 0; i < 6; i++)
    {
        for(int c = 0; c < 8; c++)
        {
            grid[i][c] = 0;
        }
    }

    //  Initializes top and bottom rows to 0
    for(int i = 0; i < 8; i++)
    {
        grid[0][i] = t1;
        grid[7][i] = t3;
    }
    //  Initializes left and right columns to 0
    for(int i = 1; i < 5; i++)
    {
        grid[i][0] = t4;
        grid[i][7] = t2;
    }

    cellTemp(grid, tol);
    if(out.is_open())
    {
        for(int i = 0; i < 6; i++)
        {
            for(int c = 0; c < 8; c++)
            {
                out << grid[i][c];
            }
            out << endl;
        }
        out.flush();
    }
    else
    {
        cout << "Cannot open file. \n";
    }
    out.close();
    in.close();
}
void cellTemp (double check[][col], double tolerance){

    double copy[row][col];
    double max = 0;
    double prev = 0;
    double prevMax;
    double prevTol = tolerance;

    tolerance = -1;
    while(prevMax > tolerance)
    {
        tolerance = prevTol;
        //  Copies the array before performing actions
        for(int i = 0; i < 6; i++)
        {
            for(int c = 0; c < 8; c++)
            {
                copy[i][c] = check[i][c];
            }
        }

        //  Sets cell values
        for(int i = 1; i < 5; i++)
        {
            for(int c = 1; c < 7; c++)
            {
                check[i][c] = (check[i-1][c] + check[i+1][c] +
                        check[i][c-1] + check[i][c+1]) / 4;
            }
        }

        for(int i = 1; i < 5; i++)
        {
            for(int c = 1; c < 7; c++)
            {
                prev = check[i][c] - copy[i][c];
                if(prev > max)
                {
                    max = prev;
                }
            }
        }
        prevMax = max;
        max = 0;
    }
}

【问题讨论】:

  • 你越界了:grid[7][i] = t3;.
  • 尝试使用路径对文件名进行硬编码并测试是否遇到相同的问题。
  • 这是什么,文件是按照您的标题打开/创建的,还是按照您的问题所说的?你想把输出文件放在哪里?
  • 感谢 cmets。 @Shibili 你的解决方案奏效了,我觉得忽略了界限很傻。
  • @Retired Ninja 对歧义感到抱歉。我使用的错误消息是“无法打开文件”。然而,该程序确实创建并打开了文件但没有保存,我猜是由于 Shibili 和 Paul Hashmi 提到的数组超出范围。

标签: c++ variable-assignment fstream ifstream ofstream


【解决方案1】:
grid[7][i] = t3;

你没有 [7] 行的伙伴认为你想要 grid[5][i] = t3; ?

【讨论】:

  • dshin没问题
猜你喜欢
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多