Thread: Bubble sort Txt file using structs

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    4

    Bubble sort Txt file using structs

    Hi
    I am trying to input data from a txt file
    Sort it using structs
    and out put it to another file
    i am getting a lot of errors that i cant seem to figure out.
    Note this is s section of a larger body of work

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>
    #defineSIZE300
    #defineemp_File_len211          // length of an employee file line
    int exit_to_main;               // allows to return to main menu
    /*
     ********************************************************************************************
     *                          Function Prototypes
     * 
     ********************************************************************************************
     */
    
    void calSub();                  //calc function  called by load data
    int load_data();                // function 1
    void display_menu();            // Function 2  returns the user input 
    void add_new_employee();
    void delete_employee();
    //void editemployeeNum(); // used to modify files by Employee number
    void view_all_employees();
    void view_qualifying_employees();
    void find_disqualified_employees();
    void sort_employees_by_name();
    void sort_employees_by_subsidy_amount();
    void view_total_subsidy_amount();
    void save_data(int, int);       // function 11 
    
    /*
     ********************************************************************************************
     *                          Structs
     * 
     ********************************************************************************************
     */
    
    typedef struct {
      int employee_code;
      const char *employee_surName[20];
      const char *employee_name[20];
      int insurable_weeks;
      float gross_pay;
      float subsidy;
      const char *qualify[3];
      const char *reason[50];
    
    } Employee;
    
    Employee tempEmployee;          // a global struct of type employee.data is read into an array of structs
    Employee compare;               // a global struct used to compare structs and elements of structs 
    Employee add;
    Employee del;
    Employee tempsort[500];         // used for sorting
    Employee EMP[500];              // used for sorting
    //** Array of structs******
    Employee dynEMP[250];           // an array of structs for the employee 
    
    /*
     ********************************************************************************************
     *                         Main Block
     * 
     ********************************************************************************************
     */
    
    int main(void)
    {
      sort_employees_by_name();
    
      return 0;
    }
    
    void sort_employees_by_name()
    {
      int i = 0, size, j;
      FILE *cfPtr = fopen("subsidy.txt", "r");
    
      char ch;
      if (cfPtr == NULL) {
      }
      printf("\n Cannot Open File");
      exit(0);
    
      while (ch != EOF) {
        (fscanf
         (cfPtr, "%d%s%s%d%f%f%s%s", &(EMP.employee_code), EMP.employee_surName,
          EMP.employee_name, &(EMP.insurable_weeks), &(EMP.gross_pay),
          &(EMP.subsidy), &(EMP.qualify), &(EMP.reason)));
        ch = fgetc(cfPtr);
        i++;
      }
      size = i - 1;
      for (i = 1; i < size; ++i)
        for (j = 0; j < size - i; j++)
          if (EMP[j + 1].surName < EMP[j].surName) {
            tempsort = EMP[j];
            EMP[j] = EMP[j + 1];
            EMP[j + 1] = tempsort;
          }
      FILE *cfPtr2 = fopen("Name_Sort.txt", "w");
      for (i = 0; i < size; i++)
        fprintf(cfPtr2, "%15d%15s%15s%15d%15f%15f%15s%15s", EMP.employee_code,
                EMP.employee_surName, EMP.employee_name, EMP.insurable_weeks,
                EMP.gross_pay, EMP.subsidy, EMP.qualify, EMP.reason);
    
      printf("\n The file is sorted successfully and saved as Name_Sort.txt \n\n");
    
    }
    Last edited by Salem; 01-10-2021 at 12:19 PM. Reason: removed crayola -

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Why all the horrible global variables? You really need to learn to properly pass the required variables to and from your functions.

    Why is the sort function trying to read from the file, shouldn't your load_data() function read the data from the file?

    You really should be using braces on all of your control statements until you are more familiar with the language.

    What is your compiler telling is the problems with the code posted?

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    4
    Apologies I am only learning I started 6 weeks ago
    I have changed some pointers in the structs to arrows , the code is compiling now but no output.
    I am trying to figure out how to load the data but I don't know how to dynamically create sufficient memory for each entry into the file.
    I apologies for my poor style.
    Compiler is not telling me anything now it says 0 problems




  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: The use of "conio.h" usually means the student is learning from a poor instructor in India.

    Are you supposed to use malloc in this assignment?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Jan 2021
    Posts
    4
    Hi Yes I am supposed to load the text file in Once the file is open, data is read into an array of structs by dynamicallycreating sufficient memory for each entry read from the file.
    Thanks for the tip much appreciated.
    My apologies again

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Since you refuse to tell me if you are supposed to use malloc I give up helping you. But, since I am not that good using malloc I would likely have given up if you said yes.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Jan 2021
    Posts
    4
    Hi Sorry i Said Yes I am supposed to use Malloc
    Apologies for the confusion

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by deburca98
    I am trying to figure out how to load the data but I don't know how to dynamically create sufficient memory for each entry into the file.
    I would start by defining a function to print a single employee's data:
    Code:
    void print_employee(FILE *fp, const Employee *employee);
    I would manually create an Employee object and test this print_employee function by printing to stdout. I expect that when you do this, you'll find that your declaration of the Employee struct is problematic, e.g., you might encounter problems with employee_surName and employee_name. So you fix the struct and the function, compile and test, and repeat until both work as expected.

    Then, the next step could be to define a function to print the data of many employees, as stored in an array, e.g.,
    Code:
    void print_employee_list(FILE *fp, const Employee *employees, size_t num_employees);
    By calling print_employee, this should be fairly simple, but test it anyway by manually defining an array of Employee objects to pass to print_employee_list.

    When you're sure print_employee_list works, you're then ready to implement the load_data function. I might rename it to load_employees, and of course there are a few ways to declare it, but you could try first defining a struct:
    Code:
    typedef struct
    {
        Employee *data;
        size_t size;
        size_t capacity;
    } EmployeeList;
    and using it:
    Code:
    void load_employees(FILE *fp, EmployeeList *employees);
    The idea is that you will allocate space for capacity number of Employee objects, then read into them from the file, increasing the size as you go. When the size reaches the capacity and you still have more employee data to read, you increase the capacity (this can be done by a fixed increase or by a factor) and re-allocate according to the new capacity.

    To test that load_employees works, you call the print_employee_list function, passing data and size. (Or you can rework the print_employee_list function to have a const EmployeeList* parameter instead of having the second and third parameters). Later, you can change the return type of load_employees to bool, int, or an enum type so that you can return a boolean value or status code indicating if the load was successful or failed (e.g., due to a failure to allocate memory, or due to invalid input, etc). When you have load_employees working within this error checking, then you're ready to implement sorting of the employee data.

    Notice that for all these functions, I'm making use of the parameters (and return type) rather than global variables. This makes it easier for you to reason about the program and to reuse the functions. When testing, likewise you would define local variables within the main function (or in auxiliary test functions called by the main function).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-27-2020, 12:44 PM
  2. Bubble Sort and Selection Sort
    By acmarshall in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2014, 10:01 AM
  3. Bubble sort text file
    By decxan in forum C++ Programming
    Replies: 5
    Last Post: 02-01-2011, 10:20 PM
  4. Bubble Sort from Input File
    By creilly in forum C Programming
    Replies: 11
    Last Post: 10-12-2010, 01:31 AM
  5. Rate My Application: File Stream/Bubble Sort
    By KingZoolerius66 in forum C++ Programming
    Replies: 2
    Last Post: 12-13-2003, 11:04 PM

Tags for this Thread