c++ 基本文件操作的演示
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int getFileSize(ifstream *in){
int size = 0;
in->seekg(0,ios::end);
int endI = in->tellg();
in->seekg(0,ios::beg);
int beginI = in->tellg();
size = endI - beginI;
return size;
}
int main(int argc, char *argv[])
{
ifstream fina;
fina.open("test.txt");
fina.seekg(0,ios::end);
int endI = fina.tellg();
fina.seekg(0,ios::beg);
int beginI = fina.tellg();
int size = endI - beginI;
//*/
//int size = getFileSize(&fina);
cout <<"file size: "<<size<<endl;
if(fina.fail()){
cout <<"fail"<<endl;
return EXIT_SUCCESS;
}else{
cout <<"success"<<endl;
}
char ch[20];
//fina.get(ch);
fina >> ch;
cout <<"get: "<<ch<<endl;
fina.close();
//
ifstream *fp = new ifstream();
fp->open("test.txt");
size = getFileSize( fp );
cout<<"fp size: "<<size<<endl;
char buff[size];
char tbch;
int j = 0;
while(!fp->eof()){
fp->get(tbch);
buff[j] = tbch;
j++;
}
if(buff[59] == '\0'){
cout <<"buff 有结尾符号"<<endl;
}
fp->close();
ifstream finb;
finb.open("test.txt");
int MAX_LENGTH = 60;
char line[MAX_LENGTH];
while(finb.getline(line,MAX_LENGTH)){
cout <<" "<<line<<endl;
}
finb.close();
if(line[59] == '\0'){
cout <<"有结尾符号"<<endl;
}
cout <<"B file size: "<<sizeof(line)<<endl;
//
ifstream finc;
finc.open("test.txt");
char text[100];
char tempch;
int i = 0;
while(!finc.eof()){
finc.get(tempch);
text[i] = tempch;
i++;
}
//text[i] = '\0';//这一句就不需要了
finc.close();
cout <<"text:\n "<<text<<endl;
ifstream fin( "test.txt" );
ofstream fout( "results.txt", ios::app );
char tch;
fin >> tch;
cout<<"tch: "<<tch<<endl;
int temp;
while( fin >> temp )
//fin >> temp;
cout<<"temp: "<<temp<<endl;
fout << temp + 2 << endl;
fin.close();
fout.close();
// 读取二进制文件
//ofstream fout("file.dat", ios::binary
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
#include <fstream>
using namespace std;
int getFileSize(ifstream *in){
int size = 0;
in->seekg(0,ios::end);
int endI = in->tellg();
in->seekg(0,ios::beg);
int beginI = in->tellg();
size = endI - beginI;
return size;
}
int main(int argc, char *argv[])
{
ifstream fina;
fina.open("test.txt");
fina.seekg(0,ios::end);
int endI = fina.tellg();
fina.seekg(0,ios::beg);
int beginI = fina.tellg();
int size = endI - beginI;
//*/
//int size = getFileSize(&fina);
cout <<"file size: "<<size<<endl;
if(fina.fail()){
cout <<"fail"<<endl;
return EXIT_SUCCESS;
}else{
cout <<"success"<<endl;
}
char ch[20];
//fina.get(ch);
fina >> ch;
cout <<"get: "<<ch<<endl;
fina.close();
//
ifstream *fp = new ifstream();
fp->open("test.txt");
size = getFileSize( fp );
cout<<"fp size: "<<size<<endl;
char buff[size];
char tbch;
int j = 0;
while(!fp->eof()){
fp->get(tbch);
buff[j] = tbch;
j++;
}
if(buff[59] == '\0'){
cout <<"buff 有结尾符号"<<endl;
}
fp->close();
ifstream finb;
finb.open("test.txt");
int MAX_LENGTH = 60;
char line[MAX_LENGTH];
while(finb.getline(line,MAX_LENGTH)){
cout <<" "<<line<<endl;
}
finb.close();
if(line[59] == '\0'){
cout <<"有结尾符号"<<endl;
}
cout <<"B file size: "<<sizeof(line)<<endl;
//
ifstream finc;
finc.open("test.txt");
char text[100];
char tempch;
int i = 0;
while(!finc.eof()){
finc.get(tempch);
text[i] = tempch;
i++;
}
//text[i] = '\0';//这一句就不需要了
finc.close();
cout <<"text:\n "<<text<<endl;
ifstream fin( "test.txt" );
ofstream fout( "results.txt", ios::app );
char tch;
fin >> tch;
cout<<"tch: "<<tch<<endl;
int temp;
while( fin >> temp )
//fin >> temp;
cout<<"temp: "<<temp<<endl;
fout << temp + 2 << endl;
fin.close();
fout.close();
// 读取二进制文件
//ofstream fout("file.dat", ios::binary
system("PAUSE");
return EXIT_SUCCESS;
}

浙公网安备 33010602011771号