define MAX_LINE 1024
void DisplayFile(void)
{
char filename[FILENAME_MAX];
FILE *infile;
// Prompt the user for a filename
puts("Enter the filename to display: ");
// Get the user's input (unsafe function call!)
gets(filename);
// Open the file in binary mode
infile = fopen(filename, "rb");
// If successful, read each character, display it
if (infile)
{
// Until we reach the end
while (!feof(infile))
{
// Get a character and display it
int c = fgetc(infile);
putc(c, stdout);
}
// Close the file (very important!)
fclose(infile);
}
else
perror(filename); // couldn't open the file
}
|
#define MAX_LINE 1024
void DisplayTextFile(void)
{
char filename[FILENAME_MAX];
FILE *infile;
// Prompt the user for a filename
puts("Enter the filename to display: ");
// Get the user's input (unsafe function call!)
gets(filename);
// Open the file for read in text/translate mode
infile = fopen(filename, "r");
// If successful, read each line, display it
if (infile)
{
char buffer[MAX_LINE];
// Until we reach the end
while (!feof(infile))
{
// Get a line and display it
//(safe function call)
if (fgets(buffer, MAX_LINE, infile))
fputs(buffer, stdout);
}
// Close the file (very important!)
fclose(infile);
}
else
perror(filename); // couldn't open the file
}
|