How do I move a file after fclose() using C - c

I'm trying to move some .csv files from a directory "NotImported" to another directory "Imported" after each file fclose().
Unfortunately I couldn't find a way!

I would say,
rename(const char *oldpath, const char *newpath);
all explained here (it's long to read but you'll learn a lot each time): man page of rename

Related

Appending a binary file to another with c

people of StackOverflow,
I'm pretty new to c and wanted to set myself a challenge:
Adding a binary file (like a .exe file) to another, so when the second one is started, bot files are executed. This is the code I tried:
FILE* fp1;
FILE* fp2;
fp1 = fopen("path_to_file1","rb");
fp2 = fopen("path_to_file2","ab");
fseek(fp1,0, SEEK_END);
int size = ftell(fp1);
rewind(fp1);
unsigned long buffer[size];
fread(buffer,sizeof(buffer),1,fp1);
fclose(fp1);
fwrite(buffer,sizeof(buffer),1,fp2);
fclose(fp2);
All of this is running in the main function.
The problem is when executing the code, instead of appending the first file to the second, it overrides the first one, and on execute only executes the second file.
I really hope you can help me :)
Your code is incorrect because by doing this
unsigned long buffer[size];
you're writing sizeof(buffer) which is sizeof(unsigned long) times too big. So the first file is appended to the second file all right, but a lot of garbage is appended too.
You should declare:
unsigned char buffer[size];
so sizeof(buffer) yields the correct result.
Also, check the return value of your fopen statements.
EDIT: it just hit me that you want to append 2 executables together. The above (fixed) code works to append 2 binary files (data files) together, but for executables it just doesn't work.
Appending an executable in the end of another one is likely to be ignored by the operating system. The first executable header contains the logical size of the program segments. It doesn't read the file beyond that (well, it can be done by a lot of hacking and it's called a virus), which explains that your updated files runs like if nothing was appended.
One could imagine running a disassembler on both files, modify start points+add a wrapper to call both start points, and reassemble to another executable.
The easiest way to execute one program, then the other one, just call them in a script (bash, .bat, whatever) or in C system calls.
system("path_to_file1");
system("path_to_file2");

How to remove a file in C program?

How do I close a file and remove it?
I have the following code:
FILE *filePtr = fopen("fileName", "w");
...
Now I want to close filePtr and remove the file "fileName".
Should I:
fclose(filePtr);
remove("fileName");
Or:
remove("fileName");
fclose(filePtr);
Does it matter which I do first?
Thanks!!
That is OS-dependent. On *nix, deleting an open file leaves it open and the data on disk, but removes the filename from the filesystem, and actually deletes the file on close; some other operating systems may not let you delete an open file at all. Therefore the former is recommended for maximum portability.
It makes more sense to fclose and then unlink.
As man unlink(2) says (for Unix systems) :
The unlink() function removes the link
named by path from its directory and
decrements the link count of the file
which was referenced by the link. If
that decrement reduces the link count
of the file to zero, and no process
has the file open, then all resources
associated with the file are
reclaimed. If one or more process
have the file open when the last link
is removed, the link is removed, but
the removal of the file is delayed
until all references to it have been
closed.
So the order doesn't matter at all.
You do not need to fopen a file to remove it. But, in linux, if you remove an fopened file, it will be deleted only after closing it. You can still read/write to it.

opening a rar file by c

I have to write code in C to extract a password protected rar file in windows. I don't have any clue about how to do this. can anybody suggest me something or provide a sample piece of code? I will be very thankful.
EDIT:
This is the code I am using to open the rar file.In the system command ranjit is the password. It's giving the error undefined symbol_system in module+thefile name. Can anybody help me?? I am struggling on this since two days.
EDIT: This code opens the archive but do not extract it. If I uses the unrar command in command line, it extracts the file. What I should I do?
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv)
{
char file[20];
char file2[50] = "F:\\Program Files\\WinRAR\\unrar.exe";
printf("enter the name of the rar file : ");
gets(file);
puts(file);
system(("%s e -p ranjit %s >C:\stdout.log 2>C:\stderr.log",file2, file));
getchar();
return 0;
}
In addition to what karlphilip's suggestions there's also a couple of potentialliy interesting looking resources at http://www.rarlabs.com/rar_add.htm.
In particular I am thinking UnRAR.dll and UnRAR source may be relevant. I can't really check it out at the momment though.
Using unrar library - extracting files into a filestream buffer
But if you're looking for a pure C solution, take a look at: http://www.unrarlib.org/
Quote from their FAQ: The URARFileLib (short name for UniquE RAR File Library, also called unrarlib) is a free library for C programmers to access RAR archives.
Another approach, which I just tested successfully, doesn't require the use of external libraries to decompress rar files. Use system() to invoke a command-line tool (such as unrar ) already installed on your system to do the job:
system("unrar x -ppassword protected_file.rar /destination_directory");
For instance, let's say the protected file was named file.rar, the password was 1234 and the destination directory was /home/user, you would call system() with the following parameters:
system("unrar x -p1234 file.rar /home/user/");

Retrieving the name of the file that is associated with a file pointer

Assume I have a file pointer FILE* myfile. Is there a way to retrieve the name of the file where myfile is reading from or writing to?
Not in any CRT implementation that I've ever seen. It is useless info, you already have to supply the file name to get a FILE*. You could probably dig an operating system handle out of the FILE structure although you might need to hop through a file descriptor table. Your next problem is then the operating system support you'd need to map a file handle back to a file name. That should be difficult too.
I found a nice example that uses an overwritten struct MyFile:
How to get filename from a FILE*

Add lines at the end of file

I would like to add logs at the end of a file for each event, and create a new one when its size up to 255 Mo.
For example, the current file could be /var/log/foo.2:
/var/log/foo.0.log (full log file)
/var/log/foo.1.log (full log file)
/var/log/foo.2.log
Have you got an idea of C source to do so?
Thank you
When opening the file with
File *fopen(const char *filename, const char *mode);
choose "a" as mode. Which will open or create a text file for writing at the end of file.
Basic c file code: http://c-programming.suite101.com/article.cfm/c_tutorial_file_handling_commands
If you just want to have the functionality, look at log4c.
If you want to know how that works, you can look at its code as well.
If you need specially tailored code for your application...
Basically you need to use fopen with the "a" option, where a stands for append.
To determine size of a file, use ftell or another platform specific function because there is no file size information method in the C standard as far as I can remember. Or you go the long way and just read all the bytes from the file and count them...

Resources