File IO for contest problems

Most contests simply say read from standard input and write to standard output. This is good. Unfortunately, there are a few brain damaged contests where you have to use files. The purpose of this is to show you examples of how to do file IO for contest problems. The goal is to have a quick reference so you don't have waste time looking stuff up in a reference book.

This is a programming contest. Hence you may assume that the file will be there (ie, don't waste time checking to see if you really opened up the file. You did.) The information and examples listed below are not the only things you can do with files. However, it should be enough to get you through the contest.

C IO

C I/O is done through the use of FILE pointers. #include <stdlib.h> to get them. The relavent functions are fopen, fprintf, fgets, and fclose. Here's an example of how to read lines in from a file until you hit the EOF:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main (void)
{
	FILE *fp;
	char s [200];

	fp = fopen ("a.in", "r");

	while (fgets (s, 200, fp)) {
		s [strlen (s) - 1] = '\0';
		printf ("Entered: %s\n", s);
	}

	return 0;
}

fopen is the function to open up a file. "a.in" is the name of the file to read from. "r" tells fopen you want the file to be read-only. It returns a file pointer. The fgets function works similarly to gets, except fgets will only read a maximum number of characters. s is the character array to read into, the 200 is the max number of characters to read, and fp is the file pointer to read from. fgets will read characters in until it hits a newline character or the max number of characters were read in. fgets returns NULL if it its the end of file.

note the weird looking s [strlen (s) - 1] = '\0'; statement. fgets does NOT remove the newline character. After fgets runs, the newline character will be the last character before the NULL. most of the time this is not a desirable condition. the above statement removes the newline character by sticking a NULL character overtop of the newline.

Here's an example of how to do file output in C:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main (void)
{
	FILE *fp;

	fp = fopen ("foo.out", "w");
	fprintf (fp, "This gets written to the file.\n");
	fclose (fp);
	return 0;
}

Again, you use fopen to open up the file. foo.out is the name of the file to write to. It will be created if it doesn't exists. If it does exist, it will be overwritten (you can change that behavior. go read the man page on fopen). The "w" means to open the file for writing. fprintf is used to write to the file. It has all the same syntax as the regular printf command, except the first argument is a file pointer to write to. Once you're done writing, call fclose to flush the buffers and make sure the file is written out to disk.

That's it for C. There are many other functions you can use to write/read to/from a file. The above, though, should be enough in a contest.

C++ IO

C++ is an entirely different beast from C. C uses the FILE pointers to do everything, C++ uses ofstream and ifstream objects. To get them #include <iostream>, <iomanip>, and <fstream>. Output uses ofstream objects. Input use ifstream objects. Here's an example of how to read lines from a file in C++:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int
main (void)
{
	ifstream in ("a.in");
	string s;

	while (getline (in, s)) {
		cout << "Entered: " << s << endl;
	}

	return 0;
}

The key here is to instantiate an ifstream object. In the example, I called it `in'. Passing "a.in" to it's constructor tells in to open up the file a.in for reading. Once the file is opened you can read things in the same way you would for cin (except instead of using cin, use in). The above example reads in one line of input using the getline method. Once we hit the EOF, getline returns 0 and we break out of the loop. Here's another example that reads in a list of integers from a file:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int
main (void)
{
	ifstream in ("b.in");
	int n;

	while ((in >> n)) {
		cout << "Entered: " << n << endl;
	}

	return 0;
}

Same deal as above for opening up the file. To read from it, I used the stream extraction operator. That will return NULL once you hit EOF.

C++ output is done in a similar fashion, except now we use ofstream objects. Here's an example:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int
main (void)
{
	ofstream out ("foo.out");

	out << "Hi.  this gets written to a file." << endl;	
	out.close ();

	return 0;
}

to open the file, I passed "foo.out" to the constructor of the out object. That tells out to open up the file "foo.out" for writing. Once opened, out works just like cout does. (ie, you use the stream insertion operators to get stuff written) Instead of being written to the screen, out will write data to the file. Once done writing, call the close () method to flush any data out to disk.

As with C, there are other methods that can used to read/write with these objects. Go look them up in a reference manual if you need them.


Ben Breech
breech@cis.udel.edu

Programming team homepage