-1

I want to scan a directory, checking for JPEG-files (i.e. the combination of the first few bytes) and copying them to another drive. I successfully tested that program and it screens documents. However, I cannot access a whole directory (e.g. my accidentally erased SD-Card in D:/).

Here is how I tried to access it:

// remember the path from the command line
char *path = argv[1];

// open the path (preferably "D:\" - which is my SD-Card ;))
FILE *inptr = fopen(path, "r");
if (inptr == NULL)
{
    fprintf(stderr, "inputfile could not be read\n");
    return 1;
}

The output is "inputfile could not be read" - which is why I am quite confident that the error is right there. Do I need to address a directory differently? E.g. by using a pointer to the first bit of the drive?

I am a beginner - so please be gentle while laughing. ;)

Thank you very much! Marcel

McCahen
  • 3
  • 3
  • What is the value of `path`? How are you verifying that it has the value you think it has? Please update your question with runnable code that reproduces the error. – larsks Aug 06 '17 at 20:56
  • 4
    You can't open a directory with fopen. See [this](https://stackoverflow.com/questions/12489/how-do-you-get-a-directory-listing-in-c). – rustyx Aug 06 '17 at 20:56
  • fopen() can only be used to open a specific file, not a directory. So unless `path` is the path to a specific file, that is likely your problem. – Dsel Aug 06 '17 at 20:58
  • "accidentally erased SD-Card" don't they have a write-protect switch? – Weather Vane Aug 06 '17 at 21:04
  • 1
    Possible duplicate of [How do you get a directory listing in C?](https://stackoverflow.com/questions/12489/how-do-you-get-a-directory-listing-in-c) – melpomene Aug 06 '17 at 22:23
  • Note that you should include the file name in the error message, which you could then quote. `fprintf(stderr, "file '%s' could not be opened for reading\n", path);`, for example, which also correctly reports errors on the standard error stream. This helps everyone with the debugging. – Jonathan Leffler Aug 07 '17 at 03:34
  • Your path "D:\" let me assume you're on Windows. AFAIK, the MS SDK doesn't provide `dirent.h` (with `opendir()` and `readdir()`). Thus, I want to mention the following links: [SO: Microsoft Visual Studio: opendir() and readdir(), how?](https://stackoverflow.com/a/883640/7478597) and [SO: Listing directory contents using C and Windows](https://stackoverflow.com/a/2315808/7478597). – Scheff's Cat Aug 07 '17 at 13:33
  • Thank you very much! Together with the answer below, I will probably be able to write a functioning program now. :) @larsks: It's 'D:\' as typed on the command-line (argv[1]). Sadly I won't be needing my code anymore but will adapt it (see answer below). Would you suggest me to copy-paste the whole program for a next question? I was unsure whether that would not be unnecessarily spamming the question... – McCahen Aug 08 '17 at 12:19

1 Answers1

0

'fopen()' cannot open an entire directory or drive, it can only open a specific path. However, there are ways to accomplish the task of listing all files in a directory. This answer already contains some pertinent information.

Since you can already open files,you could adapt the loop shown there to perform the operation.

while ((dir = readdir(d)) != NULL)
{
    printf("%s\n", dir->d_name);
    if (/*Check whether file has the .jpg extension or another test*/)
    {
        // perform your copy operation on the file
    }
}
Scott Forsythe
  • 360
  • 6
  • 18
  • Thank you very much for the rapid answer! So - fopen() can only access individual files, which is why you suggest me to create a list of all files in the directory and copy the files with ".jpg" in the name. Neat! Now - I found out that readdir() returns the next entry from the directory (thanks, gnu.org). Am I understanding this right though: If a file has been erased and the respective bits have not been overwritten, it is still an "entry" that readdir() can and does recognize(?). – McCahen Aug 08 '17 at 12:02
  • As far as I know, readdir will only read existing, non-deleted files. – Scott Forsythe Aug 08 '17 at 12:50
  • @McCahen: "*If a file has been erased and the respective bits have not been overwritten, it is still an "entry" that readdir() can and does recognize(?).*" No. – alk Aug 12 '17 at 16:25