⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ho21_3.c

📁 Software Development in C: A Practical Approach to Programming and Design 软件开发:编程与设计(C))——国外经典教材·计
💻 C
字号:
//--------------------------------------------------------------
/*
File Name:	Ho20_3.c
Comments:	This program reads any file in binary format and 
			displays its contents.
*/
//--------------------------------------------------------------


//--------------------------------------------------------------
// Include files

#include <stdio.h>

// end Include files
//--------------------------------------------------------------



//--------------------------------------------------------------
// Constants

#define FILE_NAME_LENGTH	256

const int ROW_LENGTH = 8;
const int ROWS_PER_SCREEN = 25;
const int PAGE_LENGTH = 20;
const char TAB_CHARACTER = (char)9;

// end Constants
//--------------------------------------------------------------


//--------------------------------------------------------------
// Types

typedef enum
{
	NO_ERROR = 0,
	CANT_OPEN_FILE,
	FILE_READ_ERROR
} error_status;


typedef enum
{
	FALSE = 0,
	TRUE
} boolean;

// end Types
//--------------------------------------------------------------


//--------------------------------------------------------------
// Prototypes

void ClearScreen(void);

// end Prototypes
//--------------------------------------------------------------



int main()
{
	FILE *inputFile=NULL;
	int i,j;
	char fileName[FILE_NAME_LENGTH];
	error_status errorStatus = NO_ERROR;
	boolean done;
	int inputValue;
	int rowNumber;


	// Prompt the user for a file name.
	fileName[0]='\0';
	do
	{
		printf("Please enter tye input file name: ");
		gets(fileName);
	} while (fileName[0]=='\0');
	
	// Open the file for reading.
	inputFile = fopen(fileName,"rb");

	// If the file was opened...
	if (inputFile)
	{
		// While the end of the file hasn't been reached...
		done = FALSE;
		rowNumber = 0;
		while (!done)
		{
			// Clear the screen.
			ClearScreen();

			// While one page of text has not been displayed...
			for (i = 0;
				 (i<PAGE_LENGTH) && (!done);
				 i++,rowNumber++)
			{
				// Display the row address on the left.
				printf("\n0x%X-0x%X:",
					   rowNumber*ROW_LENGTH,
					   rowNumber*ROW_LENGTH+(ROW_LENGTH-1));

				// Read and display a row of bytes.
				for (j=0;(j<ROW_LENGTH) && (!done);j++)
				{
					printf("%c",TAB_CHARACTER);

					// Read a value.
					inputValue = fgetc(inputFile);

					// If the value was read...
					if (inputValue!=EOF)
					{
						// Print it on the screen.
						printf("%X",inputValue);
					}
					// Else the value was not read...
					else
					{
						done = TRUE;

						// If not at the end of the file...
						if (!feof(inputFile))
						{
							// There was an error.
							errorStatus = FILE_READ_ERROR;
						}
						// Else the end of file was found...
						else
						{
							printf("\nEnd of file\n");
						}
					}
				}
			}

			// Prompt the user to continue.
			printf("\nPress the Enter key to continue...");

			// Wait for user input.
			while (getchar() != '\n');
		}
	}
	// Else the file could not be opened...
	else
	{
		// Set the error status.
		errorStatus = CANT_OPEN_FILE;
		printf("Can't open the file.\n");
	}

	return (errorStatus);
}


//--------------------------------------------------------------
/*
Function:		ClearScreen
Parameters:
	In:			None.
	Out:		None.
	In/Out:		None.
Return Values:	None.
Remarks:		This function clears the screen by printing
				newlines.
*/

void ClearScreen(void)
{
	int i;

	for (i=0;i<ROWS_PER_SCREEN;i++)
	{
		printf("\n");
	}
}

// end ClearScreen
//--------------------------------------------------------------


// end Ho20_3.c
//--------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -