[C++]读写txt文件和从txt文件中读取数据写入数组中

一、利用C++读写txt文件:

全篇转载http://blog.csdn.net/lh3325251325/article/details/4761575(他也是转载的)下面的代码没有逐一验证,

主要是为了学习这个框架。

  1. #include <iostream>  
  2. #include <iomanip>  
  3. #include <fstream>  
  4.   
  5. using namespace std;  
  6.   
  7. int main(){  
  8. char buffer[256];  
  9. ifstream myfile ("c://a.txt");  
  10. ofstream outfile("c://b.txt");  
  11.   
  12. if(!myfile){  
  13.   cout << "Unable to open myfile";  
  14.         exit(1); // terminate with error  
  15.   
  16. }  
  17. if(!outfile){  
  18.     cout << "Unable to open otfile";  
  19.         exit(1); // terminate with error  
  20.   
  21. }  
  22. int a,b;  
  23. int i=0,j=0;  
  24. int data[6][2];  
  25.   while (! myfile.eof() )  
  26.   {  
  27.      myfile.getline (buffer,10);  
  28.     sscanf(buffer,"%d %d",&a,&b);  
  29.     cout<<a<<" "<<b<<endl;  
  30.      data[i][0]=a;  
  31.      data[i][1]=b;  
  32.      i++;  
  33.   }  
  34. myfile.close();  
  35.   for(int k=0;k<i;k++){  
  36.       outfile<<data[k][0] <<" "<<data[k][1]<<endl;  
  37.      cout<<data[k][0] <<" "<<data[k][1]<<endl;  
  38.   }  
  39.   
  40. outfile.close();  
  41. return 0;  
  42. }  
  43.   
  44.   
  45.   
  46.   
  47. 本文来自CSDN博客,转载请标明出处:http://blog.csdn<a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET知识库" target='_blank' style='color:#df3434; font-weight:bold;'>.NET</a>/dragonworrior/archive/2009/11/02/4759484.aspx  

 

无论读写都要包含<fstream>头文件

读:从外部文件中将数据读到程序中来处理
对于程序来说,是从外部读入数据,因此定义输入流,即定义输入流对象:ifsteam infile,infile就是输入流对象。
这个对象当中存放即将从文件读入的数据流。假设有名字为myfile.txt的文件,存有两行数字数据,具体方法:

[cpp]  view plain  copy
  1. int a,b;  
  2. ifstream infile;  
  3. infile.open("myfile.txt");      //注意文件的路径  
  4. infile>>a>>b;                   //两行数据可以连续读出到变量里  
  5. infile.close()  
  6.   
  7. //如果是个很大的多行存储的文本型文件可以这么读:  
  8. char buf[1024];                //临时保存读取出来的文件内容  
  9. string message;  
  10. ifstream infile;  
  11. infile.open("myfile.js");  
  12. if(infile.is_open())          //文件打开成功,说明曾经写入过东西  
  13. {  
  14. while(infile.good() && !infile.eof())  
  15. {  
  16.     memset(buf,0,1024);  
  17.     infile.getline(buf,1204);  
  18.     message = buf;  
  19.     ......                     //这里可能对message做一些操作  
  20.     cout<<message<<endl;  
  21. }  
  22. infile.close();  
  23. }  

写:将程序中处理后的数据写到文件当中
对程序来说是将数据写出去,即数据离开程序,因此定义输出流对象ofstream outfile,outfile就是输出流对象,这个对象用来存放将要写到文件当中的数据。具体做法:

[cpp]  view plain  copy
  1. ofstream outfile;  
  2. outfile.open("myfile.bat"); //myfile.bat是存放数据的文件名  
  3. if(outfile.is_open())  
  4. {  
  5. outfile<<message<<endl;    //message是程序中处理的数据  
  6.    outfile.close();   
  7. }  
  8. else  
  9. {  
  10.    cout<<"不能打开文件!"<<endl;  
  11. }  

c++对文件的读写操作的例子

[c-sharp] view plain copy
  1. /*/从键盘读入一行字符,把其中的字母依次放在磁盘文件fa2.dat中,再把它从磁盘文件读入程序, 
  2. 将其中的小写字母改成大写字母,再存入磁盘fa3.dat中*/   
  3. #include<fstream>  
  4. #include<iostream>  
  5. #include<cmath>  
  6. using namespace std;  
  7. //从键盘上读取字符的函数  
  8. void read_save(){  
  9.       char c[80];  
  10.       ofstream outfile("f1.dat");//以输出方工打开文件  
  11.       if(!outfile){  
  12.                    cerr<<"open error!"<<endl;//注意是用的是cerr   
  13.                    exit(1);  
  14.                    }  
  15.           cin.getline(c,80);//从键盘读入一行字符  
  16.           for(int i=0;c[i]!=0;i++) //对字符一个一个的处理,直到遇到'/0'为止   
  17.                 if(c[i]>=65&&c[i]<=90||c[i]>=97&&c[i]<=122){//保证输入的字符是字符   
  18.                    outfile.put(c[i]);//将字母字符存入磁盘文件   
  19.                    cout<<c[i]<<"";  
  20.                    }  
  21.                    cout<<endl;  
  22.                    outfile.close();  
  23.                    }  
  24. void creat_data(){  
  25.       char ch;  
  26.       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);//以输入的方式打开文件   
  27.       if(!infile){  
  28.                   cerr<<"open error!"<<endl;  
  29.                   exit(1);  
  30.                   }  
  31.     ofstream outfile("f3.dat");//定义输出流f3.dat文件  
  32.     if(!outfile){  
  33.                  cerr<<"open error!"<<endl;  
  34.                  exit(1);  
  35.                  }  
  36.      while(infile.get(ch)){//当读取字符成功时   
  37.      if(ch<=122&&ch>=97)  
  38.      ch=ch-32;  
  39.      outfile.put(ch);  
  40.      cout<<ch;  
  41.      }  
  42.      cout<<endl;  
  43.      infile.close();  
  44.      outfile.close();  
  45.      }  
  46.      int main(){  
  47.          read_save();  
  48.          creat_data();  
  49.         system("pause");  
  50.          return 0;  
  51.          }   

二、从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/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值