fstream/stringstream操作

//若打开文件时,一般是必须有std::ios_base::in或std::ios_base::out,但若是app则默认会加上out,
//其他情况则返回失败

//指定app后,读取时还是从头读取的,若是写入,则是被附加到后面

//ate是打开的时候定位在末尾,之后seekp可以写在任意位置。
//app是TMD永远写在末尾。

//C++ Primer:The only to preserve the existing data in a file opend by an ofstream is to
// specify app mode explicitely.
//只有app才能抑制trunc,事实上两者是不能同时存在的。而ate仅将文件指针移到末尾,但并没有抑制trunc
#include <fstream>
//1.以默认方式打开的文件必须存在,否则由于"读",而文件并不存在出错(蛋疼。。)
//2.不要用ios::ate,打开后原来的内容被删除
//3.std::fstream析构函数会调用close(),所以不显式调用也可以

int _tmain(int argc, _TCHAR* argv[])
{
	std::fstream logfile("fstream.log", std::ios_base::out | std::ios_base::app);
	if ( logfile.fail() ) {
		std::cout << GetLastError() << std::endl;
	}

	logfile << "hello, world";

	char buf[128] = { 0 };
//	logfile >> buf;

	std::cout << buf << std::endl;
	logfile.close();
	return 0;
}


// generic_test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>

#include <string>
#include <iostream>
#include <sstream>

/*
	1.如何判断一个数为整数:
	double x;
	scanf("%f",&x);
	if((int)x==x)
        printf("%f是整数",x);
	2.为什么%f不能输出整数:
	%f 标明是输出小数形式,那么输出函数就输出小数所占用字节的内存块里的值(假设64位),
	而你提供的是整数(假设32位),那么输出函数取得的值是从整数本身那32位及接下去的32位共64位存储地址上取值,和整数值不同的,也是错误的。

	3.stringstream通常是用来做数据转换的,相比c库的转换,它更加安全,自动和直接
	
	4.C++标准库中sstream和strstream的区别  (strstream已废除,由新版本stringstream替代)

在C++有两种字符串流,一种在sstream中定义,
另一种在strstream中定义。
它们实现的东西基本一样。

strstream里包含
class strstreambuf;
class istrstream;
class ostrstream;
class strstream;
它们是基于C类型字符串char*编写的

sstream中包含
class istringstream;
class ostringstream;
class stringbuf;
class stringstream;
class …….
它们是基于std::string编写的

因此ostrstream::str()返回的是char*类型的字符串
而ostringstream::str()返回的是std::string类型的字符串

在使用的时候要注意到二者的区别,一般情况下推荐使用std::string类型的字符串
当然如果为了保持和C的兼容,使用strstream也是不错的选择。

*/
int _tmain(int argc, _TCHAR* argv[])
{
	int n = 10;
	char s[10];
//	sprintf(s, "%f", n);

//	std::cout << s << std::endl;

	std::string str;

	std::stringstream strstream;
	strstream << 1000;
	strstream >> str;
	std::cout << str.c_str() << std::endl;

	strstream.clear();
	strstream.str();

	std::string str2;
	strstream >> str2;
	std::cout << str2.c_str() << std::endl;

	/*
		stringstream 主要是用在將一個字串分割,
		可以先用 clear( ) 以及 str( ) 將指定字串設定成一開始的內容,再用 >> 把個別的資料輸出,
		例如:
	*/

	std::string str3;
	int a, b, c;
	std::getline(std::cin, str3);
	strstream.clear();
	strstream.str(str3);
	strstream >> a >> b >> c;
	std::cout << a << " " << b << " " << c << std::endl;

	{

		std::string str4;
		int n, i, sum, a;
		std::cin >> n;
		std::getline(std::cin, str4);
		for( i = 0; i < n; i++ )
		{
			std::getline( std::cin, str4 );
			strstream.clear();
			strstream.str(str4);
			sum = 0;
			while ( 1 )
			{
				strstream >> a;
				if ( strstream.fail() ) break;
				sum += a;
			}

			std::cout << sum << std::endl;
		}
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值