文件操作--不断更新
2016-05-04 14:28
155 查看
C++
打开文件,读取字符串:
#include <fstream> #include <string> #include <iostream> using namespace std; int main() { ifstream ifs("test.txt"); // 改成你要打开的文件 streambuf* old_buffer = cin.rdbuf(ifs.rdbuf()); string read; while(cin >> read) // 逐词读取方法 cout << read; cin.rdbuf(old_buffer); // 修复buffer
读取int类型
freopen("in2.txt","r",stdin); for(i=0; i<10000; i++) { scanf("%d",&a); arr[i]=a; Insert(a); }
string转int
// int sprintf( char *buffer, const char *format, [ argument] … ); //参数列表 // buffer:char型指针,指向将要写入的字符串的缓冲区。 // format:格式化字符串。 // [argument]...:可选参数,可以是任何类型的数据。 // 返回值:字符串长度(strlen) int aa = 30; char c[8]; int length = sprintf(c, "%05X", aa); cout<<c<<endl; // 0001E
方法2
int aa = 30; stringstream ss; ss<<aa; string s1 = ss.str(); cout<<s1<<endl; // 30 string s2; ss>>s2; cout<<s2<<endl; // 30
string转int
string s = "17"; stringstream ss; ss<<s; int i; ss>>i; cout<<i<<endl; // 17
相关文章推荐
- HDU 5677 ztr loves substring(回文串加多重背包)
- 相关淘宝的名词解释,SPU 关键属性 销售属性 商品属性 非关键属性 SKU AppKey 等
- 测试网站访问速度的5个方法
- android压缩图片的算法,加水印,图片验证码
- PHP文件包含漏洞
- X-code利用cocapods给工程中添加第三方的详细步骤 此博文包含图片 (2015-12-23 21:48:57)
- ZOJ 3935 2016(水题)
- AbstractSequentialList源码分析
- java枚举应用(一)
- HashMap循环遍历方式及其性能对比
- OpenStack 与 大数据的融合
- Java生成CSV文件实例详解
- 通过ip判断是否可以访问的程序
- 翻翻git之---实用的进度类自定义控件LoadingDrawable
- json对象与数组以及转换成js对象的方法
- Android aChartEngine使用方法
- 翻翻git之---实用的进度类自定义控件LoadingDrawable
- ajax中的一些参数的含义及用法
- jQuery中的ajax服务端返回方式详细说明
- 遇到问题时,如何跟客户沟通