标签:style io os ar 使用 for sp 文件 数据
【ios中的枚举常量】#include <iostream>
#include <strstream>
using namespace std;
int main(int argc, char* argv[]) {
char buf[] = "1002000";
int i , j;
istrstream s1(buf);
s1 >> i;
istrstream s2(buf, 3); //读进3个字符
s2 >> j;
cout << i + j <<endl;
return 0;
}#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
ofstream ofile;
cout << "Create file1" << endl;
ofile.open("test.txt");
if(!ofile.fail()) {
ofile << "lasolmi" << endl;
ofile.close();
cout << "Create file2" << endl;
ofile.open("test2.txt");
if(!ofile.fail()) {
ofile << "hello.world!" <<endl;
ofile.close();
}
}
return 0;
}#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(int argc, char* argv[]) {
char buf[128];
ofstream ofile("test.txt");
for(int i=0;i<5;i++) {
memset(buf,0,128);
cin >> buf;
ofile << buf;
}
ofile.close();
ifstream ifile("test.txt");
while(!ifile.eof()) {
char ch;
ifile.get(ch);
if(!ifile.eof()) cout << ch;
}
cout << endl;
ifile.close();
return 0;
}#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
fstream file("test.txt",ios::out);
if(!file.fail()) {
cout << "start write" << endl;
file << "lasolmi" << endl;
}
else cout << "can not open" <<endl;
file.close();
return 0;
}#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
fstream file("test.txt",ios::in);
if(!file.fail()) {
while(!file.eof()) {
char buf[128];
file.getline(buf,128);
if(file.tellg() > 0) {
cout <<buf;
cout <<endl;
}
}
}
else cout << "can not open" <<endl;
file.close();
return 0;
}ifstream infile;
ofstream outfile;
infile.open(infilename);
if(!infile) {cout<<"fail open infile"<<endl;}
outfile.open(outfilename);
if(!outfile) {cout<<"fail open outfile"<<endl;}
while(infile.get(c)) outfile << c;
infile.close();
outfile.close();fstream iofile("test.dat",ios::in|ios::out|ios::binary);
if(iofile) {
iofile.seekp(0,ios::end); //为了写入移动
iofile << endl;
iofile << "我是新加入的";
iofile.seekg(0); //为了写入移动
char data[100];
for(int i=0;!iofile.eof() && i<sizeof(data);i++) iofile.get(data[i]);
cout << data;
}#include <iostream>
#include <fstream>
using namespace std;
int main(int argc,char* argv[]) {
ifstream ifile;
char cFileSelect[20];
cout << "input filename:";
cin >> cFileSelect;
ifile.open(cFileSelect);
if(!ifile) {
cout << cFileSelect << " can not open" << endl;
return 0;
}
ifile.seekg(0,ios::end);
int maxpos = ifile.tellg();
int pos;
cout << "Position:";
cin >> pos;
if(pos > maxpos)
cout << "is over file length" << endl;
else {
char ch;
ifile.seekg(pos);
ifile.get(ch);
cout << ch <<endl;
}
ifile.close();
return 0;
}#include <iostream>
#include <fstream>
using namespace std;
int main(int argc,char* argv[]) {
char file[50];
cout << "input file name:" << "\n";
cin >> file;
if(!remove(file))
cout << "The file:" << file <<"已删除" << endl;
else
cout << "The file:" << file << "删除失败" <<endl;
return 0;
}标签:style io os ar 使用 for sp 文件 数据
原文地址:http://blog.csdn.net/lasolmi/article/details/40583219