关于C++用sprintf方法字符串转字符数组

fengzaiyaoye 2015-03-05 05:48:22
代码如下:
#include <cv.h>
#include <highgui.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "opencv2/opencv.hpp"
#include "windows.h"

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
String path="E:/编程/c#/opencv/opencvtest/Debug/image";
Directory dir;
vector<String> fileNames = dir.GetListFiles(path, "*.jpg", false);
vector<String> fileFullName(fileNames.size());
for(int i=0; i < fileNames.size(); i++)
{
String fileName = fileNames[i];
fileFullName[i] = path + fileName;
}

for(vector<string>::size_type i = 0; i < fileFullName.size(); ++i)
{
char* a = new char(fileFullName[i].length()+1);
sprintf(a ,"%s" , fileFullName[i].c_str());
cout<<a<<endl;
}
system("pause");
}

我这个程序是想遍历image目录下的所有图片,返回图片地址与文件名到fileFullName数组。 然后将字符串数组的每一项转化成一个字符数组(cvLoadImage需要图片地址为字符数组)。 但是 运行的时候报错

显示cvtt.exe 已触发了一个断点。
...全文
301 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-03-06
  • 打赏
  • 举报
回复
关于自己是否适合编程的很简单的测试: 在报纸或杂志上随便找一段约1000字的文章,在Word中输入一遍。输完后再参考下面答案: A里面有10处以上文字或标点错误 B里面没有文字或标点错误并敢为此跟人打赌 C里面没有文字或标点错误并且字体和排版完全与原稿一致 D打印在半透明的纸上和原稿重叠在一起检查一模一样,且自我感觉很有成就感 A不适合编程(理由:打字准确度偏低、粗心大意) B初级程序员(理由:打字准确度很高、认真细致、自信、理解全角半角概念) C高级程序员(理由:在B的基础上理解字体和排版也是电脑打印的重要因素、但相比D还不够偏执、精益求精、结果可验证) D软件项目经理(理由:能针对项目给出令人信服的细致到极点的需求说明和典型测试用例。用户几乎挑不出毛病。专业!) 如果想从A变成B的话,到我的资源http://download.csdn.net/detail/zhao4zhong1/4084259里面下载“适合程序员的键盘练习”
ztenv 版主 2015-03-06
  • 打赏
  • 举报
回复
引用 5 楼 fengzaiyaoye 的回复:
[quote=引用 3 楼 lianshaohua 的回复:] 没细看代码,确定是sprintf的问题?调试一下吧;
额,刚刚找到问题了。
char* a = new char(fileFullName[i].length()+1);
是这个的问题 要将小括号改成中括号。
char* a = new char[fileFullName[i].length()+1];
请问下这两个有什么区别?[/quote] new char();new 一个char并使用()中的值初始化 new char[];new 一个长度为[]中的字符数组
fengzaiyaoye 2015-03-06
  • 打赏
  • 举报
回复
引用 3 楼 lianshaohua 的回复:
没细看代码,确定是sprintf的问题?调试一下吧;
额,刚刚找到问题了。
char* a = new char(fileFullName[i].length()+1);
是这个的问题 要将小括号改成中括号。
char* a = new char[fileFullName[i].length()+1];
请问下这两个有什么区别?
fengzaiyaoye 2015-03-06
  • 打赏
  • 举报
回复
引用 2 楼 bear234 的回复:
char* a = new char(fileFullName[i].length()+1); what the hell is this......... char * a = new char(99); char * a = new char[99]; they are NOT the same thing.....
find the problem......, what's the difference between char * a = new char(99); and char * a = new char[99]; ?
ztenv 版主 2015-03-06
  • 打赏
  • 举报
回复
没细看代码,确定是sprintf的问题?调试一下吧;
bear234 2015-03-06
  • 打赏
  • 举报
回复
引用 4 楼 fengzaiyaoye 的回复:
[quote=引用 2 楼 bear234 的回复:] char* a = new char(fileFullName[i].length()+1); what the hell is this......... char * a = new char(99); char * a = new char[99]; they are NOT the same thing.....
find the problem......, what's the difference between char * a = new char(99); and char * a = new char[99]; ?[/quote] int * p = new int(3); // u demand a space to put an int, its value is 3. int * p = new int[3]; // u demand a space to put an int array, which length is 3, and their values are not set.
bear234 2015-03-05
  • 打赏
  • 举报
回复
char* a = new char(fileFullName[i].length()+1); what the hell is this......... char * a = new char(99); char * a = new char[99]; they are NOT the same thing.....
一根烂笔头 2015-03-05
  • 打赏
  • 举报
回复
memcpy或者strcpy试试呢

64,680

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧