Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将程序输入存储到内存?_C++ - Fatal编程技术网

C++ 如何将程序输入存储到内存?

C++ 如何将程序输入存储到内存?,c++,C++,我对编程比较陌生,因为我在学校才刚开始编程。我本来打算做一个游戏,但我没能想出如何将数据输入或其他值存储到计算机内存中,然后在以后的运行中再次使用它们。这能做到吗 S.I使用C++,所以如果你能通过C++来解释它就好了。< P>因为你是新的编程,在以后的运行中保存数据的一个好的开始是将数据写入文本文件,然后在稍后的运行中从文件中加载这些数据。这将为您提供一些关于文件输入/输出的良好经验(这对于任何编程语言都很有用,而不仅仅是C++)。这里有一个很好的关于文件i/o的教程,值得作为起点进行检查:

我对编程比较陌生,因为我在学校才刚开始编程。我本来打算做一个游戏,但我没能想出如何将数据输入或其他值存储到计算机内存中,然后在以后的运行中再次使用它们。这能做到吗


S.I使用C++,所以如果你能通过C++来解释它就好了。

< P>因为你是新的编程,在以后的运行中保存数据的一个好的开始是将数据写入文本文件,然后在稍后的运行中从文件中加载这些数据。这将为您提供一些关于文件输入/输出的良好经验(这对于任何编程语言都很有用,而不仅仅是C++)。这里有一个很好的关于文件i/o的教程,值得作为起点进行检查:

对于基本演示,假设您希望保存您的玩家姓名和点数:

#include <iostream>
#include <string>
#include <fstream> 

using namespace std;
int main() {

// player information we want to write out
string name = "player1";
int points = 1234;
int turn = 10;

ofstream fout; // I use fout as shorthand for "File OUTput"
fout.open("file1.txt"); // opens a file named 'file1.txt' for writing 

// write the data to the file
fout << name << ' ' << points << ' ' << turn << endl;

fout.close(); // don't forget to close the file output stream

// now let's open the file to read in the data
ifstream fin; // I use fin as shorthand for "File INput"
fin.open("file1.txt");

string namereadin = "";
int pointsreadin = -1;
int turnreadin = -1;

// now let's read in the data. Since we know what order we wrote the info
// to file in, we can read it in using that same order
fin >> namereadin >> pointsreadin >> turnreadin;

// now just to show that we read the input correctly, let's output it to the screen
cout << namereadin << " " << pointsreadin << " " << turnreadin << endl;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
//我们要写出的玩家信息
string name=“player1”;
积分=1234;
整圈=10;
ofstream fout;//我使用fout作为“文件输出”的缩写
fout.open(“file1.txt”);//打开名为“file1.txt”的文件进行写入
//将数据写入文件

ft<代码> int a;STD::CIN > a;<代码>你是指编写一个SaveFILE?这个问题更多地指向如何保存文件中的数据,例如,在另一个时间使用的数据,而不是C++提供的直接内存使用。