Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For saving the data in a file, you can use file stream. For that you have to know about file streams.</p> <p>Hope these link will help.</p> <p><a href="http://linux.die.net/man/3/fopen" rel="nofollow">http://linux.die.net/man/3/fopen</a></p> <p><a href="http://en.wikipedia.org/wiki/C_file_input/output" rel="nofollow">http://en.wikipedia.org/wiki/C_file_input/output</a></p> <p>Here comes the example program which I particularly typed, compiled and attached it for your reference.</p> <pre><code>#include&lt;sys/types.h&gt; #include&lt;string.h&gt; #include&lt;sys/ipc.h&gt; #include&lt;sys/shm.h&gt; #include&lt;stdio.h&gt; struct mystruct // you can make your own structure if you want to pass many data { int i; float f; char c; int arr[3]; }myObj = {1,1.1,'C',{100,1000,10000}}; main() { int shmid; char* addr1; key_t key; //file to key. Generates a unique key key = ftok("/home/muthu/Desktop/anyfile.txt",'T'); shmid = shmget(key,sizeof(struct mystruct),IPC_CREAT|SHM_R|SHM_W); printf("shmid = %d",shmid); addr1 = shmat(shmid,0,0); printf("\nIPC SHARED MEMORY"); //copying your structure at the shared location. memcpy(addr1,&amp;myObj,sizeof(myObj)); printf("\nMESSAGE STORED"); } </code></pre> <p>And for shared memory 2.....</p> <pre><code>//&lt;All necessary header files&gt; //&lt;same my struct declaration here&gt; main() { int shmid; char* addr1; FILE* fp; key_t key; struct mystruct* myObj2; //Generate the same unique key. Must provide the same file here too. key = ftok("/home/muthu/Desktop/anyfile.txt",'T'); shmid = shmget(key,sizeof(struct mystruct),SHM_R|SHM_W); addr1 = shmat(shmid,0,0); myObj2 = (struct mystruct*)malloc(sizeof(struct mystruct)*1); if(shmid == -1) printf("\nShared memory error"); //Retrieve the stored information, form the shared location. memcpy(myObj2,addr1,sizeof(struct mystruct)); fp = fopen("/home/muthu/Desktop/MyFile.txt","w"); //open a file stream if(fp == NULL) printf("\nError on opening file stream.\n"); fprintf(fp,"\nIPC SHARED MEMORY"); fprintf(fp,"\nMESSAGE RECIEVED FORM THE SHARED MEMORY IS..\n"); fprintf(fp,"\ninteger: %d",myObj2-&gt;i); fprintf(fp,"\nfloat: %f",myObj2-&gt;f); fprintf(fp,"\nchar: %c",myObj2-&gt;c); //write to the file fprintf(fp,"\narr: %d %d %d",myObj2-&gt;arr[0],myObj2-&gt;arr[1],myObj2-&gt;arr[2]); fprintf(fp,"\nDATA RECIEVED."); fclose(fp); //close the file stream printf("\nMessage successfully stored!"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload