#include
#include
#include
const int MAXLEN = 256; /* Maximum file path length */
const int DISPLAY = 80; /* Length of display line */
const int PAGE_LENGTH = 20; /* Lines per page */
int main(int argc, char *argv[])
{
char filename[MAXLEN]; /* Stores the file path */
FILE *pfile; /* File pointer */
unsigned char buffer[DISPLAY/4 - 1]; /* File input buffer */
int count = 0; /* Count of characters in buffer */
int lines = 0; /* Number of lines displayed */
if(argc == 1) /* No file name on command line? */
{
printf("Please enter a filename: "); /* Prompt for input */
fgets(filename, MAXLEN, stdin); /* Get the file name entered */
/* Remove the newline if it's there */
int len = strlen(filename);
if(filename[len-1] == '\n')
filename[len-1] = '\0';
}
else
strcpy(filename, argv[1]); /* Get 2nd command line string */
/* File can be opened OK? */
if(!(pfile = fopen(filename, "rb")))
{
printf("Sorry, can't open %s", filename);
return -1;
}
while(!feof(pfile)) /* Continue until end of file */
{
if(count < sizeof buffer) /* If the buffer is not full */
buffer[count++] = (unsigned char)fgetc(pfile); /* Read a character */
else
{ /* Output the buffer contents, first as hexadecimal */
for(count = 0; count < sizeof buffer; count++)
printf("%02X ", buffer[count]);
printf("| "); /* Output separator */
/* Now display buffer contents as characters */
for(count = 0; count < sizeof buffer; count++)
printf("%c", isprint(buffer[count]) ? buffer[count]:'.');
printf("\n"); /* End the line */
count = 0; /* Reset count */
if(!(++lines%PAGE_LENGTH)) /* End of page? */
if(getchar()=='E') /* Wait for Enter */
return 0; /* E pressed */
}
}
/* Display the last line, first as hexadecimal */
for(int i = 0; i < sizeof buffer; i++)
if(i < count)
printf("%02X ", buffer[i]); /* Output hexadecimal */
else
printf(" "); /* Output spaces */
printf("| "); /* Output separator */
/* Display last line as characters */
for(int i = 0; i < count; i++)
/* Output character */
printf("%c",isprint(buffer[i]) ? buffer[i]:'.');
/* End the line */
printf("\n");
fclose(pfile); /* Close the file */
return 0;
}
Program to Viewing the contents of a file in C
Labels:
Basics of C,
c,
C Assignments
Subscribe to:
Post Comments (Atom)
If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE
More Useful Topics... |
|
History Of C..
In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.
My 1st Program...
#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}
Next Step...
#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");
printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");
}
No comments:
Post a Comment