To use file I/O in C, you generally have stdio.h included at the top of your file (most C programs include this anyway):
#include <stdio.h>
In C, you declare a file pointer variable for any file you want to use. For example, the following line declares a file pointer variable called "infile" (our standard name for the file to be read from. You can call yours anything you like).
FILE *infile;
To open the file, you call the function "fopen()", passing it a filename and "r" for reading or "w" for writing. For example, to open a file called mydata.txt, you would use:
infile = fopen("mydata.txt","r");
Then to read from the file, you use a variation of scanf() called fscanf() which takes a file as its first parameter. For example, to read an integer from mydata.txt, you would use:
fscanf(infile,"%d",&myint);
Other than the fact that it's reading from a file, fscanf() is identical to fprintf().
Once you've read all the data you want to, close the file using the fclose() function:
fclose(infile);
To write to a file, you do the exact same thing, except you use "w" rather than "r" in the fopen() line and fprintf() rather than fscanf() to write the data out.
Potential Error Conditions:
Whenever you open a file, it's a good idea to check and make sure that
there weren't any problems in doing so. Problems can include trying to
open a file for reading when it doesn't exist. Or trying to open a file
for writing in a directory that you don't have write permissions in. If
your C file pointer is NULL after calling fopen() it indicates that there
was a problem.
Reading or writing a file that had problems when opening it can lead to error conditions or your program crashing.
Reading or writing a file that had problems when opening it can lead to error conditions or your program crashing.
Creating Data Files
The data files that contain integers are simply text files that contain
values for your array in sorted order. Thus, one way to create them would
be to open your favorite editor and start typing numbers in, saving the
file as a text file. However, it's likely that to get times that are large
enough for you to see asymptotic trends, you'll have to run on problem
sizes that are larger than you're willing to type in (1000 values? 10,000
values? 1,000,000 values?)
So... an easy way to create these data files is just to write a small
program that creates them (free advice: always make computers do work that
you're not willing to do yourself whenever you can). Writing this small
program from scratch would be a great chance to practice your file I/O
skills. Or if you want to, you can look at the simple ones below. Note,
that we don't check to see whether there were problems opening the files.
Note also that the consecutive numbers 1 through 10 may not be the best
input set to cause the worst case performance for some of the search algorithms.
Example
void main() {
int i;
FILE *outfile;
outfile = fopen("out.dat","w");
for (i=0;i<10;i++) {
fprintf(outfile,"%d\n",i+1);
}
fclose(outfile);
}
0 komentar:
Posting Komentar