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

Posted: January 13, 2015. At: 2:11 AM. This was 3 years ago. Post ID: 8011
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

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