1

SOURCE ::

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h> // strcmp()

#define MAX 255

struct book {
    int Book_ID;
    char* Title;
    char* Publisher;
    char* Author;
    int Price;
} book_db;

int main(int argc, char *argv[])
{   
    char cBuf[MAX];  
    int fd, id;
    char c;
   
    if (argc < 2) {      
    fprintf(stderr, "manual : %s file_name\n", argv[0]);  
    }
    if ((fd = open(argv[1], O_WRONLY|O_CREAT|O_EXCL, 0640)) == -1) {     
    perror(argv[1]);   
    }    
    
    while(1) {    
    int number; 
    printf("\nThis is Seong-Jae Database Book Management System\n");    
    printf("1. Add book\n");    
    printf("2. Update book\n"); 
    printf("3. Find book\n");   
    printf("4. Exit\n");      
    printf(">> ");   
    fgets(cBuf, MAX, stdin);    
   
    if(strncmp(cBuf, "1", 1) == 0){ 
            printf("%-9s %-15s %-15s %-10s %-10s\n", "Book_ID","Title", "Publisher", "Author", "Price");
            scanf("%d %s %s %s %d", &book_db.Book_ID, book_db.Title, book_db.Publisher, book_db.Author, &book_db.Price) == 5;
            lseek(fd, book_db.Book_ID * sizeof(book_db), SEEK_SET);
            write(fd, (char *) &book_db, sizeof(book_db));
    } // case1 END
    else if(strncmp(cBuf, "4", 1) == 0){        
        break;
    }
    } // while
} // main

OUTPUT::

*@ubuntu:~/myshell/pl02$ gcc -o db_manage db_mng.c 
*@ubuntu:~/myshell/pl02$ ./db_manage db_test.db

This is Database Book Management System
1. Add book
2. Update book
3. Find book
4. Exit
>> 1
Book_ID   Title           Publisher       Author     Price     
1         unix            shell           sj         13000

This is Database Book Management System
1. Add book
2. Update book
3. Find book
4. Exit
>> 
This is Database Book Management System
1. Add book
2. Update book
3. Find book
4. Exit
>> 

The result I thought is that printf statements are executed in the while statement, and when they meet fgets and receive input while waiting for input, the conditional statement according to the input is executed.

However, the result value is input to fgets, the result corresponding to the if statement "1" is executed, and the printf statement is executed twice more.

How to solve this?

Desired result :: ex)

Book_ID Title Publishr author price
1              unix shell        sj         13000

This is Seong-Jae Database Book Management System
1. Add book
2. Update book
3. Find book
4. Exit
>>  

It makes the above wait state and executes the function when another value is entered later.

+) Currently, functions 2 and 3 have not been implemented, so only functions 1 and 4 are included.

god-jae
  • 23
  • 5
  • 1
    Please explain your idea of the `== 5;`. – Yunnosch Oct 17 '20 at 08:23
  • Please provide a [mre] to demonstrate your problem. – Yunnosch Oct 17 '20 at 08:24
  • Please see [fgets() doesn't work after scanf](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf). The `scanf()` leavs a newline in the input buffer. This is read as an "empty" string by the next `fgets()` (it contains only a newline). – Weather Vane Oct 17 '20 at 08:27
  • 1
    Translated Korean to: *How do I get `fgets` to run after `scanf` or where can I fix it?* By reading all the inputs with `fgets()` and applying `sscanf()` to the input string, instead of applying `scanf()` to `stdin`. – Weather Vane Oct 17 '20 at 08:32
  • @Weather Vane Note: I modified the code. There is a part to be edited, so I deleted the comment and wrote it again, but you already posted it. Thank you. However, I have difficulty in translating when using Google Translator because I am not proficient in English. I would really appreciate it if you could write it in sentences. ㅡㅡㅡㅡㅡㅡㅡ Why does the @ function not work..? Yunnosch --> 'book_id title publish author price' I specified ==5 to accept 5 inputs. Also, the desired results have been added to the text. – god-jae Oct 17 '20 at 08:44
  • 1
    You should read all input with fgets. Then apply sscanf to the string. – Weather Vane Oct 17 '20 at 08:46
  • 1
    So you will input to `mystring` with `fgets` and then `if(sscanf(mystring, "%d %s %s %s %d", ...) != 5) { /* error */ }` – Weather Vane Oct 17 '20 at 08:52
  • 1
    Thank you so much for your help... It worked out well. Have a nice day :) – god-jae Oct 17 '20 at 09:10

1 Answers1

0

Use a definitive break; behind an if-block and not else if-block, which is "calculated" each iteration,

Like:

while(1){
/* your code */
if(/* condition is no longer true */) break;
}
paladin
  • 765
  • 6
  • 13