C++ 我在用c+读取文本文件时遇到问题+;您已经在程序中创建的

C++ 我在用c+读取文本文件时遇到问题+;您已经在程序中创建的,c++,C++,我是这个社区的新成员,向你们大家问好。我只是对我的编码有个问题 这是作业 “问题 编写一个程序来模拟管理考试成绩。 规范 在60-100范围内随机生成100个“测试分数”,并将其保存在文本文件中。 读取文本文件并执行以下操作: 计算60-69、70-79、80-89、90-100之间的考试分数。 确定所有测试分数的高、低和平均分数。 显示汇总结果(每个范围内的分数数;所有分数的高、低和平均值)。 将结果保存在一个单独的文本文件中,格式与屏幕上显示的格式相同。“ 这就是我到目前为止所做的。我完成了

我是这个社区的新成员,向你们大家问好。我只是对我的编码有个问题

这是作业

“问题 编写一个程序来模拟管理考试成绩。 规范 在60-100范围内随机生成100个“测试分数”,并将其保存在文本文件中。 读取文本文件并执行以下操作: 计算60-69、70-79、80-89、90-100之间的考试分数。 确定所有测试分数的高、低和平均分数。 显示汇总结果(每个范围内的分数数;所有分数的高、低和平均值)。 将结果保存在一个单独的文本文件中,格式与屏幕上显示的格式相同。“

这就是我到目前为止所做的。我完成了创建100个介于60和100之间的数字的文件,并将其保存在文本文件中。但是,在读取文件时,它要么不读取,要么给我一个空白的控制台页面。我该怎么办?也让我们假设如果我做了这部分,对于“计算60-69、70-79、80-89、90-100之间的考试分数”的部分,你会如何创建这个结构?(for循环,if语句) 大多数视频只显示创建一个.txt文件或读取它。我没有遇到过一个同时创建和阅读它的例子。(注:我看了很多视频,但这并没有解决我的问题,你是我最后的希望)

#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
int testnumber;
整数计数;
流数据的统计;
data.open(“data.txt”);
int fromfile;//从文件中获取整数。
ifstream readFile;//从文件拖缆读取;
readFile.open(“data.txt”);//读取data.text

对于(count=1;count,用于静默读取的
open
操作似乎失败。若要解决此问题,请仅在关闭该文件进行写入后才打开该文件进行读取:

ofstream data;
data.open("data.txt"); 

ifstream readFile; // reading from file streamer; not open yet

...

data.close();

readFile.open("data.txt"); // open the file that we have just created
void write_stuff()
{
    ofstream data("data.txt"); // one line of code is sufficient to open a file

    ...

    // No need to close - file is closed automatically when exiting a function
}

void read_stuff()
{
    ifstream readFile("data.txt"); //reads data.text

    ...

    // No need to close either
}

int main()
{
    write_stuff();
    read_stuff();
}
如果将程序中不相关的部分分开,则可以自动避免此问题。在这种情况下,最好有一个函数用于读取,另一个函数用于写入:

ofstream data;
data.open("data.txt"); 

ifstream readFile; // reading from file streamer; not open yet

...

data.close();

readFile.open("data.txt"); // open the file that we have just created
void write_stuff()
{
    ofstream data("data.txt"); // one line of code is sufficient to open a file

    ...

    // No need to close - file is closed automatically when exiting a function
}

void read_stuff()
{
    ifstream readFile("data.txt"); //reads data.text

    ...

    // No need to close either
}

int main()
{
    write_stuff();
    read_stuff();
}

如您所见,拥有两个功能的副作用是您不需要考虑打开/关闭顺序,“系统”会“自动”执行该操作.

在数据写入文件之前,您同时打开了流的
ifstream
。打开流的
,写入数据,关闭它,然后打开
ifstream
并从中读取

对于读取部分,您只需将
从行
转换为
int
,例如使用
istringstream
,然后根据需要对值进行操作。

请参见此处:

另一件事-
fromFile
是一个整数,但是
getline
检索整行。它的第二个参数是一个字符串。您应该使用格式化的输入运算符
运算符>
进行提取:

以下是您新的改进计划:

#包括
#包括
//请不要再使用名称空间std
int main()
{
int testnumber;
整数计数;
std::fstream data(“data.txt”);//对两个输入都使用'std::fstream'
//和输出
int-fromfile;

对于(count=1;count只要您将int fromfile;//从文件中获取整数。更改为std::string fromfile;。我不理解您的编译器为什么没有对此抱怨。正如其他答案所说,最好的做法是不要多次打开文件,但这在您的情况下不是问题

while (std::getline(readFile, fromFile))
while (readFile >> fromFile)
#include <iostream>
#include <fstream>

// no more using namespace std please

int main()   
{
    int testnumber;
    int count;

    std::fstream data("data.txt"); // use `std::fstream` for both input
                                   // and output

    int fromfile;

    for (count = 1; count <= 100; ++count)
    {
        testnumber = rand() % 40 + 60;
        std::cout << "test score " << testnumber << std::endl;
        data << testnumber << std::endl;
    }

    while (readFile >> fromFile) 
    {
        std::cout << fromfile;
    }

    std::cin.get(); // don't use `system("pause")
}