1.1、C++中substr函数用法_获取文件名的一部分
//1.1、C++中substr函数用法_获取文件名的一部分
#include<string>
#include<iostream>
using namespace std;
string s = "E:\Copy_data_20220217\aaaaaa.MP4";
int main()
{
//string a = s.substr(0, 5); //获得字符串s中从第0位开始的长度为5的字符串
std::size_t last_pos = s.find_last_of(".");
std::string a = s.substr(0, last_pos);
std::string vid_format = s.substr(last_pos + 1);
cout << a << endl;
cout << vid_format << endl;
system("Pause"); //按任意键后退出
return 0;
}
1.2、获取文件名的前缀和后缀
//1.2、获取文件名的前缀和后缀
#include <string>
#include <iostream>
using namespace std;
int main()
{
string name1 = "/a/b/c/v/zhiodfh.jpg";
int pos1 = name1.find_last_of('/');
string name2 = name1.substr(pos1 + 1);
cout << "pos1:" << pos1 << endl;
cout << "name2:" << name2 << endl;
int pos2 = name2.find('.');
string name3 = name2.substr(0, pos2);
cout << "name3:" << name3 << endl;
system("Pause"); //按任意键后退出
return 0;
}
2、20220401_C++ txt文件的读操作
//2、20220401_C++ txt文件的读操作
/*
C++提供文件流类来处理文件的输入输出,分别是ofstream类、ifstream类和fstream类
ofstream流对象能够对文件进行输出操作(即写操作);
ifstream流对象能够进行文件的输入操作(即读操作);
fstream流对象既能够进行文件输出操作,也能够进行文件输入操作
*/
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
using namespace std;
void writeTxt(string file);
void readTxt(string file);
string filename = "C:\\Users\\Administrator\\Desktop\\ceshi\\videos.txt";
int main()
{
writeTxt(filename);
readTxt(filename);
system("Pause"); //按任意键后退出
}
void writeTxt(string file) //写入hello world
{
ofstream outfile;
outfile.open(file.data()); //将文件流对象与文件连接起来
assert(outfile.is_open()); //若失败,则输出错误消息,并终止程序运行
outfile << "hello," << endl << "world." << endl;
outfile.close();
}
void readTxt(string file) //读出hello world
{
ifstream infile;
infile.open(file.data()); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
string s; //逐行输出txt文本中的内容
while (getline(infile, s))
{
cout << s << endl;
}
infile.close(); //关闭文件输入流
}
3、20220402读文件夹下文件名(各类文件的文件名),写入txt_可用
//3、20220402读文件夹下文件名(各类文件的文件名),写入txt_可用
#include<iostream>
#include<string>
#include<fstream>
#include <io.h>
#include <vector>
using namespace std;
//void getFiles(string path, vector<string>& files);
void getFiles(string, vector<string>&); //得到文件夹下所有的文件名
string filefolder = "E:\\Copy_data_20220217";
string filename = "C:\\Users\\Administrator\\Desktop\\vid.txt";
int main()
{
vector<string> files;
getFiles(filefolder, files);
ofstream outdata;
for (int j = 0; j < files.size(); ++j)
{
cout << files[j] << endl;
outdata.open(filename, ios::app); //打开文件txt文件,ios::app是尾部追加的意思
//outdata << files[j] << endl; //把文件夹下的文件名写入到txt中
outdata << "1" << "_" << files[j] << endl;
outdata.close(); //关闭文件
}
system("Pause"); //按任意键后退出
return 0;
}
void getFiles(string path, vector<string>& files)
{
intptr_t hFile = 0; //文件句柄
struct _finddata_t fileinfo; //文件信息
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
//如果是目录,迭代之
//如果不是,加入列表
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
4.1、vector_一维数据输入,找出一对石头
//4.1、vector_一维数据输入,找出一对石头
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int N,we; //N是石头个数,we是石头权值
int temp; //temp暂存某一权值的变量
int M=0; //权值不同的石头序号组数
cout << "请输入石头个数:";
cin >> N;
vector<int> array(N);//声明变长数组
//输入所有石头的权值
for (int i = 0; i < N; i++)
{
cout << "请输入"<<i<<"号石头权值:" << endl;
cin >> we;
array[i] = we;
}
//显示石头序号及对应权值
cout << "石头数为"<<N<<","<<"石头权值依次为" << endl;
for (int i = 0; i < N; i++)
{
cout <<"第"<< i << "号石头权值:" <<array[i] << endl;
}
//找出一组权值不同的石头序号(所有)
for (int i = 0; i < N; i++)
{
temp = array[i];
for (int j = i+1; j < N; j++)
{
if (temp == array[j])
continue;
else
{
M = M + 1;
cout << "一组权值不同的石头序号为" << i << "、" << j << endl;
}
break;
}
}
cout << "两个权值不同的石头序号共有" << M << "组" << endl;
return 0;
}
4.2、vector_二维数据的输入和显示
//4.2、vector_二维数据的输入和显示
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int num1,//行数
num2;//列数
cout << "Please enter the number for row and column: " << endl;
cin >> num1 >> num2;
//为二维数组开辟空间
int **p = new int*[num1];
for (int i = 0; i < num1; ++i)
p[i] = new int[num2];
for (int j = 0; j < num1; j++)
{
for (int k = 0; k < num2; k++)
{
p[j][k] = (j + 1)*(k + 1);
cout << setw(6) << p[j][k] << ':' << setw(8) << &p[j][k];
}
cout << endl;
}
//释放二维数组占用的空间
for (int m = 0; m < num1; m++)
delete[] p[m];
delete[] p;
return 0;
}
5.1、C++继承实例
//5.1、C++继承实例
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class parent
{
protected:
string pname;
public:
parent(string name)
{
pname = name;
}
virtual void printname() {};
};
class child :public parent
{
protected:
string cname;
public:
child(string name):parent(name)
{
cname = name;
}
virtual void printname()
{
cout << "this is child,cname is" << cname << ",pname is" << pname << endl;
}
};
class grandchild :public child
{
private:
string gname;
public:
grandchild(string name) :child(name)
{
gname = name;
}
virtual void printname()
{
cout << "this is grandchild,gname is" << gname << ", cname is" << cname << ",pname is" << pname << endl;
}
};
int main()
{
string name = "C";
child Child(name);
name = "GC";
grandchild Gchild(name);
vector<parent*> mlist;
mlist.push_back(dynamic_cast<parent*>(&Child));
mlist.push_back(dynamic_cast<parent*>(&Gchild));
for (int i = 0; i < mlist.size(); i++)
mlist[i]->printname();
return 0;
}
5.2、C++多态示例代码
//5.2、C++多态示例代码
#include<iostream>
using namespace std;
class A
{
public:
virtual void disp(int n)
{
cout << "A::disp n=" << n << endl;
}
};
class B :public A
{
public:
virtual void disp(double m)
{
cout << "B::disp m=" << m << endl;
}
};
void fn(A &a)
{
a.disp(5.5);
}
int main()
{
B b;
fn(b);
return 0;
}
5.3、C++封装
//5.3、C++封装
#include<iostream>
using namespace std;
const double PI = 3.14;
class Circle
{
public:
int m_r;
double calculateZC()
{
return 2 * PI*m_r;
}
};
int main()
{
Circle c1;
c1.m_r = 10;
cout << "圆的周长为:" << c1.calculateZC() << endl;
system("pause");
return 0;
}
6.1修改txt中某一行的数据,并保存在某一行(整行替换)
//6.1修改txt中某一行的数据,并保存在某一行(整行替换)
/*
1_E:\Copy_data_20220217\100.jpg
1_E:\Copy_data_20220217\20220303_231753.avi
1_E:\Copy_data_20220217\aaaaaaa736.MP4
1_E:\Copy_data_20220217\bbbbbbbbbbbbbbbb.MP4
1_E:\Copy_data_20220217\ccc.MP4
1_E:\Copy_data_20220217\ddddddd.MP4
*/
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
using namespace std;
void ModifyLineData(char*, int, char*);
void main()
{
ModifyLineData("C:\\Users\\Administrator\\Desktop\\vid.txt", 3, "0");
system("Pause"); //按任意键后退出
}
void ModifyLineData(char* fileName, int lineNum, char* lineData)
{
ifstream in;
in.open(fileName);
string strFileData = "";
int line = 1;
char tmpLineData[1024] = { 0 };
while (in.getline(tmpLineData, sizeof(tmpLineData)))
{
if (line == lineNum)
{
//cout << strFileData << endl;
strFileData += string(lineData);
strFileData += "\n";
}
else
{
strFileData += string(tmpLineData);
strFileData += "\n";
}
line++;
}
in.close();
//写入文件
ofstream out;
out.open(fileName);
out.flush();
out << strFileData;
out.close();
}
6.2修改txt中某一行中指定位置的数据,并保存在某一行
//6.2修改txt中某一行中指定位置的数据,并保存在某一行
/*
1_E:\Copy_data_20220217\100.jpg
1_E:\Copy_data_20220217\20220303_231753.avi
1_E:\Copy_data_20220217\aaaaaaaaaaaaaaaaa.MP4
1_E:\Copy_data_20220217\bbb.MP4
1_E:\Copy_data_20220217\ccccccccccccc.MP4
1_E:\Copy_data_20220217\ddddddddddd.MP4
*/
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
using namespace std;
void ModifyLineData(char* , int , char* );
void main()
{
ModifyLineData("C:\\Users\\Administrator\\Desktop\\vid.txt", 4, "0");
system("Pause"); //按任意键后退出
}
void ModifyLineData(char* fileName, int lineNum, char* lineData)
{
ifstream in;
in.open(fileName);
string strFileData = "";
int line = 1;
char tmpLineData[1024] = { 0 };
while (in.getline(tmpLineData, sizeof(tmpLineData)))
{
int pos1 = string(tmpLineData).find_first_of('_');
string first = string(tmpLineData).substr(0, pos1);
string leftover = string(tmpLineData).substr(pos1 + 1);
bool format_ind = (first == "1");
if (format_ind)
{
string temp1 = string(lineData) + string("_") + string(leftover);
strFileData += temp1;
strFileData += "\n";
}
else
{
continue;
}
line++;
}
in.close();
//写入文件
ofstream out;
out.open(fileName);
out.flush();
out << strFileData;
out.close();
}
7、复制txt内容到另外一个txt
//7、复制txt内容到另外一个txt
#include <fstream>
#include <string>
using namespace std;
void copyTXT(string, string);
string filename = "C:\\Users\\Administrator\\Desktop\\vid.txt";
string filename1 = "C:\\Users\\Administrator\\Desktop\\videos.txt";
int main()
{
copyTXT(filename, filename1);
return 0;
}
void copyTXT(string filename, string filename1)
{
ifstream infile(filename);
ofstream outfile(filename1, ios::app);
char c;
while (infile.get(c))
{
outfile << c;
}
infile.close();
outfile.close();
}
8、创建文件夹
//8、创建文件夹
#include <iostream>
using namespace std;
int main()
{
string folderPath = "C:\\Users\\Administrator\\Desktop\\123";
string command = "mkdir " + folderPath;
system(command.c_str());
system("Pause"); //按任意键后退出
return 0;
}
9、创建C++文件夹以及C++文件名的提取
//9、创建C++文件夹以及C++文件名的提取
#include <string>
#include <iostream>
using namespace std;
int main()
{
char p[50];
//string video_name_all = "1_E:\Copy_data_20220217\aaaa.MP4";
/*
\是转义字符, 比如\r\n就可以表示换行
所以单独一个\会被和后面的字符综合起来被编译器认知
如果你想打\,就需要打两个
\\的情况,第一个会被认为转义字符,第二个编译器就知道你是想输入\了
*/
string video_name_all = "1_E:\\Copy_data_20220217\\aaaa.MP4";
std::size_t pos = video_name_all.find_last_of("\\");
std::size_t pos1 = video_name_all.find(".");
std::string video_name = video_name_all.substr(pos+1);
std::cout << video_name << std::endl;
/*
//在桌面上建立名为123的文件夹
string folderPath = "C:\\Users\\Administrator\\Desktop\\123";
string command = "mkdir " + folderPath;
system(command.c_str());
*/
system("Pause"); //按任意键后退出
return 0;
}
10、从完整路径中提取文件名、不带后缀的名字、后缀名
//10、从完整路径中提取文件名、不带后缀的名字、后缀名
#include <iostream>
#include <string>
using namespace std;
void main()
{
string path = "C:\\Users\\Administrator\\Desktop\\text\\data.22.txt";
//1.获取不带路径的文件名
string::size_type iPos = path.find_last_of('\\') + 1;
string filename = path.substr(iPos, path.length() - iPos);
cout << filename << endl;
//2.获取不带后缀的文件名
string name = filename.substr(0, filename.rfind("."));
cout << name << endl;
//3.获取后缀名
string suffix_str = filename.substr(filename.find_last_of('.') + 1);
cout << suffix_str << endl;
system("Pause"); //按任意键后退出
}
11、修改txt中的内容,写入到另一个txt文件中
//11、修改txt中的内容,写入到另一个txt文件中
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
ifstream in("C:\\Users\\Administrator\\Desktop\\20220428\\test.txt");
ofstream out("C:\\Users\\Administrator\\Desktop\\20220428\\rec_gt_test.txt");
string filename;
string line;
if (in) // 有该文件
{
while (getline(in, line)) // line中不包括每行的换行符
{
//int pos1 = string(line).find_last_of('/');
//string leftover = string(line).substr(pos1 + 1);
//cout << leftover << endl;
//out << leftover << endl; // 输入到2.txt中
string temp = "test/";
cout << line << endl;
out << temp << line << endl; // 输入到2.txt中
}
}
else // 没有该文件
{
cout << "no such file" << endl;
}
return 0;
}