Thread: Writing an array to file

  1. #1
    Registered User
    Join Date
    Dec 2021
    Posts
    34

    Writing an array to file

    I've been trying for ages to get this code to work but it just presents me with a blank file every time. Any help is very much appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct
    {
        int x, y;
    } PPMImage;
    
    
    typedef struct 
    {
        int rgb_comp_colour;
        char buff[16];
        char filetype[2];
        int height;
        int width;
    } PPMHead;
    
    
    PPMHead head[3];
    int **Array; //Double pointer defines as a pointer pointing to a pointer that is pointing to an integer
    
    
    int main(void)
    {
        FILE *fp;
        char fname[100];
        printf("Enter file name: ");
        scanf("%s", fname);
        fseek(stdin,0,SEEK_END);
        fp = fopen(fname, "r");
        if (fp == NULL)
        {   
            printf("\tError while opening the file\n");
        }                        
        else    
        {
            printf("\tReading in %s\n", fname);
        }
    
    
        Array = malloc(head[0].width*sizeof(int*)); //Points to malloc
        for (int i=0;i<head[0].width;i++)
        {
            Array[i] = (int*)malloc(head[0].height*sizeof(int));
        }
        for(int j=0;j<head[0].height;j++)
        {
            for(int i=0;i<head[0].width;i++)
            {
                fscanf(fp, "%3d ", &Array[i][j]); //Scans the address of Array[i][j]
            }
        }    
        fclose(fp);
    
    
        //Save PPM Array Into New PPM File
        FILE *pf;
        int i, j;
        char fname2[100];
        printf("Enter file name: ");
        scanf("%s", fname2);
        fseek(stdin,0,SEEK_END);
        pf = fopen(fname2, "w");
        if (pf == NULL)
        {   
            printf("\tError while opening the file\n");
        }
        else    
        {
            printf("\tWriting in %s\n", fname2);
        }
    
    
           for(j=0;j<head[0].height;j++)
            {
                fprintf(pf, "\n");
                for(i=0;i<head[0].width;i++)
                {
                    fprintf(pf, "%3d ", Array[i][j]);
                    //fprintf(pf, "%3d ", (Array+j*head[0].width + i));
                }
            } 
    
    
        fclose(pf);
        free(Array);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    25
    I can see that you're using head[0].width and head[0].height without initializing head, which means they're always zero, as head is global. This means that your loops do not execute even a single time.

    Can you please post the code where you initialize it if you do? Also, you need to use the extern storage class if you do so, or otherwise initialize head before reading from it.

  3. #3
    Registered User
    Join Date
    Dec 2021
    Posts
    34
    Ohhhh. I see. How would I initialise it? Is it similar to initialising a pointer?

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    25
    Initializing a struct means initializing each of its members, for example:
    Code:
    #include <stdio.h>
    
    typedef struct
    {
        int x;
        int y;
        int z;
    } point3d;
    
    
    int main(void)
    {
        // Declaration
        point3d p;
    
        // Initialization
        p.x = 1;
        p.y = 2;
        p.z = 3;
    
        // Reading of members
        printf("The point is (x = %d, y = %d, z = %d).\n", p.x, p.y, p.z);
    
        return 0;
    }
    As for your case, you would want to use appropriate data to initialize head[0], head[1], etc. By the way, you may want to consider not declaring it globally, but inside main.

    P.S.: There are more ways to initialize a struct variable, but initializing each member on its own is the clearest.
    Last edited by erikkonstas; 12-31-2021 at 05:43 PM.

  5. #5
    Registered User
    Join Date
    Dec 2021
    Posts
    34
    Quote Originally Posted by erikkonstas View Post
    Initializing a struct means initializing each of its members, for example:
    Code:
    #include <stdio.h>
    
    typedef struct
    {
        int x;
        int y;
        int z;
    } point3d;
    
    
    int main(void)
    {
        // Declaration
        point3d p;
    
        // Initialization
        p.x = 1;
        p.y = 2;
        p.z = 3;
    
        // Reading of members
        printf("The point is (x = %d, y = %d, z = %d).\n", p.x, p.y, p.z);
    
        return 0;
    }
    As for your case, you would want to use appropriate data to initialize head[0], head[1], etc. By the way, you may want to consider not declaring it globally, but inside main.

    P.S.: There are more ways to initialize a struct variable, but initializing each member on its own is the clearest.
    Code:
    typedef struct 
    {
        int rgb_comp_colour;
        char buff[16];
        char filetype[2];
        int height;
        int width;
    } PPMHead;
    
    
    PPMHead head[3];
    head.height = 1;
    head.width = 2;
    head.filetype = 3;
    Thanks. I have done it like this but it tells me that . should be a ;

    Is it me doing it wrong or my program? I would assume its me.

  6. #6
    Registered User
    Join Date
    Apr 2021
    Posts
    25
    Quote Originally Posted by DGB_684 View Post
    Thanks. I have done it like this but it tells me that . should be a ;
    head isn't a PPMHead, but an array of PPMHead. You have to initialize each element of head separately (that is, the members of head[0], the members of head[1], etc.), and in a function (not outside like you have it right now). Also, you need to put the correct data in the members, e.g. head[0].filetype is a char[2], so you can't assign 3 to it. Unfortunately, I can't know what said correct data would be in your case.

  7. #7
    Registered User
    Join Date
    Dec 2021
    Posts
    34
    I changed it to this but I still don't think its right. The point of this program is to read in (pixel) values from a ppm file, store them in an array and then write them into a file. So I want the 2d array I've malloced to point to the image structure I guess...? It doesn't really work as a function for me but I can't have it in my main function because this is a small part of a bigger project.

    Code:
    void head()
    {
        PPMHead head[0];
        head[0].height;
        head[0].width;
        head[0].filetype;
    }

  8. #8
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    Why don't your forget about the image for a little while and focus on just arrays ? Once you understand how it works you can integrate your knowledge into the image project

    Code:
    #include <stdio.h>
    
    #define ROWS    4
    #define COLS    4
    
    
    int main()
    {
        int **array = NULL;
        
        // create and initialize your array
        
        // display array
        
        // save to file 
        
        // read array 
        
        // display array
        
        // delete array
    }
    Maybe you want to watch this tutorial: Dynamically allocated multi-dimensional arrays in C - YouTube
    Last edited by thmm; 01-01-2022 at 02:30 AM. Reason: Added link

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing array into file and reading array from file
    By isolatedleo in forum C Programming
    Replies: 1
    Last Post: 04-20-2014, 02:59 AM
  2. Writing an array in a file
    By Marria in forum C Programming
    Replies: 13
    Last Post: 09-12-2012, 02:25 PM
  3. Writing array to a file
    By redsfan in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2011, 05:01 PM
  4. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  5. Writing an array to a file
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 10-24-2005, 06:25 AM

Tags for this Thread