Thread: write input from user in binary file

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    3

    write input from user in binary file

    I'm trying to take input from user and write it into binary file. This is my code, it runs smoothly but when I try to read the file in another (shows that its NULL) so I'm not sure why the data is not being saved into the file.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int length=2, width=2;
    
    
    struct LandData
    {
        int height;
    };
    
    
    struct LandData* WritingData()
    {
        FILE *fptr;
        struct LandData *arr = (struct LandData*)malloc(length* width* sizeof(struct LandData));
    
    
        if ((fptr = fopen("data.bin","wb")) == NULL){
           printf("Error! opening file");
            exit(1);
        }
    
    
        for (int i = 0; i < length ; i++){
            for (int j = 0; j < width; j++){
                printf("choose height: ");
                scanf("%d", &(arr + i*width + j)->height);
                fwrite(arr, sizeof(struct LandData), 1, fptr);
            }
        }
    
    
        fclose(fptr);
        return(arr);
    }
    
    
    int main()
    {
        struct LandData *arr =WritingData();
        free(arr);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    You are writing the first element of the array over and over.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    By the way there doesn't really seem to be a need for the array of your structure, because you are only writing a single instance, so just use a single instance.

    Something more like:

    Code:
    
    void WritingData(FILE* outputFile, int numRecords)
    {
        LandData temp;
        for(int i = 0; i < numRecords; i++)
        {
            printf("choose height: ");
            scanf("%d", &temp.height);
            printf("Choose type: ");
            scanf("%d", &temp.type);
            fwrite(&temp, sizeof(struct LandData), 1, outputFile);
        }
    }
    Also there is no need to keep opening new topics for existing problems.

  4. #4
    Registered User
    Join Date
    May 2021
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    By the way there doesn't really seem to be a need for the array of your structure, because you are only writing a single instance, so just use a single instance.
    yes I know but I have to use it cause its a requirement on my project that's why I'm confused

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    yes I know but I have to use it cause its a requirement on my project that's why I'm confused
    Please show the text of your project requirements.

    What exactly don't you understand?

    It seems to me that you're confusing yourself by using dynamic memory, so I'd recommend you start with the simpler static allocated array. Define the array in main() and pass the array and the size of the array to your functions as required.

    I'd start with something more like:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ARRAY_SIZE 3
    
    typedef struct LandData
    {
        int type;
        int height;
    } LandData;
    
    void getData(LandData data[], int arraySize);
    
    
    int main()
    {
        LandData data[ARRAY_SIZE];
        getData(data, ARRAY_SIZE);
    }
    
    void getData(LandData data[], int arraySize)
    {
        for(int i = 0; i < arraySize; i++)
        {
            printf("choose height: ");
            scanf("%d", &data[i].height);
            printf("Choose type: ");
            scanf("%d", &data[i].type);
        }
    }
    I'll leave the write/read/display functions for you to implement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-06-2013, 11:39 PM
  2. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  3. getting input from user(character) converting to binary
    By saravana in forum C Programming
    Replies: 4
    Last Post: 09-24-2009, 12:47 PM
  4. write in a binary file
    By singersinger in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2007, 03:24 AM
  5. Replies: 5
    Last Post: 05-06-2004, 09:14 AM

Tags for this Thread