C++/File/fstream
Содержание
Demonstrate File random access.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
if(argc != 3) {
cout << "Usage: CHANGE <filename> <byte>\n";
return 1;
}
fstream out(argv[1], ios::in | ios::out | ios::binary);
if(!out) {
cout << "Cannot open file.\n";
return 1;
}
out.seekp(atoi(argv[2]), ios::beg);
out.put("X");
out.close();
return 0;
}
File Stream Objects as Function Arguments
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
bool writeFile (ofstream&, char*);
bool readFile (ifstream&, char*);
int main ()
{
string data;
bool status;
ofstream outfile;
status = writeFile(outfile, "students.dat");
if (!status)
{
cout << "File could not be opened for writing\n";
cout << "Program terminating\n";
return 0;
}
else
{
cout << "Writing to the file" << endl;
cout << "Enter class name: ";
getline(cin, data);
outfile << data<< endl;
cout << "Enter number of students: ";
cin >> data;
cin.ignore();
outfile << data<< endl;
outfile.close();
}
ifstream infile;
status = readFile(infile, "students.dat");
if (!status)
{
cout << "File could not be opened for reading\n";
cout << "Program terminating\n";
return 0;
}
else
{
cout << "Reading from the file" << endl;
getline(infile, data);
while(!infile.fail())
{
cout << data << endl;
getline(infile, data);
}
infile.close();
}
return 0;
}
bool writeFile (ofstream& file, char* strFile)
{
file.open(strFile);
if (file.fail())
return false;
else
return true;
}
bool readFile (ifstream& ifile, char* strFile)
{
ifile.open(strFile);
if (ifile.fail())
return false;
else
return true;
}
fstream seekp: Seek file pointer position
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
if( argc != 4) {
cout << "Usage: CHANGE <filename> <character> <char>\n";
return 1;
}
fstream out(argv[1], ios::in | ios::out | ios::binary);
if(!out) {
cout << "Cannot open file.";
return 1;
}
out.seekp(atoi(argv[ 2 ]), ios::beg);
out.put(*argv[ 3 ]);
out.close();
return 0;
}
Reverse file content
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
if(argc!=3) {
cout << "Usage: Reverse <filename> <num>\n";
return 1;
}
fstream inout(argv[1], ios::in | ios::out | ios::binary);
if(!inout) {
cout << "Cannot open input file.\n";
return 1;
}
long e, i, j;
char c1, c2;
e = atol(argv[2]);
for(i = 0, j = e; i < j; i++, j--) {
inout.seekg(i, ios::beg);
inout.get(c1);
inout.seekg(j, ios::beg);
inout.get(c2);
inout.seekp(i, ios::beg);
inout.put(c2);
inout.seekp(j, ios::beg);
inout.put(c1);
}
inout.close();
return 0;
}
Reverses the first N characters within a file
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
long n, i, j;
char ch1, ch2;
if(argc!=3) {
cout << "Usage: Reverse <filename> <num>\n";
return 1;
}
fstream finout(argv[1], ios::in | ios::out | ios::binary);
if(!finout) {
cout << "Cannot open input file.\n";
return 1;
}
n = atol(argv[2]) - 1;
for(i=0, j=n; i < j; ++i, --j) {
finout.seekg(i, ios::beg);
finout.get(ch1);
finout.seekp(j, ios::beg);
finout.put(ch1);
if(!finout.good()) {
cout << "Error reading or writing characters.";
finout.clear();
break;
}
}
finout.close();
if(!finout.good()) {
cout << "A file error occurred.";
return 1;
}
return 0;
}
To read or write to a file, you include <fstream>
#include <fstream>
int main()
{
using namespace std;
int intValue;
float realValue;
ifstream inData;
ofstream outData;
inData.open("input.dat");
outData.open("output.dat");
inData >> intValue;
inData >> realValue;
outData << "The input values are "
<< intValue << " and "
<< realValue << endl;
return 0 ;
}