我一直在为一个当地人制作一个节目..。地点,这是一个程序,将计算多少比萨饼将被订购。然而,问题不在于计算,而在于保存登录身份数据的文件。
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
using namespace std;
string logs[20];
void test(ifstream& IN, string logs[], ofstream& OUT);
void introduction();
int logging_in(string id, string logs[]);
void menu();
string newl = "\n";
string dnewl = "\n\n";
string tnewl = "\n\n\n";
string qnewl = "\n\n\n\n";
string pnewl = "\n\n\n\n\n";
int main()
{
ifstream IN;
ofstream OUT;
string id;
IN.open("loginn.dat");
cout << IN.is_open();
test(IN, logs, OUT);
string sup;
int receive = 0;
introduction();
return 0;
}
void test(ifstream& IN, string logs[], ofstream& OUT)
{
for (int x = 0; x < 20; x++)
{
IN >> logs[x];
}
IN.close();
OUT.open("loginn.dat");
for (int x = 0; x < 20; x++)
{
OUT << logs[x] << " " << "hue" << " ";
}
}
void introduction()
{
string cont;
cout << "Hello. I am the..." << dnewl
<< "Statistical" << newl << "Pizza" << newl
<< "Order" << newl << "Amount" << newl
<< "Diagnostic." << dnewl
<< "Otherwise known as Pizzahand. I will be assisting you to estimate the \namount of pizza that is to be ordered for <INSERT NAME>, as to \neliminate excessive ordering."
<< tnewl;
cout << "Press Enter to continue..." << newl;
cin.get();
}理论上,这应该是在执行其余代码之前输出数组"logs[]“。这是当我除了主要功能之外没有其他功能时的情况。当我开始使用我的下一个函数"introduction()“时,在这里读取文本文件的代码
for (int x = 0; x < 20; x++)
{
IN >> logs[x];
}好像被打乱了。与其在任何其他任务之前执行此任务,不如在程序的最末尾执行它,就像我测试的那样,在程序仍在阅读"test()“时输出其内容,没有任何运气。但是,在主函数返回"0“之后,我看到我的程序正确地将数据输出到测试文件"loginns.dat”中。对于我的程序来说,必须在程序开始时读取这个登录ID数据,因为当程序过渡到登录时,就需要数据。此外,我还尝试将这些数组和for循环放置在不同的位置:在登录函数本身、主函数中,甚至还有我在绝望中创建的另一个函数。
我已经搜索了几个小时如何解决这个问题,但没有结果,并尝试了很多小时更多。我试图解决这个问题的每一步都会导致更多的死胡同,或者更多的问题。从某种意义上说,我是一个初学者,因为今年是学习c++的第一年,我迫切需要一个专家意见(或任何有知识的人)来帮助我面对正确的方向。
谢谢。
发布于 2015-03-19 03:12:54
你只需要在给它写完之后冲洗一下它:
for (int x = 0; x < 20; x++)
{
OUT << logs[x] << " " << "hue" << " ";
}
OUT.flush();造成这种奇怪行为的原因是,当您向文件写入文件时,文件流不一定会立即写入文件。出于效率原因,他们将数据写入内部内存缓冲区(流使用的内存区域),然后在缓冲区刷新时立即将缓冲区内容写入文件。当应用程序完成时,它的所有流缓冲区都会自动刷新,这就是为什么在程序完成后,您将看到该文件已被写入。但是,您可以自己提前冲洗它们,如上面所示。当缓冲区已满时,也可能发生这种情况。
还可以使用endl令牌触发刷新,该标记写入换行符并刷新缓冲区,如下所示:
for (int x = 0; x < 20; x++)
{
OUT << logs[x] << " " << "hue" << " " << endl;
}https://stackoverflow.com/questions/29136437
复制相似问题