cpp读写文件cstring转stringstring转cstring(代码片段)

17bdw 17bdw     2023-02-24     631

关键词:

场景

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

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互相转换?

https://stackoverflow.com/questions/258050/how-to-convert-cstring-and-stdstring-stdwstring-to-each-other

c_cpp用c读写文件(代码片段)

查看详情

c_cpp提升读写ini文件(代码片段)

查看详情

cpp二进制文件读写

//readingbinaryfile#include<iostream>#include<fstream.h>constchar*filename="test.txt";intmain(){char*buffer;longsize;ifstreamin(filename,ios::in|ios::binary|ios::ate);size=in.tellg();in.se 查看详情

c++开发中利用yaml-cpp读写yaml配置文件(代码片段)

在程序员的开发生涯中,读写配置文件必不可少。配置文件有利于我们灵活配置工程,解决大量重复劳动,也方便调试。配置文件的格式有很多,最简单的有一行一行的文本,也有像json、xml、protocolbuffer这样... 查看详情

转:cstring::getlength()获得字节数的正确方法

...,上传得到的文件总是小于正常文件。最终发现问题出在CString::GetLength()方法上。当采用Unicode编码时,而且httpheader字符串中出现了中文或其其他多字节字符,该方法仍旧只返回字符数,而不是实际的字节数,这样在 查看详情

excel.cpp

1、C++读写EXCEL文件方式比较-CSDN博客.html(http://blog.csdn.net/fullsail/article/details/4067416)  C++使用OLE高速读写EXCEL的源码-CSDN博客.html(http://blog.csdn.net/fullsail/article/details/8449448)2、3、4、5、  查看详情

(c++)cstring转string怎么转!!

--------------------------------------------------------------------CStringwo("xxxxcc");strings;s=wo.GetBuffer();//失败了!!!//错误信息1>f:\c++试验\count_machine\count_machine\count_machinedlg.cpp(185):errorC2679:二进制“=”:没有找到接受“wchar_... 查看详情

读写txt文件

读取txt文件中的内容放如cstring变量中: CStringfilePath; CStdioFilefile;//打开文件 if(!file.Open(filePath,CFile::modeRead))  ::AfxMessageBox(_T("文件打开失败!"));  return;//读取文件内容CStringst 查看详情

c_cpp读写(代码片段)

查看详情

文件读写原理(转)

系统调用操作系统的主要功能是为管理硬件资源和为应用程序开发人员提供良好的环境,但是计算机系统的各种硬件资源是有限的,因此为了保证每一个进程都能安全的执行。处理器设有两种模式:“用户模式”与“内核模式”... 查看详情

pe读写

//仿PE文件.cpp:Definestheentrypointfortheconsoleapplication.//#include"stdafx.h"#include<windows.h>#include<malloc.h> /*将文件从硬盘读取到缓冲区中参数1:文件路径参数2:接受读取数据的缓冲区的地址的指针【指针的指针】读取成功返回... 查看详情

(c++)cstring转string怎么转!!用getbuffer()不行的!!!

--------------------------------------------------------------------CStringwo("xxxxcc");strings;s=wo.GetBuffer();//失败了!!!//错误信息1>f:\c++试验\count_machine\count_machine\count_machinedlg.cpp(185):errorC2679:二进制“=”:没有找到接受“wchar_... 查看详情

(转)cstring截取字符串全攻略

  源地址http://blog.163.com/seraph_leo/blog/static/1689297102010786206361/?fromdm&fromSearch&isFromSearchEngine=yes例程1:(csdn) 文件xxxx.dll去掉后面的.dll 方法1、 charstr[]="xxxx.dll 查看详情

转:mfc之colevariant

COleVariant 本质上是一个枚举,用同一种类型来表达不同的子类型。如同boost中的variant。例子[cpp] viewplain copyCOleVariant var(3.6f);  float v = var.fltVal;  CString str("test 查看详情

CString : (TCHAR*)(this + 1) 是啥意思?

】CString:(TCHAR*)(this+1)是啥意思?【英文标题】:CString:Whatdoes(TCHAR*)(this+1)mean?CString:(TCHAR*)(this+1)是什么意思?【发布时间】:2009-12-0210:14:19【问题描述】:在CString头文件中(无论是Microsoft的还是OpenFoundationClasses-http://www.koders.com/cp... 查看详情

c_cpp在c中读写二进制数据块(代码片段)

查看详情

cstring与char*string转cstring(unicode编码)

unicode编码下,cstring与char*、String转Cstring的转换方法: 1 .CSstring转char* wchar_t*psw_wchar=psw.GetBuffer();//获取psw的宽字符用数组保存 psw.ReleaseBuffer();  intnLen=psw.GetLength();//获取psw的字 查看详情

如何在 C++ 的构造函数中初始化一个 cstring?

】如何在C++的构造函数中初始化一个cstring?【英文标题】:Howtoinitialacstringinconstructorinc++?【发布时间】:2021-05-0723:44:34【问题描述】:我的作业需要使用cstring来保存全名,所以在头文件中我有private:charfullName[50];在我的cpp文件中... 查看详情