C ++使用fstream对象读取和写入文本文件时遇到问题

时间:2014-04-12 19:32:11

标签: c++ fstream

我是一名C ++新手,在完成作业时遇到了一些麻烦,我的老师下周不会出现,所以我不得不求助!我确定我只是累了并且遗漏了一些小东西,但是我无法获取fstream对象来创建文件,然后从中读取以打印到屏幕上,其他一切似乎都正常。

以下是作业的说明,非常简单和基本:

1 - 编写一个计算圆圈面积和圆周的程序。

2 - 从主输入通过键盘圆圈的半径并存储在一个数组中。这必须通过循环完成。假设最多100条记录。

3 - 使用上面的半径调用函数计算每个圆的周长并存储在另一个数组中。

4 - 调用另一个函数来计算圆的面积并存储在另一个数组中。

5 - 从主打印到屏幕显示圆的半径,周长和面积。应从3个阵列中的数据打印此信息。在打印实际数据之前,请打印“Radius”,“Circumference”和“Area”的标签,并在每个标签下对齐信息。

6 - 在main中为输出文件创建一个名为Lecture20Output.txt的fstream对象。

7 - 调用函数将上面数组中的半径,周长和面积写入Lab20Output.txt

8 - 从主打印到屏幕显示Lab20Output.txt。

的内容

9 - 样品运行:半径5,4,7。

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

// prototypes
void getCircumf(const double, double, double &);
void getArea(const double, double, double &);
void writeOutFile(int, double[], double[], double[], fstream &);
void greeting();

int main()
{
    const int ARRAY_SIZE = 100;
    const double PI = 3.14;
    int i = 0, i2 = 0;
    double radii[ARRAY_SIZE],
           circumf[ARRAY_SIZE],
           area[ARRAY_SIZE];
    fstream myFile;
    string line;

    // use loop to prompt user for radii
    cout << "==============================================================" << endl;
    cout << "  Below, you may enter all of your radii (up to 100 entries)  " << endl;
    cout << "         *** Enter 0 (zero) when you are finished ***         " << endl;
    cout << "==============================================================" << endl;
    while (i<100)
    {
        cout << "\nEnter your radius: ";
        cin >> radii[i];
        if (radii[i] == 0)          // test if user has no more entries
            i = 100;
        else
        {
            getCircumf(PI, radii[i], circumf[i]);   // call function to calculate circumference
            getArea(PI,radii[i], area[i]);          // call function to calculate area
            i++;
            i2++;
        }
    }

    // print results table to screen
    cout << "\n======================================="
         << "\n| Radius | Circumference |    Area    |"
         << "\n=======================================" << endl;
    for (int i=0; i<i2; i++)
    {
        cout << fixed << setprecision(2);
        cout << "| "
             << setw(6) << radii[i]
             << " | "
             << setw(13) << circumf[i]
             << " | "
             << setw(10) << area[i]
             << " |" << endl;
        cout << "---------------------------------------" << endl;
    }

    // call function to print results table to output file
    myFile.open("Lab20Output.txt", ios::out | ios::in);
    if (!myFile)
    {
        cout << "FILE OPEN ERROR!" << endl;
        return 0;
    }
    cout << "\nWe are now writing this data to a file...";
    writeOutFile(i2,radii,circumf,area,myFile);
    cout << "done." << endl;

    // print to screen the contents of file "Lab20Output.txt"
    cout << "\nNow we will read back the data from the file..." << endl;
    while (getline(myFile, line))
    {
        cout << line << '\n';
    }

    myFile.close();
    greeting();
    return 0;
}

// function definitions

void getCircumf(const double PI, double radii, double &circumf)
{
    // caluculate the circumference of a circle
    circumf = 2 * PI * radii;
}

void getArea(const double PI, double radii, double &area)
{
    // caluculate the area of a circle
    area = PI * (radii * radii);
}

void writeOutFile(int i2, double radii[], double circumf[], double area[], fstream &myFile)
{
    // print results table to myFile
    myFile << "\n=======================================\n"
           << "| Radius | Circumference |    Area    |\n"
           << "=======================================" << endl;
    for (int i=0; i<i2; i++)
    {
        myFile << fixed << setprecision(2);
        myFile << "| "
               << setw(6) << radii[i]
               << " | "
               << setw(13) << circumf[i]
               << " | "
               << setw(10) << area[i]
               << " |" << endl;
        myFile << "---------------------------------------" << endl;
    }
}

void greeting()
{
    cout << "\n========================"
         << "\n    Have a nice day!    "
         << "\n========================" << endl;
}

2 个答案:

答案 0 :(得分:4)

在尝试打印之前,只需回放您的信息流。

myFile.seekg(0, myFile.beg);  // <---

while (getline(myFile, line))
{
    cout << line << '\n';
}

答案 1 :(得分:3)

我要抓住这是你正在尝试做的事情:

// call function to print results table to output file

// NOTE: open in out/trunc mode.
myFile.open("Lab20Output.txt", ios::out|ios::trunc);
if (!myFile)
{
    cout << "FILE OPEN ERROR!" << endl;
    return 0;
}
cout << "\nWe are now writing this data to a file...";
writeOutFile(i2,radii,circumf,area,myFile);
cout << "done." << endl;
myFile.close();

// print to screen the contents of file "Lab20Output.txt"
cout << "\nNow we will read back the data from the file..." << endl;

// NOTE: Open in read-mode
myFile.open("Lab20Output.txt", ios::in);
while (getline(myFile, line))
{
    cout << line << '\n';
}

myFile.close();
greeting();
return 0;

这总是截断输出文件,写入,关闭,然后以读取模式打开。很难说这肯定是你基于这个问题所希望的,但它似乎做了你想要的。

<强>输出

==============================================================
  Below, you may enter all of your radii (up to 100 entries)  
         *** Enter 0 (zero) when you are finished ***         
==============================================================

Enter your radius: 10

Enter your radius: 11

Enter your radius: 0

=======================================
| Radius | Circumference |    Area    |
=======================================
|  10.00 |         62.80 |     314.00 |
---------------------------------------
|  11.00 |         69.08 |     379.94 |
---------------------------------------

We are now writing this data to a file...done.

Now we will read back the data from the file...

=======================================
| Radius | Circumference |    Area    |
=======================================
|  10.00 |         62.80 |     314.00 |
---------------------------------------
|  11.00 |         69.08 |     379.94 |
---------------------------------------

========================
    Have a nice day!    
========================