C++;使用fstream对象读取和写入文本文件时出现问题 我是一个C++新手,完成作业有点麻烦,我的老师下周就没空了,所以我会寻求帮助!我肯定我只是累了,错过了一些小东西,但是我在让fstream对象创建文件,然后从文件中读取并打印到屏幕上时遇到了问题,其他一切似乎都正常
以下是作业说明,非常简单明了: 1-编写一个程序,计算圆的面积和周长 2-通过键盘从主输入圆的半径,并存储在阵列中。这必须通过一个循环来完成。假设最多有100条记录 3-调用函数,使用上面的半径计算每个圆的周长,并存储在另一个数组中 4-调用另一个函数计算圆的面积并存储在另一个数组中 5-从主打印到屏幕的圆的半径、周长和面积。应根据3个阵列中的数据打印此信息。在打印实际数据之前,请打印“半径”、“周长”和“面积”标签,并对齐每个标签下的信息 6-在main中,为名为讲师20output.txt的输出文件创建一个fstream对象 7-调用函数将上面数组中的半径、周长和面积写入Lab20Output.txt 8-从主打印到屏幕显示Lab20Output.txt的内容 9-样本运行:半径5、4、7C++;使用fstream对象读取和写入文本文件时出现问题 我是一个C++新手,完成作业有点麻烦,我的老师下周就没空了,所以我会寻求帮助!我肯定我只是累了,错过了一些小东西,但是我在让fstream对象创建文件,然后从文件中读取并打印到屏幕上时遇到了问题,其他一切似乎都正常,c++,fstream,C++,Fstream,以下是作业说明,非常简单明了: 1-编写一个程序,计算圆的面积和周长 2-通过键盘从主输入圆的半径,并存储在阵列中。这必须通过一个循环来完成。假设最多有100条记录 3-调用函数,使用上面的半径计算每个圆的周长,并存储在另一个数组中 4-调用另一个函数计算圆的面积并存储在另一个数组中 5-从主打印到屏幕的圆的半径、周长和面积。应根据3个阵列中的数据打印此信息。在打印实际数据之前,请打印“半径”、“周长”和“面积”标签,并对齐每个标签下的信息 6-在main中,为名为讲师20output.txt的
#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
//原型
无效循环(常数双,双,双&);
void getArea(常数双精度、双精度、双精度和双精度);
无效写出文件(int,double[],double[],double[],fstream&);
无效问候语();
int main()
{
常量int数组_SIZE=100;
常数双PI=3.14;
int i=0,i2=0;
双半径[阵列大小],
环[ARRAY_SIZE],
面积[阵列大小];
fstream-myFile;
弦线;
//使用循环提示用户输入半径
cout我要尝试一下,这就是你想要做的:
// 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;
只需在打印前倒回流
myFile.seekg(0, myFile.beg); // <---
while (getline(myFile, line))
{
cout << line << '\n';
}
myFile.seekg(0,myFile.beg);//有什么问题吗?有什么东西被写入了吗?你不能读取吗?没有任何东西被写入文件,甚至没有创建文件。当我使用ofstream&ifstream对象时,它就像一个符咒,但我的老师希望我们使用fstream对象写入,然后从同一个文件读取。你想创建文件吗?然后添加ios::trunc
到openmode。您是否收到“文件打开错误”?(无关:因为这是一个错误,在这种情况下,您不应该返回0
,因为0
意味着成功。)“它不起作用”不是很有描述性…你介意给我一些详细的反馈吗?例如,什么不起作用?是否创建了文件?这非常有效,谢谢!我还没有了解.seekg和.beg成员函数,所以我不确定我的老师是否会接受它们。你能简单地解释一下它们的工作原理吗?我认为这对那些如果它在一个地方,我以后可能会看到它,它肯定会对我有帮助!感谢你花时间和我一起做这件事!这件事很好,并且在我们迄今所学的范围之内…太棒了!而且,非常感谢你花时间和我一起做这件事!