C++整行读取.txt文件
来源:互联网 发布:经期自慰 知乎 编辑:程序博客网 时间:2024/06/10 01:01
读取文件 lhc.txt, 内容如下:
zhongguo 1 2 3 4 chinese
#include<iostream>
#include<sstream>#include<string>
#include<fstream>
int main()
{
std::string filename = "lhc.txt";
std::ifstream infile( filename.c_str() );
std::string str1, str2 ;
int a[4];
std::string lineStr;
std::stringstream ss;
while ( std::getline(infile,lineStr) )
{
ss << lineStr;
ss >> str1;
for(int i=0; i<4; i++ )
ss >> a[i];
ss >> str2;
}
std::cout << " str1 is " << str1 << " str2 is " << str2 << std::endl;
for (int i=0; i<4; i++)
std::cout << " the " << i << " num is: " << a[i] << std::endl;
return 1;
}
////注意的是: ss.clear() ; //如果你想通过使用同一stringstream对象实现多种类型的转换,请注意在每一次转换之后都必须调用clear()成员函数。
-----------------------------------------------------------------------------------
将数据写入到.txt文件
#include<stdio.h> // this is C?
int main(){
std::cout << "hello, world" << std::endl ;
FILE *f_buffer ;
f_buffer = fopen("zz_lhc.txt", "w") ; // this will create the file, if it does not exit.
fprintf( f_buffer, "%s ", "hello" ) ;
double a = 2.3 ;
fprintf( f_buffer, "%f\n", a ) ; // \n will change to another line.
fprintf( f_buffer, "%f\n", a ) ;
fclose( f_buffer ) ;
return 1 ;
}
- C#--读取txt文件
- C#Winform读取txt文件
- C#StreamReader 读取Txt文件
- C#winform读取.txt文件
- C++整行读取.txt文件
- C格式化读取txt文件内容
- 第十五章,读取txt文件(C++)
- 【C#】读取和写入本地txt文件
- c++,对txt文件进行读取显示
- c语言一行一行的读取txt文件
- C/C++ | 17-1 读取文件.txt
- C读取txt教程
- C\C++读取txt
- C读取txt文档
- 随机读取txt文件某一行
- Java按行读取txt文件
- C# 按行读取txt记事本文件
- 随机读取txt文件的某一行
- 几何图形案例(openGL)
- 实战c++中的智能指针unique_ptr系列--通过unique_ptr对shared_ptr进行初始化
- HDU 1205 吃糖果(鸽巢原理)
- 堆排序的java简单实现以及讲解
- IO_字符流_纯文本_节点流_读取_写出_拷贝JAVA151
- C++整行读取.txt文件
- 【算法导论】最优二叉搜索树
- Linux—命令效率大于拖拖拽拽
- 数据库之基础2
- equals、HashCode与实体类的设计
- Deep Learning(深度学习)学习笔记整理
- centos7.0装搜狗拼音 sougou
- ugui ui相对位置的计算,以及如何把ui限制在屏幕内
- Android 编辑框增加圆角边框