title: Load data from file in C
body: <p>I'm creating a simple C program - employee database... My data are stored in a .txt file, separated by commas and \n, so it looks like this:</p>
  
  <blockquote>
    <p>data1,id1,name1</p>
    
    <p>data2,id2,name2</p>
  </blockquote>
  
  <p>What is the best way (in C, not C++) to load all data from the file and every value save as independent variable (e.g. p->data, p->id, p->name)?</p>
  
  <p>This is the code I use to store data into variables (not into the file, variables just for the script):</p>
  
  <pre><code>    void insert(emp *p,int n)
  {
    p=p+n;
    printf("\nenter name of emplyee:");
    _flushall();
    gets(p-&gt;name);
    printf("\nenter employee id:");
    scanf("%d",&amp;p-&gt;empid);
    printf("\nenter salary of the emplyee:");
    scanf("%d",&amp;p-&gt;salary);
    printf("\nenter phone no of the emplyee:");
    flushall();
    gets(p-&gt;ph);
  }
  </code></pre>
  
  <p>And this script saves data to a file:</p>
  
  <pre><code>void sejv(emp *p,int n)
  {
        int i;
          FILE *fptr;
      fptr=fopen("ulozka.txt","w+");
      if(fptr==NULL){
        printf("Error opening file!");   
        getchar();             
     }
    printf("Ukladani...");
    for(i=0;i&lt;n;i++)
    {
     fprintf(fptr,"%d,%s,%d,%s\n",p-&gt;empid,p-&gt;name,p-&gt;salary,p-&gt;ph);   
  
      p=p+1;
    }
  
     fclose(fptr);
     exit(0);
   }
  </code></pre>
  
  <p>All I need is to load the data to the main function so I can work with it.</p>
  
  <p>EDIT:
  I'm adding the rest of my code:</p>
  
  <pre><code>typedef struct employee
  {
    char name[20],ph[20];
    int empid,salary;
  }emp;
  
  void main()
  {
    emp e;
    emp *p;
    int n=0,ch;
    p=&amp;e;
  }
  </code></pre>
  
  <p>And "something" from the function I need to create...</p>
  
  <pre><code>void nacti(emp *p,int n)
  {
        int i;
          FILE *fptr;
      fptr=fopen("ulozka.txt","r");
      if(fptr==NULL){
        printf("Error opening file!");   
        getchar();             
     }
   printf("Nacitani...");
  
  //loading function
        p=p+n;
     fscanf(fptr,"%d,%[^\,],%d,%s", &amp;p-&gt;empid, p-&gt;name, &amp;p-&gt;salary,p-&gt;ph);
     printf("%s", p-&gt;name);
  
  
   }
  </code></pre>
  
  <p>I need a function that would read the data from the txt file by collumns and rows (a loop that would add the data from the first row to a p, then increase the value of p by 1, insert data from the second row and so on...)</p>
  
  <p>Thank you for any advice...</p>