0

I am writing a function that reads 10 .pgm files into a 2D array images using ifstream

void readPGMImages ( unsigned char images [784][10], int img_size ,
                    const char * filename , int imgNum ){...

where the first dimension img_size is the number of "pixels"(0-783) in the image and the second dim imgNum is the image number(0-9). The place I am having trouble is that I am required to use const char* filename to read in the new file title every time the image number changes. I just do not understand how to store the values of filename properly or how to read in the names using 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 
                }

the names of the files are

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";

So in summary I have a pointer const char* that points to filename and I need const char* filename = digit_00.pgm on the first iteration then I need filename = digit_01.pgm on the second iteration and so on.

If I wasn't required to use const char* I would make an array of strings and increment the string over the for loop but that is not what the problem I am tasked with solving is asking.

Thank you so much in advance! I will monitor the thread to clear up anything.

  • 1
    `std::string` has a member function [`c_str()`](https://en.cppreference.com/w/cpp/string/basic_string/c_str) that returns a null terminated cstring. You might be interested in it. – ph3rin Oct 20 '19 at 23:30

1 Answers1

0

Welcome to Stack Overflow!

It looks like some of your variables, const char* filename and int imgNum, are inputs to your function, but you're ignoring whichever values are passed in, and setting the variables yourself inside the function. Instead, assuming you intended to use them as private variables for the function, you should make those local (like your int header; and ifstream PGM;).

Another problem is that this line doesn't actually store all 10 of the different filenames:

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";

Rather, it ignores the first 9 using the comma operator and only stores filename = "digit_09.pgm";. Instead, you can use an array const char *filenames[10] and filenames[imgNum] inside the loop, or you can build the string dynamically from the current imgNum and then use .c_str() as someone suggested in a comment above.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • Thanks for the help! the `fiIename=...` was more of a pseudo code-ish example. I have tried to use `const char *filenames[10]` but I get a **Redefinition of 'filename' with a different type: 'const char *[10]' vs 'const char *'** error how do I change the parameters so that it all fits together. – StoneCodeStawny Oct 20 '19 at 23:53
  • Do you actually want `filename` to be passed as an input to the function? That error comes from trying to have 2 variables with the exact same name. You should either make the names different, or preferably remove one of the variables since there's no reason to have it be both an input and a local variable. – jtbandes Oct 20 '19 at 23:56
  • Darn I spoke too soon I am getting an error in `PGM.open(filename[imgNum]);` the error is `No matching member function for call to 'open'` do you know why this would be? I defined `const char* filename[10]` before the function and it passes in as an input fine now. – StoneCodeStawny Oct 21 '19 at 00:13
  • 1
    okay I see I didn't pass `filenames**[10]**` in i just put `const char *filenames`. all seems to be good. I can't tell you how grateful I am for your help. Thank you! – StoneCodeStawny Oct 21 '19 at 00:28
  • You're welcome! In the future I would recommend including more of your real code and clearly label which parts are psuedocode; that will make it clearer what problem you're encountering and what you are trying to do. See also [ask] and [mre] – jtbandes Oct 21 '19 at 00:34