Thread: C programing save inputed array values to bin - assigment

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    3

    Lightbulb C programing save inputed array values to bin - assigment

    p.s. there was a topic with same files i guess my class mates

    Main objective :

    1.Shows data (in table format) from a binary file
    2.If file does not exist show nothing
    3.Then ask for user input to fill data
    4.Saves user input to file
    5.Exits

    1. so this is the main code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    
    struct student {
      char name[20];
      char lastname[20];
      unsigned int course;
      char ReasonToKill[20];
    };
    
    void data_input(struct student *data, int num)
    {
      int idx = 0;
      while (num-- > 0) {
        printf("Enter Name,Lastnamem, course(numbers only),"
                 "ReasonToKill(press enter after each section Andrejs[Enter]"
                  " Ivanovs[Enter] "
              "2[Enter] Uglymot..[Enter]\n "
              "if more then one student this message will repeat: ");
        scanf("%s %s %d %s", (data + idx)->name, (data + idx)->lastname,
              &(data + idx)->course, (data + idx)->ReasonToKill);
        idx++;
      }
    }
    
    void data_show(struct student *data, int num)
    {
      int idx = 0;
      printf("Name                |"
             " LastName            |" " Course |" " ReasonToKill     \n");
      while (num-- > 0) {
        printf("%-20s| %-20s| %-7d| %-10s\n", (data + idx)->name,
               (data + idx)->lastname,
               (data + idx)->course, (data + idx)->ReasonToKill);
        idx++;
      }
    }
    
    int main(int argc, char **argv)
    {
      int num;
      struct student *sp = NULL;
      umber of students to kill:");
     scanf(" % d ", &num);
     sp = (struct student *)malloc(sizeof(struct student) * num);
     if (!sp) {
      printf(" Memory allocation failed \ n ");
      return -1;
    
     }
     data_input(sp, num);
     data_show(sp, num);
    
     free(sp);
    
     return 0;
    }
    this is the code to create(if not created),save to(overwrites) bin file but it has function to save only data that is already given from the start
    2.
    Code:
    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    struct student {
      int group;
      int kurss;
    };
    int main(int argc, char **argv)
    {
      int fd;
      int bytes = 0;
      char *file = "data.bin";
      struct student data = {
        .group = 3,
        .kurss = 4
      };
      fd = open(file, O_CREAT | O_TRUNC | O_RDWR, 0000777);
      if (fd == -1) {
        printf("File open error: %i (%s)\n", errno, strerror(errno));
        return fd;
      }
      bytes = write(fd, &data, sizeof(data));
      if (bytes == -1) {
        printf("File write error: %i (%s)\n", errno, strerror(errno));
        close(fd);
        return bytes;
      }
      printf("Student data saved to %s (%i bytes)\n", file, bytes);
      close(fd);
      return 0;
    }
    this the 3rd code
    it functions as the 2nd
    only it can not create a bin file if no file detected it will give a error in command promp(logicaly), it can save file(adding more values with out overwriting the the old ones)
    Code:
    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    struct student {
      int group;
      int kurss;
    };
    int main(int argc, char **argv)
    {
      int fd;
      int err = 0;
      char *file = "data.bin";
      struct student data = {
        .group = 2,
        .kurss = 1
      };
      fd = open(file, O_APPEND | O_RDWR, 0000777);
      if (fd == -1) {
        printf("File open error: %i (%s)\n", errno, strerror(errno));
        return fd;
      }
      err = write(fd, &data, sizeof(struct student));
      if (err == -1) {
        printf("File write error: %i\n", err);
        close(fd);
        return err;
      }
      printf("%lu bytes saved to %s\n", sizeof(struct student), file);
      close(fd);
      return 0;
    }
    Question 1 how to make 2nd or 3th to:
    create if not created, save(adding more values like 3rd code) without [FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]overwriting the old file.
    Question 2 how to implement a program(a 2nd+3rd code to the 1st one) so it would read all values:
    that I have inputed and save it(like code 3 and create if not created a bin file with these values).
    As the 1st program is base of structure that users inputs while 2nd and 3rd is from values that are set in the code.
    really need help with these ones and would very much appreciate
    the comments in the code.


    4. code it reads the bin file (not that important)
    it must be seperate.
    Question 3 (if possible) how to make it read the the 1st code.
    Because its size of that of 2. and 3.
    Code:
    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    #include <stdlib.h>
    struct student {
      int group;
      int kurss;
    };
    int main(int argc, char **argv)
    {
      int fd;
      int err = 0;
      int bytes = 1;
      int count = 0;
      int idx = 0;
      char *file = "data.bin";
      struct student *data = NULL;
      struct student *tmp;
      fd = open(file, O_RDWR, 0000777);
      if (fd == -1) {
        printf("File open error: %i (%s)\n", errno, strerror(errno));
        return fd;
      }
      data = (struct student *) malloc(sizeof(struct student));
      while (bytes > 0) {
        bytes = read(fd, data + count, sizeof(struct student));
        if (bytes == -1) {
          printf("File read error: %i (%s)\n", errno, strerror(errno));
          close(fd);
          free(data);
          return errno;
        }
        if (!bytes)
          break;
        if (bytes < sizeof(struct student)) {
          printf("Error: read %i bytes out of %lu\n",
                 bytes, sizeof(struct student));
          close(fd);
          free(data);
          return errno;
        }
        count++;
        tmp = (struct student *) realloc(data,
                                         sizeof(struct student) * (count + 1));
        if (!tmp) {
          close(fd);
          free(data);
          return errno;
        }
        data = tmp;
      }
      printf("group\tkurss\n");
      while (count--) {
        printf("%i\t%i\n", (data + idx)->group, (data + idx)->kurss);
        idx++;
      }
      printf("Read %i records from %s\n", idx, file);
      close(fd);
      free(data);
      return 0;
    }
    Last edited by Salem; 12-18-2019 at 01:34 PM. Reason: Removed crayola

  2. #2
    Registered User
    Join Date
    Dec 2019
    Posts
    3
    also problem that 2nd and 3nd they work with int if theres a char[20] variable it will give out an error
    Last edited by Denton72; 12-18-2019 at 05:49 AM.

  3. #3
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    306
    Where's your indentation at? Does your main function take command line arguments? I don't think you ever use int argc or char **argv so why do you think it's important to have them there?
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  4. #4
    Registered User
    Join Date
    Dec 2019
    Posts
    3
    int argc, char **argv - no i didn't putted that there the code had it from the start
    char group, you can say its just there, wasting space.
    .group = D, |19|error (any number that is integer is working normaly) 'D' was not declared in this scope|
    or char group[20]; … .group = 1, error: name 'group' used in a GNU-style designated initializer for an array
    Trying to first make this work so i could understand how this code can work
    Last edited by Denton72; 12-18-2019 at 09:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to save pixel values of a Image in a matrix?
    By jimmy2447 in forum C Programming
    Replies: 24
    Last Post: 03-30-2016, 11:15 AM
  2. Save values in a function
    By dgs012 in forum C Programming
    Replies: 3
    Last Post: 12-08-2010, 05:01 AM
  3. how to save one svg image by programing
    By sfguofen in forum C++ Programming
    Replies: 9
    Last Post: 09-04-2009, 08:11 PM
  4. search numbers in an inputed string [array of strings]
    By zattara13 in forum C Programming
    Replies: 1
    Last Post: 03-28-2008, 06:11 AM
  5. save values from array to datatable more quick
    By Leite33 in forum Windows Programming
    Replies: 0
    Last Post: 11-09-2006, 10:09 AM

Tags for this Thread