Note that there are some explanatory texts on larger screens.

plurals
  1. POreading characters line by line from a text file in c with multiprocessing
    text
    copied!<p>hi there i'm facing with <code>null</code> character problem while i'm reading and try to printing characters of a file with using <code>fseek()</code> function in a multiprocess programme. here is my simple code,</p> <pre><code>#include &lt;stdio.h&gt; /* basic I/O routines. */ #include &lt;unistd.h&gt; /* define fork(), etc. */ #include &lt;sys/types.h&gt; /* define pid_t, etc. */ #include &lt;sys/wait.h&gt; /* define wait(), etc. */ #include &lt;signal.h&gt; /* define signal(), etc. */ #include &lt;pthread.h&gt; #include &lt;time.h&gt; void print_screen(int i); int counter=0; int main(int argc, char* argv[]) { FILE* fptr; fptr = fopen("sample.txt","w"); int counter = atoi(argv[1]); int i,k; int temp; pid_t child_pid; int child_status; char array[counter]; srand ( time(NULL) ); for(i=0; i&lt;counter; i++){ temp = rand()%4; if( temp==0 ) { fprintf(fptr,"A\n"); array[i]='A'; } else if( temp==1 ) { fprintf(fptr,"C\n"); array[i]='C'; } else if( temp==2 ) { fprintf(fptr,"G\n"); array[i]='G'; } else if( temp==3 ) { fprintf(fptr,"T\n"); array[i]='T'; } } fclose(fptr); for(i=1; i&lt;=counter; i++){ child_pid = fork(); switch(child_pid) { case -1: printf("Error occured with fork()\n"); exit(1); case 0: print_screen(i); /* Child Process */ exit(0); } } wait(&amp;child_status); execl("/usr/bin/killall","killall","tail",(char *) 0); return 0; } void print_screen(int i){ char* str; FILE* fptr; fptr=fopen("sample.txt","r"); fseek(fptr,i,SEEK_SET); fscanf(fptr,"%s",str); printf("Process Number %d, Character = %s\n",i,str); sleep(1); fclose(fptr); return; } </code></pre> <p>Suppose that i enter <code>./sample 10</code> in to command line so programme will print 10 characters into sample.txt and then 10 child processes will be created, each of them try to pick a character and print onto the screen.moreover, as you can see i send <code>i</code> as a parameter to set the offset.but as i mentioned it prints null. this is the outlook the programme.</p> <pre><code>Process Number 7, Character = (null) Process Number 6, Character = (null) Process Number 5, Character = (null) Process Number 3, Character = (null) Process Number 8, Character = (null) Process Number 4, Character = (null) Process Number 9, Character = (null) Process Number 10, Character = (null) Process Number 2, Character = (null) Process Number 1, Character = (null) </code></pre> <p>and the txt file was like this.</p> <pre><code>G A A T G C C A A T </code></pre> <p>i will appreciated if you can help and thanks anyway.</p> <p>edit: i realized that i compile like <code>$ gcc sample.c -o sample -lpthread</code> it prints out null. On the other hand, i compile it without <code>-lpthread</code> it prints character but not properly for example like this is the text file.</p> <pre><code>T G G T G </code></pre> <p>and terminal gives the output like this.</p> <pre><code>Process Number 1, Character = G Process Number 2, Character = G Process Number 3, Character = G Process Number 4, Character = G Process Number 5, Character = T </code></pre>
 

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