1

I want to store multiple lines in a string as when I press Enter, I will move to the next line and when I place ";" in input, it will terminate from getting output.

I want to write data according to this form

Name: abc
Phone :123
Address : usa ;

here I enter ";" in the last and it will terminate.

HERE is the code:

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>

// structure for message queue

struct mesg_buffer {
    long mesg_type;
    char mesg_text[100];
} message;
  
int main()
{
    key_t key;
    int msgid;
  
    // ftok to generate unique key
    key = ftok("progfile", 65);
  
    // msgget creates a message queue
    // and returns identifier
    msgid = msgget(key, 0666 | IPC_CREAT);
    message.mesg_type = 1;
  
    printf("Write Data : ");
    gets(message.mesg_text);
  
    // msgsnd to send message
    msgsnd(msgid, &message, sizeof(message), 0);

    // display the message
    printf("Data send is : %s \n", message.mesg_text);

    return 0;
}
daniel kullmann
  • 13,653
  • 8
  • 51
  • 67
  • You should make a loop. In the loop, you read an individual line into a temporary buffer and append that to a buffer that contains all the lines (be aware of buffer limits). When the line you read contains a semicolon, you break out of the loop. – daniel kullmann Jan 04 '22 at 07:50
  • not understandable – Muhammad Usman Arshad Jan 04 '22 at 07:58
  • @Muhammad Never use `gets()` please look [why gets() is dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – mohammed yaqub Jan 04 '22 at 08:43
  • Check out [How to read / parse input in C? The FAQ](https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq) – Lundin Jan 04 '22 at 09:22
  • @mohammedyaqub In fact, `gets` is not even a function in C. It's removed. – Shambhav Jan 04 '22 at 09:58

0 Answers0