title: how I correctly read struct from a file?
body: <p>I have this kind of struct:</p>
  
  <pre><code>  typedef struct {
    char a[MAX];
    char b[MAX];
    int c;
    } mys;
  </code></pre>
  
  <p>and this functions:</p>
  
  <pre><code>mys* search_m(FILE *fp, int m)
    {
      mys* s;
      s=(mys *)malloc(sizeof(mys));
      if(s){
        for(int i=0;i&lt;m;i++)
          fread(s,sizeof(mys),1,fp);
      }else{
        printf("Error during allocation\n");
      }
      return s;
    }
  
  
    void search_s(FILE *fp, char *c)
    {
      mys s;
      int count=0;
  
      while(fread(&amp;s,sizeof(mys),1,fp)==1){
  
          if(strcmp(s.b,c)==0){
            show_s(s,count);
  
          }
          count++;
      }    
       return;
    }
  </code></pre>
  
  <p>that's a part of my program. During the input fase (when I write data on my file) I don't have any problem ( using fread) but I have some problem reading it.</p>
  
  <p>The my function <strong>search_s</strong> execute and find the value every time that I call it, but the <strong>search_m</strong> function seems that can't read anby data in my file and since it's practically the same things that I did on <strong>search_s</strong> I don't know where is my mistake.</p>
  
  <p>In my main when I call <strong>seach_m</strong> I do this:</p>
  
  <pre><code>s=*search_ID(f_ptr,m);
  show_s(s,m);
  </code></pre>
  
  <p>I don't think that the problem is in the main since the fread in the <strong>search_m</strong> function doesn't load any data (it stops on the first try).</p>
  
  <p>Where is my mistake? I thought that it was all right
  PS before calling this function I open the file and I check for errors and after I close the file.</p>
  
  <p>EDIT:
  I'm opening the file with this:</p>
  
  <pre><code>f_ptr=fopen(argv[1],"rb");
  </code></pre>
  
  <p>(It doesn't work (also) if I only use "r")
  argv[1] is the name of my file</p>
  
  <p>PS m is the number of the element in the file that I want to read.
  (I read every time one block untile I don't get the m-esimo element)</p>
  
  <p>My file contains only struct of that type.</p>