Simple file reader... I'm learning

#include 

#define BUFSIZE 1024

void usage(char *cmd)
{
 printf("Usage: %s \n", cmd);
 printf("If your file is at /home/sudha/documents/names.txt, then the command will be: \n");
 printf("%s /home/sudha/documents/names.txt\n", cmd);
}

int main(int argc, char *argv[])
{
 FILE *fp;

 if (argc < 2)
 {
   usage(argv[0]);
   exit(0);
 }

 fp = fopen(argv[1], "r");
 if (fp == NULL)
 {
   usage(argv[0]);
   exit(0);
 }

 printf("Contents of %s are: \n", argv[1]);

 while (!feof(fp))
 {
   char buf[BUFSIZE] = {0};

   fread(buf, sizeof(char), BUFSIZE, fp);
   printf("%s", buf);
 }

 printf("End of the file\n");

 fclose(fp);
}

0 comments:

Post a Comment

| More

Twitter Updates