-1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

typedef struct student{
   char name[25];
   float marks;

}student_t;

int size = 0;
int capacity = 2;


char filename[30];
#define LINE_SIZE 128

void print(student_t **arr){
    for (int i = 0; i < size; i++){
        printf("%d: %s, %f\n", i+1, arr[i]->name, arr[i]->marks);
    }
}

int create_table_entry(student_t **arr, int *size, int *capacity){
     //printf("%d",size);
     if( *size == *capacity) {
         *capacity = *capacity * 2;
         *arr = realloc (*arr, *capacity * sizeof(student_t));
     }
     return 0;
}

void add_record(student_t **arr, char *name, char *marks){
    create_table_entry(arr,&size ,&capacity);
    strcpy(arr[size]->name, name);
    arr[size]->marks = atof(marks);
    size++;
    //print(arr);
}

int get_token(char *line, char fields[][30], char *delim) {
    int token_cnt = 0;
    char *token = strtok(line , delim);
    while(token != NULL) {
        // check if there is more than two tokens
        if(token_cnt >= 2) {
            return 3;
        }
        //printf("%s\n", token);
        strcpy(fields[token_cnt++], token);
        // update token
        token = strtok(NULL, delim);
    }
    return token_cnt;
}

static void sort_data(){
   // printf("reading the file ...........");
   FILE *fileOpen = fopen( filename , "r");
   if (fileOpen == NULL){
       fprintf(stderr, "Error in opening input file. Filename = %s, Error = %s\n", filename, strerror(errno));
       exit (-1);
   }
   char line[LINE_SIZE];
   char fields [2][30];
   student_t *arr;
   arr = malloc(capacity * sizeof(student_t));
   while(fgets(line, LINE_SIZE, fileOpen) != NULL){
         int no_of_token = get_token(line, fields, ",");
        // printf("%d\n",no_of_token);
         if(no_of_token != 2){
              //printf(" number of fields is not 2 for entry %d",j);
              continue;
         }
         add_record(&arr,fields[0],fields[1]);
         //printf("%s \n %s",fields[0],fields[1]);      
   }
   print(&arr);
   free(arr);
}

void check_argument(int argc,char *argv[]){
   int count =0;
   for(int i=1; i<argc; i++){
      if(strstr("-f",argv[i])){
        sprintf(filename,"%s",argv[i+1]);
        count++;
      }
   }
   if(count>1||count<1){
        printf("invalid format");
   }
   //printf("%s\n",filename);
}

int main(int argc, char *argv[]){
   check_argument(argc, argv);
   sort_data();
   return 0;
}

In the above program what i want to do is to open a (.csv)file read it line by line and then pass those line to the get_token function so token can be generated and i want only those line which have 2 fields i also want to get values of those 2 tokens so that i can pass it in add_record function and create a dynamic struct array so that i can sort it and store it in other file. but i am not getting the values of the student record in the file instead my output when i run it shows core dumped. so please help me with this problem as i am new to programming in c.And also sorry if my code hurt your eyes

  • 4
    Welcome to Stack Overflow! You seem to have posted more code than what would be reasonable for your issue. Please read [ask] and how to make a [mre]; providing a MRE helps users answer your question and future users relate to your issue. http://idownvotedbecau.se/nomcve/ – Sourav Ghosh Aug 11 '20 at 07:50
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) and [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – kaylum Aug 11 '20 at 07:51
  • 1
    In your previous question about same code I asked you to run the program in a debugger to see where your crash happens. Did you do it? – Gerhardh Aug 11 '20 at 07:56
  • 1
    segmentation faults occur when your program tries accessing memory it shouldn't be accessing. When you have this error, try to look for coding errors when allocating and freeing memory – snus74 Aug 11 '20 at 07:57
  • 3
    `strcpy(arr[size]->name, name);` The code treats `arr` as an array of pointers. But it's actually a pointer to an array. They are different things. The correct way to access that is `(*arr)[size].name`. – kaylum Aug 11 '20 at 07:58
  • 2
    Saransh, I would recommend that you edit your question with shorter, more to-the-point code, and seperate what you are trying to say into a few short paragraphs which are also to-the-point. This is **for your benefit**, because the easier to read and understand your code is, the faster we can help you. Also, many experienced users browsing around may see you question but skip it due to its poor readability. Just trying to guide you, since your new. – A P Jo Aug 11 '20 at 08:00
  • 2
    `*arr = realloc (*arr, ...` You should never directly reassign the result of `realloc` to the same pointer that was passed into the function. `realloc` may return `NULL` and you would lose your pointer. Also, of course, you should check result of all allocation functions for `NULL`. – Gerhardh Aug 11 '20 at 08:01
  • yes @Gerhardh sir i did it but i am little bit un-experienced so i was not able to find it – Saransh Dixit Aug 11 '20 at 08:12
  • 1
    Normally the debugger stops where the crash happens and you can see the line of code that caused the issue. – Gerhardh Aug 11 '20 at 08:21
  • @Gerhardh sir thanks to you i was able to solve my problem – Saransh Dixit Aug 11 '20 at 09:21

1 Answers1

1

This part

void print(student_t **arr){
    for (int i = 0; i < size; i++){
        printf("%d: %s, %f\n", i+1, arr[i]->name, arr[i]->marks);
    }
}

is problematic. You pass a pointer-to-pointer-to-student_t so when you index it expects that it's an array of pointer-to-student_t. But you actually have an array of student_t.

Try:

void print(student_t *arr){
    for (int i = 0; i < size; i++){
        printf("%d: %s, %f\n", i+1, arr[i].name, arr[i].marks);
    }
}

and call it like: print(arr);

EDIT: As pointed out in a comment by @kaylum, you have the same problem here:

strcpy(arr[size]->name, name);
arr[size]->marks = atof(marks);
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63