不规则的代码执行
Irregular code execution
我一直在为一个本地。。。这是一个程序,可以计算应该订购多少披萨。然而,问题甚至不在于计算,而在于记录身份证数据的文件。
#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 = "nn";
string tnewl = "nnn";
string qnewl = "nnnn";
string pnewl = "nnnnn";
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++的第一年,我迫切需要一个专家意见(或任何有知识的人)来帮助我面对正确的方向。
谢谢。
您只需要在写入后刷新流:
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;
}
- 需要将此代码更改为执行代码
- 当前不会命中断点。没有调试器目标代码类型的可执行代码与此文件关联
- 单步执行代码时重复上一行
- 如何使用介子在C++中执行代码覆盖?
- Visual Studio 2017,C++,在单步执行代码时指向错误的行
- 在 R 中执行C++代码
- 通过 dll 注入在主线程中执行代码
- 无法在 c++ 中循环后执行代码
- 执行C 代码时快速频繁的文件访问
- 执行 C++ 代码后出错
- 一个人如何从代表函数的字符串中执行运行时执行C 代码
- 是否可以在程序崩溃后执行代码?
- 第一次在 Linux 上执行 c++ 代码的时间非常慢
- 计算 JSON 中的条目数并相应地执行代码
- 将在 CATCH 块之后执行代码
- 分析执行C++代码的每一行所花费的确切时间
- 我在执行代码时不断得到"Bus Error"?
- c while()..执行代码行的条件
- 从并行线程在主 Maya 线程上执行代码
- 在调用GNUPLOT之后,如何继续执行C 代码