Read File

C Programming

C Programming Examples


int main() {

	/* Declare File pointer */
	FILE *fp;
	char text, text1[10];

	/* Open file 'myfile.txt' in read mode */
	fp = fopen("myfile.txt", "r");

	/* Read character on file using 'fgetc' function */
	printf("Get text as character from file\n");
	while( (text = fgetc(fp) ) != EOF )
		printf("%c", text);

	/* Go to the beginning of the file */

	/* Read text on file using 'fscanf' function */
	printf("Read file up to first blank space or line break\n");
	fscanf(fp, "%s", text1);
	printf("%s\n", text1);

	/* Close file */
	return 0;

          /****** Output ******/

Get text as character from file


Hello World!

Read file up to first blank space or line break


Learn more about File I/O in C