title: C++ : read csv file with fgetc and separate words on semicolon ";"
body: <p>I have to read in a csv file with 5 fields (int , char[], char[], char[], float) that looks like that :</p>
  
  <pre><code>2345678;Meier;Hans;12.10.1985;2.4;      
  1234567;Müller;Fritz;17.05.1990;1.9;
  </code></pre>
  
  <p>I have to put the fields in a struct, and then put the struct after one line is complete, into a array of the struct type ...</p>
  
  <p>for the learning effect, we are only allowed to use LOW-LEVEL coding, and only use functions like fgetc, strcpy and no strings, only char[]...
  Now I made my algorithm to read the textfile character by character, but I have problems separating them correctly, putting them together again and assigning them to the struct fields correctly. Here is my Code:</p>
  
  <pre><code>  #include &lt;cstdlib&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string.h&gt;
  
  using namespace std;
  
  int main(int argc, char **argv)
  {
      struct Stud{
          long matrnr;
          char vorname[30];
          char name[30];
          char datum[30];
          float note;
      };
  
      const int MAX = 30;
      Stud stud;  
      Stud mystud[30]; // &lt;&lt;-- Array of "Stud" type
      //memset((void*)mystud,0,sizeof(mystud) * sizeof(Stud));
      int wordCounter(0);
      int i(0); //thats the charCounter or index
      int studentCounter(0);
      char wort[MAX];
      //int matrnr;
      //char vorname[MAX];
      //char name[MAX];
      //char datum[MAX];
      //float note;
  
  
    FILE * pFile;
    int cnr(0); 
  
  
    pFile=fopen("studentendaten.txt","r");  
    if (pFile==nullptr) 
    {
        perror ("Fehler beim öffnen der Datei");
    }
  
    else
    {       
      while (cnr != EOF) 
      {       
          (cnr=fgetc(pFile)) ;
  
  
          if ((char)cnr == '\n') {
              mystud[studentCounter] = stud;
              studentCounter++;                       
              continue;           
          }
  
          if ((char)cnr == ';') { 
  
              wort[i] = '\0'; 
  
              switch (wordCounter % 5) {
  
                  case 0:             
                  stud.matrnr = atol(wort);
                  break;
  
                  case 1:
                  strcpy(stud.name, wort);
                  break;
  
                  case 2:
                  strcpy(stud.vorname, wort);
                  break;
  
                  case 3:
                  strcpy(stud.datum,wort);
                  break;
  
                  case 4:
                  stud.note = atof(wort); 
                  break;
              }       
  
              wordCounter++;          
              i = 0;
              continue;
          }
  
          if (wordCounter %  5 == 0 &amp;&amp; (char)cnr != ';') {        
          wort[i] = (char)cnr;
          i++;
          //stud.matrnr = atol(wort);
          }           
  
          if (wordCounter % 5 == 1) {
              wort[i] =  (char)cnr;
              i++;
          //strcpy(stud.name, wort);
          }
  
          if (wordCounter % 5 == 2) {
              wort[i] = (char)cnr;
              i++;
              //strcpy(stud.vorname, wort);
          }
  
          if (wordCounter % 5 == 3) {
              wort[i] = (char)cnr;
              i++;
              //strcpy(stud.datum,wort);
          }
  
          if (wordCounter % 5 == 4) {
              wort[i] = (char)cnr;
              i++;
              //stud.note = atof(wort);                       
          }
  
      }   
  
  
      fclose (pFile);
  }
  for (int i(0) ; i &lt;= studentCounter; i++) {
  cout &lt;&lt;mystud[i].matrnr &lt;&lt; "    " &lt;&lt; mystud[i].name &lt;&lt; "    " &lt;&lt; mystud[i].vorname &lt;&lt;"    " 
  &lt;&lt; mystud[i].datum &lt;&lt;"    " &lt;&lt; mystud[i].note &lt;&lt; endl;
    //printf("%5ld        %5s      %5s     %5s     %5f     \n",mystud[i].matrnr,mystud[i].name,mystud[i].vorname,mystud[i].datum,mystud[i].note);
  
  }
  
      return 0;
  }
  </code></pre>
  
  <p>I am not sure if it has to do with a wrong increment variables, or the fact that I don't put an '\0' at the  end of my wort[] array..and therefore not recognizing the end of my array? And if so, how do I do it without knowing where the end exactly is... ? (I don't know the length of the words..)</p>
  
  <p>EDIT: I updated my code again, the only thing that wonders me is that the LAST LINE IS NOT BEING CORRECTLY PARSED , its showing some rubbish, and I can't see the error in my code...</p>
  
  <pre><code>2345678;Meier;Hans;12.10.1985;2.4;      
  1234567;Müller;Fritz;17.05.1990;1.9;
  8392019;Thomas;Kretschmer;28.3.1920;2.5;
  3471144;Mensch;Arbeit;29.2.2013;4.5;
  2039482;Test;Test;30.20.2031;2.0;
  7584932;Bau;Maschine;02.02.2010;2.3;
  2345678;Meier;Hans;12.10.1985;2.4;      
  1234567;Müller;Fritz;17.05.1990;1.9;
  8392019;Thomas;Kretschmer;28.3.1920;2.5;
  3471144;Mensch;Arbeit;29.2.2013;4.5;
  2039482;Test;Test;30.20.2031;2.0;
  7584932;Bau;Maschine;02.02.2010;2.3;
  2345678;Meier;Hans;12.10.1985;2.4;      
  1234567;Müller;Fritz;17.05.1990;1.9;
  8392019;Thomas;Kretschmer;28.3.1920;2.5;
  3471144;Mensch;Arbeit;29.2.2013;4.5;
  2039482;Test;Test;30.20.2031;2.0;
  7584932;Bau;Maschine;02.02.2010;2.3;
  2345678;Meier;Hans;12.10.1985;2.4;      
  1234567;Müller;Fritz;17.05.1990;1.9;
  8392019;Thomas;Kretschmer;28.3.1920;2.5;
  3471144;Mensch;Arbeit;29.2.2013;4.5;
  2039482;Test;Test;30.20.2031;2.0;
  7584932;Bau;Maschine;02.02.2010;2.3;
  </code></pre>