|
C File Processing |
|
|
|
|
|
Data On File |
|
Writing Data to a File |
|
To save a file, you must write data to its contents. This operation is performed using the fprintf() or the fwprintf() functions. Their syntaxes are: int fprintf(FILE *stream, const char *format, ...); int fwprintf(FILE *stream, const wchar_t *format, ...); The fprintf() function takes a few arguments depending on how it is used. The first parameter, stream, must be an instance of a FILE structure. The second parameter, format, is a string that specifies how data will be formatted and possibly positioned in the stream instance. The string typically starts with the % symbol followed by one or more characters that represents a format. Different formats are used depending on the type of data of the variable that is being written. You can use one the following characters: |
|
|
After specifying the format, you can type the name of the variable that is being saved. You can repeatedly use the fprintf() function for each variable you want to save. Here is an example of creating a file and letting the user supply data that would be saved to the file: // Program to perform file processing using the FILE structure
// Author: Jules Larson
#include <iostream>
using namespace std;
int main()
{
char Make[20], Model[20];
unsigned int CarYear;
long Mileage;
FILE *CarInventory = fopen("cars.inv", "w");
cout << "Enter the following pieces of information\n";
cout << "Make: "; gets(Make);
cout << "Model: "; gets(Model);
cout << "Year: "; cin >> CarYear;
cout << "Mileage: "; cin >> Mileage;
fprintf(CarInventory, "%s\n", Make);
fprintf(CarInventory, "%s\n", Model);
fprintf(CarInventory, "%d\n", CarYear);
fprintf(CarInventory, "%d\n", Mileage);
fclose(CarInventory);
cout << endl;
return 0;
}
Here is an example of running the program:
|
|
Reading Data From a File |
|
If you have a file and want to retrieve data stored from it, you can use the fscanf() or the fwscanf() function. Their syntaxes are: int fscanf(FILE *stream, const char *format[, address, ...]); int fwscanf(FILE *stream, const wchar_t *format[, address, ...]); The first parameter, stream, must be a valid instance of a FILE structure. The second parameter, format, follows the same rules as for the fprintf() and the fwprintf() functions. After typing the format, type the name of the variable that is being retrieved. Here is an example that reads data saved previously: // Program to perform file processing using the FILE structure
// Author: Jules Larson
#include <iostream>
using namespace std;
int main()
{
char Make[20], Model[20];
unsigned int CarYear;
long Mileage;
/*
FILE *CarInventory = fopen("cars.inv", "w");
cout << "Enter the following pieces of information\n";
cout << "Make: "; gets(Make);
cout << "Model: "; gets(Model);
cout << "Year: "; cin >> CarYear;
cout << "Mileage: "; cin >> Mileage;
fprintf(CarInventory, "%s\n", Make);
fprintf(CarInventory, "%s\n", Model);
fprintf(CarInventory, "%d\n", CarYear);
fprintf(CarInventory, "%d\n", Mileage);
*/
FILE *CarInventory = fopen("cars.inv", "r+");
fscanf(CarInventory, "%s\n", Make);
fscanf(CarInventory, "%s\n", Model);
fscanf(CarInventory, "%d\n", &CarYear);
fscanf(CarInventory, "%d\n", &Mileage);
cout << "Information about the car";
cout << "\nMake: " << Make;
cout << "\nModel: " << Model;
cout << "\nYear: " << CarYear;
cout << "\nMileage: " << Mileage;
fclose(CarInventory);
cout << endl;
return 0;
}
This would produce:
|
|
|
||
| Copyright © 1998-2004 FunctionX, Inc. | ||
|
|
||