Read File


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

Comments (0)

  • To add your comment please or

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

Got It!