IO
Write into a File with Redirection
#include <stdio.h>

int main()
{
	printf("Hello World!\n");

	return 0;
}
		
gcc hello.c
./a.out > output.txt // redirect outputs to a file
./a.out >> output.txt // attach outputs to the exist file, if the file does not exist, create it
		
Write into a File
#include <stdio.h>
#include <stdlib.h>

int main()
{
	FILE * fp; // define a file pointer
	fp = fopen ("info.txt", "w"); // open a file with writing mode

	// exit program if the file cannot be openned
	if (fp == NULL)
        	exit(EXIT_FAILURE);

	// write into file
	fprintf(fp, "%10s: %10d\n", "Lin Chen", 800);
	fprintf(fp, "%10s: %10s\n", "Office", "2205");

	fclose(fp);

	return 0;
}
		
Read a File Line by Line
// letters.txt
abcdefg
hijklmn
opqrstu
vwxyz
		
#include <stdio.h>
#include <stdlib.h>

int main()
{
	FILE* filePointer; // define a file pointer

	// create an array to save the string
	int bufferLength = 255;
	char *buffer = (char *)malloc(bufferLength*sizeof(char));

	// open the file
	filePointer = fopen("letters.txt", "r");

	// exit program if the file cannot be openned
	if (filePointer == NULL)
        	exit(EXIT_FAILURE);

	// read file line by line
	while(fgets(buffer, bufferLength, filePointer)) {
	     	printf("%s\n", buffer);
	}

	// close file
	fclose(filePointer);

	return 0;
}
		
Access Specified Location of a File
#include <stdio.h>
#include <stdlib.h>

void readChar(FILE *fp)
{
	printf("Current location: %ld, ", ftell(fp));
	char c = fgetc(fp); // read a character and move to next character
	printf("|%c|\n", c);
}

int main()
{
	FILE* filePointer; // define a file pointer

	// open the file
	filePointer = fopen("letters.txt", "r");

	readChar(filePointer); // a
	readChar(filePointer); // b

	fseek(filePointer, 2, SEEK_CUR); // SEEK_CUR represents the current location
	readChar(filePointer); // e

	fseek(filePointer, 2, SEEK_SET); // SEEK_SET represents the start location of a file
	readChar(filePointer); // c

	rewind(filePointer); // go back to the beginning of the file
	char c;
	while((c = fgetc(filePointer)) != EOF)
	{
		printf("%c", c);
	}

	// close file
	fclose(filePointer);

	return 0;
}
		
Parameters to C Program
#include <stdio.h>

int main(int argc, char *argv[])
{
	for(int i = 0; i < argc; i++)
	{
		printf("%ith parameter: %s\n", i, argv[i]);
	}

	return 0;
}
		
gcc main.c
./a.out 1 2 3 Hello World
		
Standard Files
  • 0, stdin, keyboard
  • 1, stdout, screen
  • 2, stderr, screen
  • #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	FILE * fp;
    	fp = fopen("Unknown.txt", "r");
    
    	if(fp == NULL) // output information to stderr
    	{
    		perror("Cannot open Unknown.txt ...");
    		fprintf(stderr, "Unknown.txt is not found\n");
    	}
    	else // output information to stdout
    	{
    		printf("Openned Unknown.txt successfully ...\n");
    		fprintf(stdout, "Opened Unknow.txt\n");
    
    		fclose(fp);
    	}
    
    	return 0;
    }
    		
    // if Unknown.txt does not exist, print error message to stderr
    gcc main.c
    ./a.out // print to screen
    ./a.out 2> error.txt // redirect stderr to a file, no space between 2 and >
    		
    // if Unknown.txt exists, print message to stdout
    gcc main.c
    ./a.out // print to screen
    ./a.out 1> message.txt // redirect stdout to a file, no space between 1 and >