-2

I need to produce a random matrix of type double (Dictionary) and save it to a file so that I can read them later in another program. here is how I generate the data: (OK, I have made one final edit and now each element is saved to the file after it is generated in the loop. but there is an error about fopen being unsafe!)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

/* Number of columns and rows in dictionary */
#define M (2000)
#define N ((int)(M/2))

/* Sign function */
double sign(double x) { return (x >= 0) - (x<0); }

/* Matrix indexing convention */
#define id(m, n, ld) (((n) * (ld) + (m)))


FILE *out_file = fopen("name_of_file", "w"); // write only

int main()
{
    double *D;
    double norm = sqrt(N), a;
    int MN = M*N, m, n;

    /* Initialize srand */
    srand(time(NULL));

    /* Initialize dictionary */
    D = (double*)malloc(MN * sizeof(D[0]));
    if (D == 0)
    {
        fprintf(stderr, "!host memory allocation error(dictionary)\n");
        return EXIT_FAILURE;
    }
    for (n = 0; n < N; n++)
    {
        for (m = 0; m < M; m++)
        {
            a = sign(2.0*rand() / (double)RAND_MAX - 1.0) / norm;
            D[id(m, n, M)] = a;
            fprintf(out_file, "%lf ", D[id(m, n, M)]);
        }
    }
}

How can I save D in a file so that I can load it later in another program for optimization purposes? I'm using Visual Studio 2017.

Baback Kh
  • 1
  • 3
  • Why did you tag C++? You are not using it, this is plain C. Also, please format your code properly next time. You were missing a `{` after your main signature. – hellow Oct 16 '18 at 07:15
  • 2
    Your program does not compile. And what is `id`? – Jabberwocky Oct 16 '18 at 07:16
  • Possible duplicate of [How to write an array to file in C](https://stackoverflow.com/questions/18597685/how-to-write-an-array-to-file-in-c) – M.Winkens Oct 16 '18 at 07:18
  • Please fix your code! I edited once, you edited over it again. Your turn now. – hellow Oct 16 '18 at 07:19
  • basically 2 options: `fwrite()` or `fprintf()`. First one is unreadable by humans, may fail if copied to a different system, second one is readable by humans. – pmg Oct 16 '18 at 07:20
  • I edited the code, does it compile now? – Baback Kh Oct 16 '18 at 07:27
  • @BabackKh did *you* compile what is now posted? If you did, chances are decent it will for us too. (and no, it won't compile; neither `n` nor `m` are declared anywhere, but are used in multiple places). Btw, since `D` is not use for anything but temporary storage before saving to a file, why not just save to the file rather than `D` and eliminate the unnecessary dynamic management entirely. Run your loop, dump to a file, that's it. GVa's answer does as much. – WhozCraig Oct 16 '18 at 07:37
  • @WhozCraig I extracted the code from a larger program for simplicity, I am trying to compile it in a separate project now. you are right! I should have compiled it before posting it but the main goal was to just give you an idea about the nature of matrix D. – Baback Kh Oct 16 '18 at 07:41
  • Well, it *almost* does as much. The loop that populates `D` could just as well dump to the file instead. – WhozCraig Oct 16 '18 at 07:44
  • OK, I have made one final edit and now each element is saved to the file after it is generated in the loop. but there is an error about fopen being unsafe! – Baback Kh Oct 16 '18 at 12:02

1 Answers1

0

Define a pointer of type FILE which takes a string ( the name of the file ) and the type mode (w=write, a=append, r=read). Loop over the matrix and use fprintf

FILE *out_file = fopen("name_of_file", "w"); // write only

for (n = 0; n < N; n++)
{
    for (m = 0; m < M; m++)
    {
        fprintf(out_file,"%lf ", D[n][m]);
    }
}
hellow
  • 12,430
  • 7
  • 56
  • 79
  • Thanks for your answer, Do I need to create the file before defining the pointer? and I also get an error for fopen being unsafe! – Baback Kh Oct 16 '18 at 08:00
  • OK, I have made one final edit and now each element is saved to the file after it is generated in the loop. but there is an error about fopen being unsafe! – Baback Kh Oct 16 '18 at 12:03
  • No, you do not need to create the file before. With the operator write the function automatically creates it. Regarding the unsafe, it depends on the editor that you are using. Using visual studio, there could be the possibility to use fprinf_s –  Oct 16 '18 at 14:24