C++ 如何将数组的内容写入文本文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22190048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write the content of an array into a text file?
提问by Marcus
How to write the content of an array to a text file? Is it possible?
如何将数组的内容写入文本文件?是否可以?
Below is my code:
下面是我的代码:
x=0;
y=0;
//copy to real array
if(nRow == 0){
for(i=nTCol; i>=0 ; i--){
nPanelMap[nRow][x] = nTempMap[i];
x++;
}
}
if(nRow == 1){
for (i=nTCol; i>=0 ; i--){
nPanelMap[nRow][y] = nTempMap[i];
y++;
}
}
k=0;
for (i=nTCol; i>=0 ; i--){
array [k] = nPanelMap[nRow][x];
k++;
array [k] = nPanelMap[nRow][y];
k++;
}
j=0;
for (i=nTCol; i>=0 ; i--){
nPanelMap[nRow][j] = array [k];
j++;
}
nRow++;
I would like to print out arrays x
, y
, k
, and j
and write them into a text file.
The purpose of doing this is to ensure that the data passing is correct.
我想打印出数组x
,y
,k
,和j
,并写入到一个文本文件中。这样做的目的是确保数据传递是正确的。
回答by i'm PosSible
Yes, you can write into a text file.
是的,您可以写入文本文件。
#include <iostream>
#include <fstream>
using namespace std;
int main () {
const int size = 5;
double x[] = {1,2,3,4,5};
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
for(int count = 0; count < size; count ++){
myfile << x[count] << " " ;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
More reference:http://www.cplusplus.com/doc/tutorial/files/
更多参考:http : //www.cplusplus.com/doc/tutorial/files/
To write array data use this.
要写入数组数据,请使用它。
for(int count = 0; count < size; count ++){
out_myfile << x[count] << " " ;
}
回答by Ben Key
My suggestion is that you use the JSON (JavaScript Object Notation) format if you want the resulting file to be human readable. JSON is documented at http://json.org/. The ThorsSerializer found at https://github.com/Loki-Astari/ThorsSerializeris a C++ Serialization library for JSON. Serialization, as defined at http://en.wikipedia.org/wiki/Serialization, "is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and reconstructed later in the same or another computer environment." If ThorsSerializer does not work for you, I suggest that you do a Google search for "C++ JSON Serialization library."
我的建议是,如果您希望生成的文件是人类可读的,请使用 JSON(JavaScript 对象表示法)格式。JSON 记录在http://json.org/。在https://github.com/Loki-Astari/ThorsSerializer上找到的 ThorsSerializer是一个用于 JSON 的 C++ 序列化库。序列化,如http://en.wikipedia.org/wiki/Serialization所定义,“是将数据结构或对象状态转换为可以存储(例如,在文件或内存缓冲区中,或传输通过网络连接链接)并稍后在相同或其他计算机环境中重建。” 如果 ThorsSerializer 不适合您,我建议您在 Google 上搜索“C++ JSON 序列化库”。
Another option I found when doing this Google search is "cereal - A C++11 library for serialization" found at http://uscilab.github.io/cereal/.
我在进行 Google 搜索时发现的另一个选项是http://uscilab.github.io/cereal/ 上的“cereal - A C++11 library for serialization” 。
回答by Praful Surve
Use fstream class for operations like file write, read, append, seek, etc..
使用 fstream 类进行文件写入、读取、追加、查找等操作。