一、利用C++读写txt文件:
全篇转载http://blog.csdn.net/lh3325251325/article/details/4761575(他也是转载的)下面的代码没有逐一验证,
主要是为了学习这个框架。
- #include <iostream>
- #include <iomanip>
- #include <fstream>
-
- using namespace std;
-
- int main(){
- char buffer[256];
- ifstream myfile ("c://a.txt");
- ofstream outfile("c://b.txt");
-
- if(!myfile){
- cout << "Unable to open myfile";
- exit(1);
-
- }
- if(!outfile){
- cout << "Unable to open otfile";
- exit(1);
-
- }
- int a,b;
- int i=0,j=0;
- int data[6][2];
- while (! myfile.eof() )
- {
- myfile.getline (buffer,10);
- sscanf(buffer,"%d %d",&a,&b);
- cout<<a<<" "<<b<<endl;
- data[i][0]=a;
- data[i][1]=b;
- i++;
- }
- myfile.close();
- for(int k=0;k<i;k++){
- outfile<<data[k][0] <<" "<<data[k][1]<<endl;
- cout<<data[k][0] <<" "<<data[k][1]<<endl;
- }
-
- outfile.close();
- return 0;
- }
-
-
-
-
- 本文来自CSDN博客,转载请标明出处:http:
无论读写都要包含<fstream>头文件
读:从外部文件中将数据读到程序中来处理
对于程序来说,是从外部读入数据,因此定义输入流,即定义输入流对象:ifsteam infile,infile就是输入流对象。
这个对象当中存放即将从文件读入的数据流。假设有名字为myfile.txt的文件,存有两行数字数据,具体方法:
- int a,b;
- ifstream infile;
- infile.open("myfile.txt");
- infile>>a>>b;
- infile.close()
-
-
- char buf[1024];
- string message;
- ifstream infile;
- infile.open("myfile.js");
- if(infile.is_open())
- {
- while(infile.good() && !infile.eof())
- {
- memset(buf,0,1024);
- infile.getline(buf,1204);
- message = buf;
- ......
- cout<<message<<endl;
- }
- infile.close();
- }
写:将程序中处理后的数据写到文件当中
对程序来说是将数据写出去,即数据离开程序,因此定义输出流对象ofstream outfile,outfile就是输出流对象,这个对象用来存放将要写到文件当中的数据。具体做法:
- ofstream outfile;
- outfile.open("myfile.bat");
- if(outfile.is_open())
- {
- outfile<<message<<endl;
- outfile.close();
- }
- else
- {
- cout<<"不能打开文件!"<<endl;
- }
c++对文件的读写操作的例子
-
-
- #include<fstream>
- #include<iostream>
- #include<cmath>
- using namespace std;
-
- void read_save(){
- char c[80];
- ofstream outfile("f1.dat");
- if(!outfile){
- cerr<<"open error!"<<endl;
- exit(1);
- }
- cin.getline(c,80);
- for(int i=0;c[i]!=0;i++)
- if(c[i]>=65&&c[i]<=90||c[i]>=97&&c[i]<=122){
- outfile.put(c[i]);
- cout<<c[i]<<"";
- }
- cout<<endl;
- outfile.close();
- }
- void creat_data(){
- char ch;
- ifstream infile("f1.dat",<a href="http://lib.csdn.net/base/ios" class='replace_word' title="iOS知识库" target='_blank' style='color:#df3434; font-weight:bold;'>iOS</a>::in);//以输入的方式打开文件
- if(!infile){
- cerr<<"open error!"<<endl;
- exit(1);
- }
- ofstream outfile("f3.dat");
- if(!outfile){
- cerr<<"open error!"<<endl;
- exit(1);
- }
- while(infile.get(ch)){
- if(ch<=122&&ch>=97)
- ch=ch-32;
- outfile.put(ch);
- cout<<ch;
- }
- cout<<endl;
- infile.close();
- outfile.close();
- }
- int main(){
- read_save();
- creat_data();
- system("pause");
- return 0;
- }
二、从txt文件中读取数据写入数组中
直接看代码吧,通过代码来学习:
#include<iostream>
#include<fstream>
using namespace std;
void test() {
char* FileName = "E://ZTE//InputFile.txt";
ifstream myfile(FileName, ios::in);
}
int main() {
char* FileName = "E://ZTE//InputFile.txt";
ifstream infile(FileName);
const int size = 18; //txt保存的数据是18*18的矩阵
int array[size][size] = { 0 };
int* ptr = &array[0][0];
while (!infile.eof())
{
infile >> *ptr;
ptr++;
}
cout << "******测试********" << endl;
for (int i = 0;i < 18;i++) { //i<18和下面的j<18表示我的txt文件中的数据是18*18的矩阵
for (int j = 0;j < 18;j++) {
cout << array[i][j] << " ";
}
cout << "\n";
cout << endl;
}
system("pause");
return 0;
}
输出结果截屏:
关于文件读写的函数可以看下面链接:
http://blog.csdn.net/kingstar158/article/details/6859379/