C code that will open a file and print the contents to the terminal.

This code will print the contents of a file to the terminal. Feel free to use this in your own projects if you wish.

#include <stdio.h>
 
#define MEM "/proc/meminfo"
 
int main (void) {
 
	FILE *g;
	char Meminfo[40];
	g = fopen(MEM, "r");
	if(!g) {
		printf ("Sorry, I cannot" \
			"open: %s.\n", MEM);
	} else {
		while (feof(g) != 1) {
			fgets(Meminfo, 2, g);
				printf("%s", Meminfo );
		}
		fflush(stdout);
	}
	fclose(g);
}

This code is very fast and useful for opening a text file and reading the contents.

1 responses to “C code that will open a file and print the contents to the terminal.

Leave a Reply