0
void issueBook(){
    printf("Hii!\n");
    
    //printf("Enter your student ID: ");
    //int stdID;
    //scanf("%d", &stdID);
    
    printf("Enter the book ID\n");
    int bookID;
    scanf("%d", &bookID);
    
    FILE *in_file = fopen("recordLib.txt", "r");
    FILE *fp = fopen("temp.txt", "w");
    struct stat sb;
    stat("recordLib.txt", &sb);

    char *file_contents = malloc(1024);
    int mark = 0;
    char ID[] = "";
    while (fscanf(in_file, "%[^\n] ", file_contents) != EOF) {
    
        int size = strlen(file_contents);
        int countCom = 0;
        char ID[] = "";
        //printf("%d\n", size);
        
        if(mark == 0){
            for(int i=0; i<size; i++){
                //printf("asdfsd\n");
                //printf("%c", file_contents[i]);            
                if(countCom == 0 && (file_contents[i] != ',')){
                    strncat(ID, &file_contents[i], 1);
                    //printf("%c\n", file_contents[i]);
                }
                
                else if(atoi(ID) != bookID){
                    break;
                }
                
                else if((file_contents[i] == ',') && (countCom < 3) && (atoi(ID) == bookID)){
                    //printf("%c\n", file_contents[i]);
                    //printf("%s\n", ID);
                    countCom++;
                }
                
                else{
                    //printf("%c\n", file_contents[i]);
                    if(file_contents[i] == '1'){
                        printf("Sorry!! someone has already issued the book");
                        mark = 2;
                        break;
                    }
                    else if(file_contents[i] == '0'){
                        file_contents[i] = '1';
                        mark = 1;
                        break;
                    }
                }
            }
        }        
        fwrite(file_contents, 1, size, fp);
        fwrite("\n", 1, 1, fp);
    }  
    
    fclose(fp);
    fclose(in_file);
    remove("recordLib.txt");
    rename("temp.txt", "recordLib.txt");
    //printf("%d\n", s);
    //if(mark == 1){
      //  updateStu();
    //}
    free(file_contents);   
    
    
}

I have made this function which takes the bookID from the user and then searches for it in the file: Searching: It reads line by line and as we already know till first comma ',' it would be book ID so it will store in another string and then convert it to int. Now it will compare it with entered ID if it matches then it will update the 0 at the end to 1, else move to next line.

Error: I always getting stack smashing error when the function again returns to the main function. I am unable to find the thing that is causing the error. Also that when I run in VS code, it runs successfully but in Linux this problem occurs! Please help. Thanks in advance.

  • 1
    Both your `ID` variables are arrays of only a *single* character, the null-terminator `'\0'` (the definition `char ID[] = "";` is equivalent to `char ID[1] = { '\0' };`). Arrays are not dynamic in C, their size will be fixed. Any attempt to go out of bounds of an array (like your attempt to append characters to `ID`) leads to *undefined behavior*. – Some programmer dude Apr 25 '22 at 09:16
  • So how Do I tackle it? because as it is in loop, I need to always have an empty string :( – Arth Detroja Apr 25 '22 at 09:19
  • 1
    `char ID[1024] = "";`? Pointers, dynamic allocation and reallocation? – Some programmer dude Apr 25 '22 at 09:32
  • Side note: the first `ID` declared with `char ID[] = "";` (the one just before `while (fscanf...`) is never used. You should remove this declaration. – Jabberwocky Apr 25 '22 at 10:19

0 Answers0