Accessing a different drive in c - c

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

'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
}
}

Related

renaming, and moving files from a directory using c

i am working on a home made little program that is going to make my life a lot easier.
the idea is it looks at a directory, sees the file names in it, removes all certain character combinations and certain characters from the name of the files, then copies the newly named file to a separate folder, and deletes the original.
i can do most of that. what i dont know is how to load the file names into my program i can figure everything else out as i know how to manipulate strings in C and so on.
ive been looking for an easy to implement solution for a few days and found nothing.
tldr:
look at directory
load all file names
change all file names based on criteria
copy files to new directory.
i dont really know how to do step 1, 2, or 4.
i dont expect you guys to write the program for me, even a library and command suggestion would be great if there is one.
See a few related questions : How can I get the list of files in a directory using C or C++?
Read file names from a directory
And as stated on this page :
https://www.geeksforgeeks.org/c-program-list-files-sub-directories-directory/
Using the <dirent.h> module, doing so :
#include <stdio.h>
#include <dirent.h>
int main(void)
{
struct dirent *de; // Pointer for directory entry
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(".");
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory" );
return 0;
}
// Refer http://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
// for readdir()
while ((de = readdir(dr)) != NULL)
printf("%s\n", de->d_name);
closedir(dr);
return 0;
}
Would allow you to see the files and directories inside a directory.
I think with this, you should be good.

Where to put files so C program can access them?

I'm trying to open files in a C program, but I am unsure where to place the files I want to open (as in which directory). Here is the code, but I really just need to know where to place the file I want to open with fopen().
FILE *fileptr;
fileptr = fopen("QuizQuestions.txt", "r");
if (fileptr == NULL) {
printf("Unable to open file.");
}
Any help is appreciated!
If you don't use an absolute pathname in your code, paths are interpreted relative to the working directory of the user when they run the program. So for your program, the user should put the file in their current directory.
The location of the program itself is irrelevant. If you want to get the location of the program, you see this question:
How do I find the location of the executable in C?
You can then concatenate the directory with the filename.
You need to keep files where source code file is placed.Otherwise, you need to give absolute path.

Open file in C linux

I am using C to open a file for reading. I have this code :
fp = fopen("./settings.cfg","r");
if (fp != NULL)
printf("OK");
else
printf("ERROR");
but I always get an error.
The file is located in the folder where the executable resides. I have tried writing only "settings.cfg". What might be the problem?
Try perror() to have the library itself tell you what, if anything, is wrong.
fp = fopen("./settings.cfg", "r");
if (fp != NULL)
printf("OK\n");
else perror("fopen");
You are opening the file in the "current directory", not "in the folder where the executable is".
In fact, unix has no easy way to find that particular folder; in Linux you could readlink() the /proc/[your pid]/exe link to find the executable and strip off the filename portion -- that will work in many cases, but there are some fringe cases like hardlinks that will make it fail.
From which directory do you run the program? It won't have the directory where it resides as the current directory, it will be inherited from the environment.
Could also be rights, that the file is owned by someone else and you don't have read rights.
Also double-check the filename. This sounds obvious, but do it anyway.
If file you are trying to open is in the same directory of your C compiled file, you must simply do
fp = fopen("settings.cfg","r");
if (fp != NULL)
printf("OK");
else
printf("ERROR");
without the initial " ./ " at the file's name

open different file using C programming

Hello ever one I want to ask a question about file opening.I want to write a program in which I will give a path to my main folder.main folder can contain files and and other folders which in again contain other folders and files.I want to open all the file in the loop and do some manipulation on them also I want to open only specific file for example .c extended file.Is there a built in program or function that do that? or atleast is there a way through which I can check which files are there in the folder so that I can repeatedly open them
I am using C programming linux
Thanks
You can save yourself a lot of work and look at ftw() and nftw(). They will walk through directories and their entries , starting with the path you provide, and a call a callback function that you provide. In your callback you can check if the file is relevant for purpose and operate on it if it is.
Also glob() will save you some effort if you are going to be doing a lot of filename matching.
I am not aware of any built-in functions for what you want to do here. But you could use dirent.h.
int main(){
DIR *dir;
struct dirent *ent;
dir = opendir ("c:\\folder\\");
if (dir != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
printf ("%s\n", ent->d_name);
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}
}
Here you can find even more examples.
opendir, readdir, closedir.
if dirent->d_type==DT_DIR, descend into it (recursion would help).
Take a look at the file name, figure out if you're interested.

open file dir problem

I'm running an Linux OS and trying to open file in C compiler like this :
file = fopen ("list.txt", "r");
but the file is not opend!
and when i put the full path like this :
file = fopen ("/home/rami/Desktop/netfilter/list.txt", "r");
it is working!
why the first example is not working?
the list.txt is in the same directory of the c file
thanks.
It's not the directory of the C file that matters, it's your current working directory that does. Try
cd /home/rami/Desktop/netfilter
before running the executable.
Do you know WHY the file didn't open?
Always check the return value of fopen() (and most other functions) and report back a readable error.
file = fopen("file.txt", "r");
if (!file) {
perror("file open");
exit(EXIT_FAILURE);
}
I see you've already found out what your problem is, but the above is a suggestion for the future (and to change your current project)
Is the executable also in the same dir as that of list.txt?
Edit: Actually that doesnt matter. It's the current working dir as per the other answer.

Resources