Supercharging File Operations
Reference Note: In the previous section, you learned about using standard C file output operations. It is not terribly complicated but it does have a few requirements that make it less than optimum for learning about file output. Therefore, as mentioned in the previous section, this reference will use a file output utility that manages the process with much less complexity. In addition to learning about file output with loops, you will also have an opportunity to learn about this new class.
The file operations provided previously in this topic will be helpful for you to use when you are first trying out file operations. However, it gets much more interesting when you are outputting large quantities of data. For example, if you wanted to output a series of random numbers, you would probably write a function like the following.
// global constants
const int HIGH_RAND_VAL = 99;
const int ITEMS_PER_ROW = 10;
const int LOW_RAND_VAL = 10;
const int TWO_ENDLINES = 2;
const char COMMA_SPACE[] = ", ";
// function prototypes
void sendRandsToFile( char *fileName, int numRands );
int getRandBetween( int lowVal, int highVal );
// main function
int main()
{
// initialize variables
srand( time(0) );
char fileName[] = "randfile.txt";
int numRands = 25;
// send random values to file
// function: sendRandsToFile
sendRandsToFile( fileName, numRands );
// return success
return 0;
}
void sendRandsToFile( char *fileName, int numRands )
{
// initialize variables
int counter, nextVal;
// open file
// function: openOutputFile
openOutputFile( fileName );
// loop up to number of random values requested
for( counter = 1; counter <= numRands; counter++ )
{
// get the next random value
// function: getRandBetween
nextVal = getRandBetween( LOW_RAND_VAL, HIGH_RAND_VAL );
// output random value
// function: writeInt
writeIntegerToFile( nextVal );
// check for needing a comma after all values but the last one
if( counter != numRands )
{
// send comma/space to file
// function: writeString
writeStringToFile( COMMA_SPACE );
}
// check for end of row of values
if( counter % ITEMS_PER_ROW == 0 )
{
// send two end lines to file
// function: writeEndlines
writeEndlinesToFile( TWO_ENDLINES );
}
}
// end loop
// add one more end of line
// function: writeEndline
writeEndlineToFile();
// flush and close the file
// function: closeOutputFile
closeOutputFile();
}
int getRandBetween( int lowVal, int highVal )
{
// initialize variables, set range and random value
int range = highVal - lowVal + 1;
int randVal = rand();
// return calculated random value
// between low/high, inclusive
return randVal % range + lowVal;
}
Watch this video to see the whole program that includes this function. As a bonus, the program also demonstrates the use of randomly generated integers, and how the random generation process is set up and implemented in a program.
The above function would open a file for output, load the file with the specified number of random values, and then close the file. For the condition: LOW_RAND_VAL = 10, HIGH_RAND_VAL = 99, ITEMS_PER_ROW = 5, and numRands = 17, the following might be found in the text file to which this data was sent.
65, 42, 19, 55, 57,
22, 14, 98, 33, 15,
27, 11, 44, 13, 18,
85, 93
There is one thing . . .
It is important to remember that the output operation opens a file and puts data into it. It won’t concern itself over whether a file was there or not. If a file of the same name is in the same directory, this program will overwrite it without any indication or error message. This makes the file output actions pretty easy. You open it, you send data to it, and you close it. You don’t have to do any testing and you don’t have to have had a file already in place. However, if you meant to save the contents of the previous file, that's a problem.
Wrapping Up File Output
This is just one example of how you can output data to a file, also known as outputting, writing, or downloading to a file. There will be many more opportunities to output quantities of data using loops so be prepared to manage these. You can see how the commas are managed so that a comma does not appear before the first number or after the last number, and you can also see how the rows are limited to 5 items per row. These and the outputting process are all important, but again, no part of the outputting process is very complicated.
The file output utilities can be found here, the header file is here, and the standard constants header file needed by this utility can be found here.
Your next step is to start thinking about getting data back from a file, called file input. You will find this discussion in the next topics.