Cpp读文件、CString转String、String转CString
场景
C++读取文件
技术点
读取文件
fstream提供了三个类,用来实现c++对文件的操作。(文件的创建、读、写)。
ifstream -- 从已有的文件读入
ofstream -- 向文件写内容
fstream - 打开文件供读写
文件打开模式:
ios::in 只读
ios::out 只写
ios::app 从文件末尾开始写,防止丢失文件中原来就有的内容
ios::binary 二进制模式
ios::nocreate 打开一个文件时,如果文件不存在,不创建文件
ios::noreplace 打开一个文件时,如果文件不存在,创建该文件
ios::trunc 打开一个文件,然后清空内容
ios::ate 打开一个文件时,将位置移动到文件尾
文件指针位置在c++中的用法:
ios::beg 文件头
ios::end 文件尾
ios::cur 当前位置
例子:
移动的是字节,不是行。
file.seekg(0,ios::beg); //让文件指针定位到文件开头
file.seekg(0,ios::end); //让文件指针定位到文件末尾
file.seekg(10,ios::cur); //让文件指针从当前位置向文件末方向移动10个字节
file.seekg(-10,ios::cur); //让文件指针从当前位置向文件开始方向移动10个字节
file.seekg(10,ios::beg); //让文件指针定位到离文件开头10个字节的位置
常用的错误判断方法:
good() 如果文件打开成功
bad() 打开文件时发生错误
eof() 到达文件尾
按行读取文件
const int bufsize = 512;
wchar_t strbuf[bufsize];
//
while (!file_.eof())
{
file_.getline(strbuf, bufsize);
}
String转CString
c_str()函数返回一个指向正规c字符串的指针,内容和string类的本身对象是一样的,通过string类的c_str()函数能够把string对象转换成c中的字符串的样式
CString strMfc;
std::string strStl=“test“;
strMfc=strStl.c_str();
CString转String
CString到std::string:
CString cs("Hello");
std::string s((LPCTSTR)cs);
由于std::string只能从LPSTR/ 构造LPCSTR,使用VC ++ 7.x或更高版本的程序员可以使用转换类,例如作为CT2CA中介。
CString cs ("Hello");
// Convert a TCHAR string to a LPCSTR
CT2CA pszConvertedAnsiString (cs);
// construct a std::string using the LPCSTR input
std::string strStd (pszConvertedAnsiString);
代码
按行读取文本文件
- 使用方式
vector<wstring> vecResult; // 文件读取的结果
wstring filename; // 文件路径
Cpp_read_file(vecResult, filename);
- MFC
#include <fstream>
#include <iostream>
int CMFCClistDlg::Cpp_read_file(vector<string> &result, string szFile)
{
//清空原始值
result.clear();
ifstream file_(szFile.c_str(), std::ios::in);
const int bufsize = 512;
char strbuf[bufsize];
// 读到文件末尾
while (!file_.eof())
{
file_.getline(strbuf, bufsize);
string sentence = strbuf;
if (sentence.empty())
{
continue;
}
result.push_back(sentence);
}
file_.close();
//如果结果值为空,返回-1
if(result.size()<1)
{
return -1;
}
return 0;
}
参考
C++读写TXT文件中的string或者int型数据以及string流的用法
https://www.cnblogs.com/1242118789lr/p/6885691.html
如何将CString和:: std :: string :: std :: wstring互相转换?
Cpp读文件、CString转String、String转CString的更多相关文章
- python 写文件write(string), writelines(list) ,读文件
read()方法用于直接读取字节到字符串中,可以接参数给定最多读取的字节数,如果没有给定,则文件读取到末尾. readline()方法读取打开文件的一行(读取下个行结束符之前的所有字节),然后整行,包 ...
- 头文件string.h,cstring与string
string.h string.h是一个C标准头文件,所有的C标准头文件都形如name.h的形式,通过#include <string.h>可以导入此头文件.之后我们就可以在程序中使用st ...
- <string> 与<string.h>、<cstring>的区别
<string.h> <string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <cstring> 在C++标准化(1998年 ...
- string string.h=cstring=str
<string.h> <string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <cstring> 在C++标准化(1998年 ...
- C/C++ - <string> 与<string.h>、<cstring>的区别
<string.h><string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <string><string>是C ...
- 【转】<string> <string.h> <cstring>的区别
#include < string.h > void main() { string aaa = " abcsd d " ; printf( " lookin ...
- CString转char * ,string
CString头文件#include <afx.h> string头文件#include <string.h> 1.CString转char * CString cstr; c ...
- string string.h cstring 区别
c++中 string与string.h 的作用和区别 #include <string.h> void main() { string aaa= "a ...
- C++中cstring.h和string.h的区别
转载:https://blog.csdn.net/qian_chun_qiang/article/details/80648691 1.string与cstring有什么区别 <string&g ...
随机推荐
- MT【24】一道五次方程的求根题
解答: 评:一般的五次及以上的多项式方程是无根式解的,只能用计算机去精确到某某位.但是特殊的比如$x^5=1$显然有根式解,本题就是一个不平凡的特殊的例子,这里的代换用于求解三次方程的求根过程是一样的 ...
- 更新本地git仓库的远程地址(remote地址)
如果远程仓库的地址更新了,我们本地仓库就需要更新remote地址, 可以通过git remote -v或者cat .git/config查看通信方式及远程地址 更新远程地址的方式有两种: 第一种方式: ...
- bzoj1003/luogu1772 物流运输 (dijkstra+dp)
先求出某一段时间[i,j]一直用同一个路径的最短路,乘上天数,记作cost[i,j] 那就可以设f[i]是前i天的最小代价,f[i]=f[j]+cost[j+1,i]+K #include<bi ...
- MyEclipse 检出新项目后,如果项目名称签名有个红色感叹号
MyEclipse 检出新项目后,如果项目名称签名有个红色感叹号,那么看 Problems中的错误提示(如果找不到Problems窗口,点 菜单栏的 Window——Reset Perspective ...
- 批处理 ------ @、ECHO OFF、ECHO ON 的使用
1.在批处理文件中,如果命令前加@,表示这条命令不打印出来,只把结果打印出来,即@是关闭命令本身的回显 2.::在批处理中表示注释某一行 3.ECHO ON表示接下来的命令中(不包括本命令),执行命令 ...
- STM8S ------ VCAP download
There is a specific pin called vcap in stm8s mcu. I recommend this pin connects to a 1uF capacitor w ...
- Hadoop生态圈-Cloudera Manager的基本使用
Hadoop生态圈-Cloudera Manager的基本使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1>.ClouderaManager功能 1.1>.管理监 ...
- SuperArray
package com.lovo.array; public class SuperIntArray { //属性 public int[] array; private int index;//代表 ...
- JDBC中的那些设计模式
一.单例模式获取数据库连接 1.关于单例模式的定义 保证一个类仅有一个实例,并提供访问它的全局访问点.Java里面实现的单例是一个虚拟机的范围.因为装载类的功能时虚拟机,所以一个虚拟机在听过自己的Cl ...
- Hive记录-配置支持事务管理hive-site.xml
<property> <name>hive.support.concurrency</name> <value>true</value> & ...