Fri Jun 22 2018

Read File

C Programming964 views

File Name: read-file.c

#include<stdio.h>

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);
	printf("\n");

	/* Go to the beginning of the file */
	fseek(fp,0L,0);

	/* 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 */
	fclose(fp);								
	return 0;
}


/* Output */
Get text as character from file
Hello!
Hello World!
Read file up to first blank space or line break
Hello!
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.