This question already has answers here:
Hide a file or directory using the Windows API from C
(4 answers)
Closed 8 years ago.
Does anybody know if it is possible to hide files or make them invisible to other users? Or, does creating file in "w" mode achieve invisibility?
Ex:
If I create a file like this:
FILE *fp = fopen("aFile","w");
Can other users on my system read it?
I guess I'm asking for the C way to add access modifiers to files in C, sort of like the chmod command does..
It is not possible to set access permission when using fopen.
Use CreateFile instead to open file and set access permissions.
Related
This question already has answers here:
Using `read` system call on a directory
(2 answers)
Linux C read a directory
(2 answers)
Closed 3 years ago.
It's possible to read a regular file or a symbolic link file ( not the file it's pointing to ).
Is there a way to read the content of a directory file in a file reading fashion (e.g. fread()) .
I have a set of configuration files (10 or more), and if user opens any of these file using any editor (e.g vim,vi,geany,qt,leafpad..). How would I come to know that which file is opened and if some writing process is done, then it is saved or not (using C code).
For the 1st part of your question, please refer e.g. to How to check if a file has been opened by another application in C++?
One way described there is to use a system tool like lsof and call this via a system() call.
For the 2nd part, about knowing whether a file has been modified, you will have to create a backup file to check against. Most editors already do that, but their naming scheme is different, so you might want to take care of that yourself. How to do that? Just automatically create a (hidden) file .mylogfile.txt if it does not exist by simply copying mylogfile.txt. If .mylogfile.txt exists, is having an older timestamp than mylogfile.txt, and differs in size and/or hash-value (using e.g. md5sum) your file was modified.
But before re-implementing this, take a look at How do I make my program watch for file modification in C++?
This question already has answers here:
Detect if stdin is a terminal or pipe?
(6 answers)
Closed 5 years ago.
I'm currently working on my school project and wondered if there is any way to tell if user has written <textfile.txt to the terminal.
Like this: ./project <textfile.txt
My project reads all the lines from the text file to stdin and via scanf works with them. But when I don't use the <textfile.txt "thing", it starts without the data. Instead, I'd like it to write an error and stop the program.
Thanks a lot.
You might use isatty(3) to detect if your stdin is a terminal.
You could use fstat(2) on STDIN_FILENO, e.g. to detect redirection or pipelines.
Or (on Linux) readlink(2) /proc/self/fd/0 , see proc(5)
I recommend accepting some program argument to override such an auto-detection. Read this.
Be aware that redirection and globbing is done by the shell, see this.
On unix systems you can check whether stdin refers to a terminal:
if (isatty(0)) {
fprintf(stderr, "input was not redirected\n");
exit(EXIT_FAILURE);
}
This question already has an answer here:
How to add, delete edit username from /etc/passwd [duplicate]
(1 answer)
Closed 6 years ago.
I m playing with /etc/passwd in my C program.
I want to change a user password. are there a standard linux functions that do a such functions ?
Method 1: system("passwd <parameters>");
Method 2: *pwent() function family and also putpwent().
The question is possibly a duplicate.
Linux stores the password in the /etc/shadow file. The second column (delimited by : character) in this file against the user name shows the hashed password.
It's best advised to not touch this file and cause harm. If you must, you can use the vipw utility for editing
This question already has answers here:
C++/Win32: How to wait for a pending delete to complete
(13 answers)
Closed 9 years ago.
I have a file that was opened with with the Windows API CreateFile() using the FILE_SHARE_DELETE flag so that I can delete it while the file handle is still open. But in the window between the first process deleting the file and the process ending, I want to be able to tell that the file is being deleted so that I can go into a retry loop.
I found error code 303 that looks exactly like what I'm looking for:
ERROR_DELETE_PENDING
303 (0x12F)
The file cannot be opened because it is in the process of being deleted.
But I can't find what I can use that will return this code for me. Does anyone know how I can determine that my file is in this state of being deleted but still has an open handle?
On Vista and later, you can open the file with CreateFile() (make sure the FILE_SHARE_DELETE flag is specified so the open can succeed) and then use GetFileInformationByHandleEx() to retrieve the file's FILE_STANDARD_INFO structure. It has a BOOLEAN DeletePending data member that will be TRUE if the file has been marked for deletion.
To get that error code you need to call GetLastError immediately after the CreateFile function fails. It will return ERROR_DELETE_PENDING (the constant for error code 303) when that situation occurs