C++笔记

文章目录

      • 2. C++常用总结
        • 2.5 #define和__FILE__,__LINE__,__FILENAME__
        • 2.5 判断文件的大小
        • 2.4 判断文件是否存在,是否可写可读
        • 2.3 不同cpp之间函数的调用
        • 2.2 static变量的使用
        • 2.1 全局变量的使用
      • 1. opencv常用总结
        • 1.2 cv::Mat文件写入csv
        • 1.1 导入opencv库遇到导入失败解决

2. C++常用总结

2.5 #define和__FILE__,LINE,FILENAME

//1.h
#pragma once
//#include 
#define ERROR "error"
#define FILENAME (strrchr(FILE, '\') ? strrchr(FILE, '\') + 1 : FILE)//注意不能用双引号,双引号表示字符串,单引号表示字符
#define LOG(n) std::cout<//FILE直接返回“当前路径+文件名”
//LINE直接获取行数
//FILENAME需要定义下,获取当前文件名
//char *strrchr(const char *str, int c) 在参数 str 所指向的字符串中搜索最后一次出现字符 c(一个无符号字符)的位置。

//2.cpp
#include "4.h"
#include 
using namespace std;void main()
{
	cout << FILE << endl;
	//cout << strrchr(FILE, "\") << endl;
	LOG(ERROR);
}

2.5 判断文件的大小

(1)FILE和fopen
FILE*fp 定义一个名为fp的指针,属于FILE(文件)类型。FILE是一类特殊的指针,用来操作文件。fp=fopen(“文件名”,“r”);调用fopen()函数,尝试打开同目录下的文件名相同的文件,参数“r"代表以只读方式打开。如果打开成功,则将文件起始位置 的地址返回给fp指针,否则,返回NULL到fp指针。

2.4 判断文件是否存在,是否可写可读

头文件
函数原型:int _access(const char *pathname, int mode);
返回值:如果文件具有指定的访问权限,则函数返回0;如果文件不存在或者不能访问指定的权限,则返回为-1
注意: 当pathname为文件时,_access函数判断文件是否存在,并判断文件是否可以用mode值指定的模式进行访问。当pathname为目录时,_access只判断指定目录是否存在,在Windows NT和Windows 2000中,所有的目录都只有读写权限。(个人理解:一般文件和目录都可以有mode权限)
mode的值和含义
00——只检查是否存在
02——写权限
04——读权限
06——读写权限

#include 
#include 
#include 
#include 
using namespace std;
using namespace cv;int check_file(string filepath)
{
	//int a = _access(filepath.c_str(), 0);
	int a = _access(filepath.c_str(), 2);
	cout << a << endl;
	bool ret = (0 == _access(filepath.c_str(), 0));
	//如果具有相应的权限,返回0;不具有,返回-1
	return ret;
}void main()
{
	string img_path1 = "../test/1.txt";
	string img_path2 = "../test";
	bool ret = check_file(img_path1);
	cout << ret << endl;
	bool ret1 = check_file(img_path2);
	cout << ret1 << endl;		
	// ../上级目录 ./当前目录 /根目录	
	//string img_path = "./test/1.bmp";    string img_path = "../test/2.bmp";
	cv::Mat img = cv::imread(img_path);
	cv::imshow("1", img);
	cv::waitKey(500);}

2.3 不同cpp之间函数的调用

//1.cpp
#include 
using namespace std;
void detect()
{
	cout<<"12"<<endl;
}

//2.cpp
#include 
using namespace std;
void detect();
void main
{
	detect();
}
//输出结果为:"12"
//注意不要1.cpp不要头文件都行

2.2 static变量的使用

#include 
using namespace std;
void main()
{
	
	for (int i = 0; i < 10; i++)
	{
		static int a;	
		cout << a * a << endl;
		a++;
	}
	/*1*/
	//cout<< "最后输出的a值:" << a << endl;
	//上面这句会报错,由于static定义在for循环中,相当于是一个局部变量
}
/*2*/输出结果为01491625364964
//static定义的变量初值默认为0,下一次会保持下一次的值

2.1 全局变量的使用

//2.h
#pragma once
extern int a;

//3.h
#pragma once
void test();

//2.cpp
#include "2.h"
#include "3.h"
#include 
using namespace std;
int a= 10;//定义全局变量,定义在main函数外面
//在其他cpp中使用时应该加上.h头文件,并且不用定义
void main()
{
	cout<<a<<endl;
	test();
}

//3.cpp
#include "2.h"
#include 
using namespace std;
void test()
{
	cout<<"test函数中"a*a"的输出值"<<endl;
	cout<<a*a<<endl;
}

1. opencv常用总结

1.2 cv::Mat文件写入csv

#include 
#include 
#include 
#include 
using namespace std;

void writCSV(string filename, cv::Mat m)
{
	ofstream myfile;
	myfile.open(filename.c_str());
	myfile<<cv::format(m, cv::Formatter::FMT_CSV);
	myfile.close();
}
void main()
{
	cv::Mat pic = cv::imread("1.jpg");
	cv::imshow("1", pic);
	cv::waitKey(1000);
	string csv_name = "1.csv";
	writeCSV(csv_name,pic);
	cout<<"csv保存成功"<<endl;
}

1.1 导入opencv库遇到导入失败解决

#include
#include
using namespace std;
using namespace cv;

void main()
{
	cout<<1<<endl;
	return 0;
}

(1)将debug后面的“x86”改为“x64”
(2)在解决方案-属性“C/C+±常规”中,附加包含目录中添加

E:\opencv\opencv-3.4.1-vc14_vc15\opencv\build\include\opencv
E:\opencv\opencv-3.4.1-vc14_vc15\opencv\build\include\opencv2
E:\opencv\opencv-3.4.1-vc14_vc15\opencv\build\include

在解决方案-属性“链接器-常规”中,附加库目录中添加

E:\opencv\opencv-3.4.1-vc14_vc15\opencv\build\x64\vc14\lib

在解决方案-属性“链接器-输入”中,附加依赖项

opencv_world341d.lib

你可能感兴趣的:(笔记)