C++ 文件和流 技术教程文章

c++读取文件夹下所有文件名【代码】

int readFileList(const std::string &folderPath,std::vector<std::string> &vFileList) {DIR *dp;struct dirent *dirp;if ((dp = opendir(folderPath.c_str())) == NULL){return 0;}int num = 0;while ((dirp = readdir(dp)) != NULL){std::string name = std::string(dirp->d_name);if (name != "." && name != ".."){vFileList.push_back(name);num++;}}closedir(dp);// std::sort(vFileList.begin(), vFileList.end());return...

c++文件读写【代码】

// 输入输出流1.cpp: 定义控制台应用程序的入口点。 //#include "stdafx.h" #include<iomanip> #include<iostream> #include<fstream> using namespace std; //文本文件读写 void test1() {const char * path = "C:\\Users\\lfzh\\Desktop\\source.txt";const char* target = "C:\\Users\\lfzh\\Desktop\\target.txt";//法一ifstream ism(path, ios::in);//只读方式打开文件,调用构造函数打开文件//ofstream osm(target, ios::out)...

C/C++ 读文件

读文件 std::ifstream infile; infile.open("/Users/yangwenhuan/IVP_workbench/test/graph.prototxt"); std::stringstream buf; buf << infile.rdbuf(); string prototxt_str = buf.str(); infile.close();FILE *pfile = fopen("/Users/yangwenhuan/IVP_workbench/test/graph.prototxt", "r"); char *data; if (pfile == NULL) {cout << "Read prototxt fail!" << endl;return -1; } fseek(pfile, 0, SEEK_END); int length = f...

C++学习笔记——文件操作【代码】

C++在程序运行的时候产生的数据属于临时数据,在程序运行结束后释放掉,有时候我们需要把一些简单的数据永久的存储起来,这个时候就可以利用文件将数据持久化。 文件可以分为两种类型: 1.文本文件:文件以文本的ASCII码形式存储在计算机中; 2.二进制文件:文件以文本的二进制形式存储在计算机中,用户一般情况下不能读懂。 文件操作的三个类: 1.ofstream:写操作; 2.ifstream:读操作; 3.fstream:读写操作。 文件的打开方式:...

学习笔记(04):C++入门基础视频精讲-编写自己的头文件

立即学习:https://edu.csdn.net/course/play/9598/204710?utm_source=blogtoeduC++头文件的作用: 1,隔离实现与声明,条理清晰,很好的表达功能含义,保护实现代码; 2,代码重复利用;点赞 收藏分享文章举报dollyandkalin发布了5 篇原创文章 · 获赞 0 · 访问量 135私信 关注

C++ 计算文件指定位置 rof【代码】

功能代码: int get_PE_feature_rof(string path_r,int codeArr[],int codeCtrl[],int len){// 打开文件ifstream in = ifstream(path_r, ios::binary);if (!in.is_open()){cout << "文件打开失败:" << GetLastError() << endl;in.close();return 0;}// 获取文件大小、文件名long long Beg = in.tellg();in.seekg(0, ios::end);long long End = in.tellg();long long fileSize = End - Beg;in.seekg(0, ios::beg);// 读文件(每次循...

关于c++的头文件和变量声明【图】

写再最前面:摘录于柳神的笔记:   C++的头?件?般是没有像C语?的 .h 这样的扩展后缀的,?般情况下C语???的头?件去掉 .h 然 后在前?加个 c 就可以继续在C++?件中使?C语?头?件中的函数啦~?如:   C++的变量声明:C语?的变量声明?般都在函数的开头,但是C++在?次使?变量之前声明即可~(当然也可以都放在 函数的开头),?且?般C语???会在 for 循环的外?定义 i 变量,但是C++??可以在 for 循环内 部定义~(关于这点, VC++6.0...

【C++】cmath文件名详解

【C++】cmath文件名详解 cmath是c++语言中的标准库头文件。其中的 “c” 表示其中的函数是来自 C标准库,“math”表示为数学常用库函数。该头文件主要声明了常用的数学库函数,比如三角函数相关,常用数学运算相关的一些基本函数。其使包含math.h的头文件。 绝对值函数int abs(int i) 返回整型参数i的绝对值 double fabs(double x) 返回双精度参数x的绝对值 long labs(long n) 返回长整型参数n的绝对值 double cabs(struct complex...

c++文件复制【代码】

#include <iostream> #include<fstream> using namespace std; int main() {ifstream in;in.open("bools.txt");if (!in){cerr << "打开文件失败" << endl;return EOF;}char x;while (in >> noskipws>>x){cout << x;}cout << endl;in.close();system("pause");return 0; }**1.noskipws在输入文件时,不忽略换行符,如 char woed; ifstream ifile(“c\out.txt”); ifile>>noskipws>>word; no skip whitespace(空白)noskipws #include...

vscode同时使用(msvc)cl.exe与(MingW)g++.exe编译c++文件【代码】【图】

在网上查了一圈都没找到我想要的,自己折腾一番成功了,特此记录下来供大家参考。 vscode下载和安装都很简单,聪明的你一看就会。本文重点介绍如何使用vscode在同一个c++项目内自由切换使用微软msvc的cl.exe和MingW的g++.exe编译器来编译调试,以方便学习不同编译器的差别。我只用了这两款编译器,其他的编译器可能设置方法大同小异。 最终效果: 这是一个cpp源文件hello.cpp和两款编译器都编译过后的其他所有文件,hello.cpp内容不...

C++ 文件操作I/O流【代码】

程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放 通过文件可以将数据持久化 C++中对文件操作需要包含头文件 < fstream > 文件类型分为两种: 文本文件 - 文件以文本的ASCII码形式存储在计算机中 二进制文件 - 文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂它们 操作文件的三大类 : 1、ofstream:写操作 2、ifstream: 读操作 3、fstream : 读写操作 文本文件 写文件步骤如下: 1、包含头文件 #in...

C++读取文件

#include <iostream>#include <stdio.h>#define INF 100000000000using namespace std; // 文件输入输出小应用int main(){   FILE* fin, * fout;// 定义两个文件类型指针   fin = fopen("data.in", "r"); //读取文件   fout = fopen("data.out", "w"); // 写入文件   int x, n = 0, min = INF, max = -INF, s = 0;   while (fscanf(fin, "%d", &x) == 1) {     s += x;     if (x < min)       min = ...

c++实现文件复制并修改相应属性

问题描述 完成一个目录复制命令mycp,包括目录下的文件和子目录, 运行结果如下: beta@bugs.com [~/]# ls –la semtotal 56drwxr-xr-x 3 beta beta 4096 Dec 19 02:53 ./drwxr-xr-x 8 beta beta 4096 Nov 27 08:49 ../-rw-r--r-- 1 beta beta 128 Nov 27 09:31 Makefile-rwxr-xr-x 1 beta beta 5705 Nov 27 08:50 consumer*-rw-r--r-- 1 beta beta 349 Nov 27 09:30 consumer.cdrwxr-xr-x 2 beta beta 4096 Dec 19 02:53 s...

C++配置文件相关【代码】

1.#ifndef COMMON_H #define COMMON_Htypedef struct _StructConfig {std::string file_name; }StructConfig;extern StructConfig sConfig;#endif Config configSettings("../config.ini");sConfig.file_name = configSettings.Read("file_name", sConfig.file_name);#config.ini文件 file_name=../test.mp4

c++ 读取 utf-8 文件到 string【代码】

#include <iostream> #include <assert.h> #include <fstream> #include <string> #include <string.h> using namespace std;#ifdef _WIN32 #include <Windows.h> #endiftypedef enum FileType {FileType_ANSI = 0,FileType_UNICODE,FileType_UTF8 }FILETYPE;#ifdef _WIN32 string UTF8ToGB(const char* str); #endifFILETYPE GetTextFileType(const std::string & strFileName); string ReadTextFile(const std::string & strFile...

C++ 配置文件解析类 ParseConfig【代码】

依赖项: 依赖于 ProcessString 类,可从该篇博客获取「字符串处理类 ProcessString (包含常用字符串处理函数)」 ParseConfig.h //Linux & C++11#pragma once//包含系统头文件 #include <string> #include <exception> #include <memory> #include <map>//宏定义:函数参数流向 #define IN #define OUT #define INOUT//使用命名 using std::string; using std::exception; using std::shared_ptr; using std::make_shared; using s...

c++关于multiset的头文件包含问题【代码】【图】

最近在Bilibili上看到不少侯捷老师C++的视频教程,侯捷老师翻译了很多C++的经典书籍,比如《Essential C++中文版》、《STL源码剖析》,也写了《深入浅出MFC 第二版》。使用到multiset这个关联容器时,本来以为Visual Studio2017中会提供#include <multiset>这个头文件,没想到加入到显示错误,后来查了下资料,说是multiset只是set的一个特例而已,只需要包含set的头文件即可,使用#include<set>即可。## 测试代码如下:// test_mu...

C++的源文件和头文件【代码】

首先以一个类的定义作为例子 在名称为student.h的头文件中#include <iostream> using namespace std; #include <string.h> class Student { public: //外部接口void input(char* pid,char* pname,int a,float s);void modify(float s) {score=s;}//成员函数体放在类中自动成为内联函数void display() ; private: //私有成员 char* id; char* name; int age; float score; }; 在student.cpp中实student类的成员函数#include "s...

[理解] C++ 中的 源文件 和 头文件【代码】【图】

我是学 C井 的, 现在在工作中主要使用的编程语言是 Java, 还记得当初在第一次接触到 Cpp 的时候, 听到的第一个概念就是 Cpp 的头文件和源文件, 当初理解了好久, 死活都弄不明白, 现在好歹是理解一点了, 做个笔记 在说头文件和源文件之前, 先来说一下 Cpp 大概的源文件编译过程吧, 新建一个文件 Source.cpp, 然后写#include <iostream>using namespace std; int main(){ cout << "hello world" << endl;return 0; }1): 编译...

解决关于VC++ 6.0打开文件时,程序停止的问题

不少boys和girls安装VC++ 6.0英文版后,开始学习C++语言,但是使用软件的过程中,点击“打开”时,就会出现程序进程错误,崩溃的事儿,很是郁闷。最后直接一个对话框如下:并且vc6.0直接死掉,无论重装零碎还是重装vc6都无济于事。这是vc6的一个bug ,微软给出了解决的办法 以下就是解决办法: 1.先到以下网址:vc60ent/s1/ 6.0/w9xnt4/en-us/filetool.exe下载一个FileTool.exe的文件, (1)双击文件,点击"Unzip" , 复制"unzop T...