Sunday, September 11, 2011

reading a file in C [reading line]


#include <stdio.h>
int main ()
{
   
  FILE * pFile;
  char lines[2][200];
  int i=0;
  int j=0;
  int c;
  int n = 0;
   //clear file
  for(int i=0;i<2;i++) {
      for(int j=0;j<200;j++) {
              lines[i][j] = '\0';
      }      
  }
  pFile=fopen ("name.txt","r");
  if (pFile==NULL) perror ("Error opening file");
 
  else
  {
    do {
      c = fgetc (pFile);
      if (c == '$') n++;
        if(c== '\n') {
              i++;
              j=0;
              continue;
              printf("new line begins\n");
       }
       lines[i][j] = c;
        printf ("%c",lines[i][j]);
     
       j++; 
    } while (c != EOF);
    fclose (pFile);
   printf("line1 %s \n",lines[0]);
   printf("line2 %s \n",lines[1]);
  }
  return 0;
}


No comments:

Post a Comment