views:

1815

answers:

2

Hello,

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

A: 

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

dmckee
+2  A: 

View this too How do you serialize an object in C++?

lsalamon