使用 Visual C++ 将二维数组写入文件并从文件中读取二维数组
我是 C++ 新手。我创建了 680x680 二维数组。我尝试将其写入 txt 文件。不幸的是,我无法定期沿其维度写入二维数组。 我还想从txt文件中读取二维数组。我的代码如下。你能帮我吗?
/*Declaration 680 *680 multidimensional array*/
array< array< double >^ >^ arr = gcnew array< array< double >^ >(680);
for (j=0;j<arr->Length;j++){
arr[j]=gcnew array<double>(680);}
/*Write double array to file*/
FILE *OutFile = fopen("C:\\test.txt","w++");
for(n=0;n<=(N-1);n++){
fprintf(OutFile,"\n ");
for(k=0;k<=(N-1);k++){
fprintf(OutFile,"\t %f ",dizi[n][k]);}}
fclose(OutFile);
/* Declaration array reading from file*/
array< array< double >^ >^ read = gcnew array< array< double >^ >(680);
for (j=0;j<read->Length;j++){
read[j]=gcnew array<double>(680);}
/* reading array from file*/
FILE *InFile = fopen("C:\\test.txt","r");
double db;
for(n=0;n<=(N-1);n++){
for(k=0;k<=(N-1);k++){
fscanf(InFile,"\t %f ",&db);
read[n][k]=db; }}
fclose(InFile);
此致...
I'm new in C++.I made up 680x680 two dimensional array.And I tried to write it to txt file.Unfortunately,I can't write two dimensional array along its dimensions regularly.
Also I want to read two dimensional array from txt file.My code is below.Could you help me ?
/*Declaration 680 *680 multidimensional array*/
array< array< double >^ >^ arr = gcnew array< array< double >^ >(680);
for (j=0;j<arr->Length;j++){
arr[j]=gcnew array<double>(680);}
/*Write double array to file*/
FILE *OutFile = fopen("C:\\test.txt","w++");
for(n=0;n<=(N-1);n++){
fprintf(OutFile,"\n ");
for(k=0;k<=(N-1);k++){
fprintf(OutFile,"\t %f ",dizi[n][k]);}}
fclose(OutFile);
/* Declaration array reading from file*/
array< array< double >^ >^ read = gcnew array< array< double >^ >(680);
for (j=0;j<read->Length;j++){
read[j]=gcnew array<double>(680);}
/* reading array from file*/
FILE *InFile = fopen("C:\\test.txt","r");
double db;
for(n=0;n<=(N-1);n++){
for(k=0;k<=(N-1);k++){
fscanf(InFile,"\t %f ",&db);
read[n][k]=db; }}
fclose(InFile);
Best Regards...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在没有时间详细讨论这个问题,您可能会使用“序列化”和“反序列化”一词四处看看(谷歌或SO)。
稍后:我不做任何视觉上的事情,所以我将忽略任何与语法相关的内容。
我的建议:慢慢来,检查每一步发生了什么,当你更好地了解可能出了什么问题时,然后报告(编辑问题)......
No time to work through this in detail right now, you might have a look around (google or SO) using the words "serialization" and "deserialization".
Later: I don't do visual-anything, so I'm going to ignore anything syntax related.
My advice: go slow, check what is happening at each step, and report back (edit the question) when you have a better idea what might be wrong...