使用 Visual C++ 将二维数组写入文件并从文件中读取二维数组

发布于 2024-07-14 09:21:23 字数 1024 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行至春深 2024-07-21 09:21:23

现在没有时间详细讨论这个问题,您可能会使用“序列化”和“反序列化”一词四处看看(谷歌或SO)。


稍后:我不做任何视觉上的事情,所以我将忽略任何与语法相关的内容。

  1. 它能编译吗?
  2. 如果是这样,它运行时不会崩溃吗?
  3. 如果是这样,您是否直接查看了输出? 它给出了你所期望的吗?
  4. 使用 fscanf 进行输入解析很棘手。 您需要非常小心,确保空白字符匹配。 看这里。 你最大的问题似乎是是您正在编写换行符,但不允许输入包含它们。

我的建议:慢慢来,检查每一步发生了什么,当你更好地了解可能出了什么问题时,然后报告(编辑问题)......

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.

  1. Does it compile?
  2. If so, does it run without crashing?
  3. If so, have you looked at the output directly? Is it giving what you expect?
  4. Using fscanf for input parsing is tricky. You need to be very careful that you get the whitespace characters to match up. Look here. You biggest problem appears to be that you are writing newlines, but not allowing the input to contain them.

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文