【发布时间】:2019-10-20 23:21:42
【问题描述】:
我正在编写一个函数,它使用 ifstream 将 10 个 .pgm 文件读入二维数组 images
void readPGMImages ( unsigned char images [784][10], int img_size ,
const char * filename , int imgNum ){...
其中第一个维度img_size 是图像中的“像素”数(0-783),第二个维度imgNum 是图像数(0-9)。我遇到麻烦的地方是,每次图像编号更改时,我都需要使用const char* filename 来读取新文件标题。我只是不明白如何正确存储filename 的值或如何使用const char* 读取名称。
void readPGMImages ( uchar images [784][10], int img_size ,
const char * filename , int imgNum ){
filename = "digit_00.pgm","digit_01.pgm","digit_02.pgm","digit_03.pgm","digit_04.pgm","digit_05.pgm","digit_06.pgm","digit_07.pgm","digit_08.pgm","digit_09.pgm";
int header;
ifstream PGM ;
PGM.open(filename);
for (imgNum = 0; imgNum<10; imgNum++){
PGM.open(filename);
PGM >> header >> header >> header >> header ; // this is b/c the first 4 characters of each are unused
for (img_size = 0; img_size<784 ; img_size++){
PGM >> images[img_size][imgNum];
}
const char* ++; // this is kind of pseudo code for what I am trying to accomplish
}
文件名是
filename = "digit_00.pgm","digit_01.pgm","digit_02.pgm","digit_03.pgm","digit_04.pgm","digit_05.pgm","digit_06.pgm","digit_07.pgm","digit_08.pgm","digit_09.pgm";
总之,我有一个指针const char* 指向filename,第一次迭代需要const char* filename = digit_00.pgm,然后第二次迭代需要filename = digit_01.pgm,依此类推。
如果我不需要使用const char*,我会创建一个字符串数组并在 for 循环中递增字符串,但这不是我要解决的问题。
提前非常感谢您!我会监视线程以清除任何内容。
【问题讨论】:
-
std::string有一个成员函数c_str(),它返回一个以空值结尾的 cstring。您可能对此感兴趣。
标签: c++