title: How to read and write registers from a CSV file (in C)?
body: <p>I have a .CSV file and I need to read it and write on a binary file. </p>
  
  <p>I've tried to adapt some codes that I saw in some similar questions, but didn't worked.</p>
  
  <p>My file is like:</p>
  
  <pre><code>nroInscricao,nota,data,cidade,nomeEscola
  439,607.5,01/01/2004,Maceio,PEDRO II
  387,,,Sao Paulo,JOAO KOPKE
  332,400.8,03/01/2004,Brasilia,REINALDO RIBEIRO DA SILVA DOU
  296,436.4,04/01/2004,,JOSE CANDIDO DE SOUZA
  </code></pre>
  
  <p>And I tried to read the file with the following code:</p>
  
  <pre><code>const char* getfield(char* line, int num){
  
      const char* tok;
      for (tok = strtok(line, ","); tok &amp;&amp; *tok; tok = strtok(NULL, ",\n")){
          if (!--num)
              return tok;
      }
      return NULL;
  
  }
  
  
  int main(){
  
      FILE* stream = fopen("C:\\Users\\10734140\\Downloads\\SCC0503012019trabalho1.csv", "r+");
  
      char line[1024];
      while (fgets(line, 1024, stream)){
          char* tmp = strdup(line);
          printf("Field 3 would be %s\n", getfield(tmp, 3));
          // NOTE strtok clobbers tmp
          free(tmp);
      }
  }
  </code></pre>
  
  <p>But nothing came out in the execution.</p>