1 创建 mystruct.h 写入
struct mypic_struct
{
int SrcId;
char SrcPicName[15];
char SrcPicPath[256];
char SrcPicFormat[10];
};
extern struct mypic_struct PIC[100]; //申明为全局
2 创建 mystruct.cpp
#include "mystruct.h"
struct mypic_struct PIC[100]; //定义结构体数组
3 需要使用结构体文件 a.cpp#include "mystruct.h"
extern struct mypic_struct PIC[100];//申明使
4 结构体的写入,是用循环写入的,注意getFileName(filepath)返回值时QString类型,返回一个图片文件路径的文件名,不包含后缀, 以下时将QString转换为char类型。
QByteArray file_name;
file_name=getFileName(filepath).toLatin1();
strcpy(PIC[line_count].SrcPicName,file_name.data()); //数组的写法
qDebug("line=%d,filename:%s",line_count,PIC[line_count].SrcPicName);
//说明:PIC[line_count].SrcPicName=file_name.data(); 此时为指针的写法,例如 结构体定义的是char *SrcPicName,可以写入,并且下面的qDebug可以将信息打印出来,但是当你用for循环读取的时候会出现乱码或者打印不出来。所以定义是要用数组,而不用指针。