Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird behavior of fread | A function behaves differently when called from main and differently when called from some other function
    primarykey
    data
    text
    <p>I wrote a function to read given number of bytes from a specified place from a file. This works as expected when called from main. But when it is called from some other function which in turn is called from main, it reads certain extra garbage characters as well (Some of which are non-printable).</p> <p>Please explain what is happening and how do I prevent it. The code and the corresponding output are given below:</p> <p>EDIT: The final goal is to calculate a hash of this data, create a packet of (data + hash) and send it via a TCP socket to a another node in the network.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; char * read_from_file(char * filename, int offset, int n_bytes) { printf("Inside read function\n"); printf("offset: %d\n",offset); printf("n_bytes: %d\n",n_bytes); char * bfr; FILE * f_ptr; int count; f_ptr = fopen (filename,"r"); if(f_ptr==NULL) { printf("File not found\n"); exit(1); } //Set the offset position fseek(f_ptr, offset , SEEK_SET); //memory aloocation bfr = malloc (sizeof(char)*n_bytes); if (bfr == NULL) { printf("Memory allocation problem\n"); exit (2); } count = fread(bfr,1,n_bytes,f_ptr); printf("no. of characters read from file: %d\n",count); printf("string read: %s\n", bfr); printf("Length of string read: %zd\n",strlen(bfr)); if (count != n_bytes) { printf("Error in reading the file"); exit(1); } // Close the file fclose (f_ptr); printf("Exiting read function\n\n"); return bfr; } int send_file()//nc_args_t * nc_args) { printf("Inside send_file\n"); char * data; data = malloc (10); data = read_from_file("alphabet.txt", 0, 10); printf("Length of data: %d\n",strlen(data)); printf("Data Read: %s\n", data); } int main() { char * data; data = read_from_file("alphabet.txt", 0, 10); printf("Length of data: %zd\n",strlen(data)); printf("Data Read: %s\n", data); printf("\nCalling send_file\n"); send_file(); } </code></pre> <p>OUTPUT</p> <pre><code>Inside read function offset: 0 n_bytes: 10 no. of characters read from file: 10 string read: ABCDEFGHIJ Length of string read: 10 Exiting read function Length of data: 10 Data Read: ABCDEFGHIJ Calling send_file Inside send_file Inside read function offset: 0 n_bytes: 10 no. of characters read from file: 10 string read: ABCDEFGHIJLsE Length of string read: 14 Exiting read function Length of data: 14 Data Read: ABCDEFGHIJLsE </code></pre>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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