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/");
Related
I need to make a game in C for my finals. The user should input the map file he wants to play.
Here's my simple code:
int main(){
FILE *map;
char fileToRead[100];
do{
printf("Insert file name: ");
fgets(fileToRead, 100, stdin);
map = fopen("/Users/rajunior/Desktop/map_2.txt", "r");
//map = fopen(fileToRead, "r");
printf("%s", fileToRead);
If I use the "map = fopen("/Users/rajunior...)" hardcoded, it works!
But I need to use the second (commented) option; the first one is useless for my purpose.
In other words, I need the fileToRead to be in the same directory as my .c, but how?
screenshot: https://imgur.com/a/DbX9tw4
Option 1: Install the command line tools. Put the C file and the text file in the same directory. Open a terminal window. Compile and run from the command line. If I recall correctly, the command line tools download can be found in Preferences.../Downloads.
Option 2: Go to the Product/Scheme/Edit Scheme... menu. When the dialog box appears, select Run at the left and Options at the top. Then look for Working Directory. Set the working directory to point to the directory where the text file is.
This was going to be a comment, but it is too long for comfort.
You'll need to know the current directory of the process when it is run. If you run it from the shell, the current directory of your program will be the same as the current directory of the program. If you run it from within XCode, I've no idea what the directory will be, but it probably won't be where the source is — it'll be in a build directory of some sort, probably.
Your program can find out where it is run from with getcwd(). Then you'll be able to tell how to chdir() to the directory where the source is (as long as the program knows where the source is, because you told it somehow — argument or command line variable, or …). Or you can determine how to create a relative path name that will find the file in the source directory.
There's probably an XCode (maybe Objective-C) way to find the information, perhaps via plists.
I don't code for a Mac; I only code on a Mac, and I run XCode itself rather seldom.
I am trying to get the HDD serial key of a windows PC using system() command and save that number in a text (.txt) file with a file name that user chooses. Everything is working fine before the system() command, but the system() command is NOT changing the file name,that is, instead of naming the file after the user choice, it is just naming it after "contract_file_name" with no extension. For example: if I give file name: blahblah , it's supposed to create a text file with the name "blahblah.txt" (containing HDD serial Key), but instead it's creating a file with "contract_file_name".
Here is code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char contract_file_name[100];
FILE *fp1 ;
filename:
printf("Please Give an APPROPRIATE name for SENDER-RECEIVER CONTRACT file. Please Don't use any .txt extension\nFILE NAME: ");
gets(contract_file_name);
strcat(contract_file_name,".txt");
if((fp1=fopen(contract_file_name,"r")))
{
printf("A Contract File with %s already EXITS.Please Choose another name\n".contract_file_name);
goto filename;
}
else
{
fp1= fopen(contract_file_name,"w");
fprintf(fp1,"$Sender: %s\n",getenv("USERNAME"));
fclose(fp1);
system("wmic path win32_physicalmedia get SerialNumber >> contract_file_name"); //Having problem in this line,I think.
fp1 = fopen("contract_file_name","a");
fprintf(fp1,"\n");
fclose(fp1);
}
return 0;
}
I can feel that the problem is with my method system() command, But can't find any solution.Can anyone please suggest me how to FIX this problem?
Thanks in Advance.
The problem is not with the system function (which you shouldn't be using for this task anyway), but with your misunderstanding how string substitution works.
You have a variable contract_file_name of type "array of 100 char" and apparently expect every occurance of "contract_file_name" inside a string to automatically being replaced. This is called "variable expansion" and is in fact supported in some languages. Most notably shell and Perl. However you actually have to mark the variables in a string for replacement (usually by prepending a $ sign). That does not work in C! C is a very frugal language and doesn't come with that feature.
Anyway your program immediately reads back the file and hence using a fixed filename or a filename at all is a bad idea anyway (think about what might happen if several instances of your program get run at the same time; can you prove that all parts in play are idempotent? probably not).
Instead you should run wmic with popen. With popen the output of wmic is written to a FIFO which you can read directy as if it were a file, without a file ever being written to a disk: https://msdn.microsoft.com/en-us/library/96ayss4b.aspx
You have contract_file_name is a literal string in your system call. You have to put the value of the variable in the string first, something like
char tmp[100+50];
sprintf(tmp, "wmic path win32_physicalmedia get SerialNumber >> %s", contract_file_name);
system(tmp);
You should also limit what you read into contract_file_name to 99 characters.
I currently have a short program to read and sort a text tile in C.
If I want to read many files, is there a substitute for:
FILE *f
f = fopen("*.txt", "rw");
Thanks in advance.
f = fopen("*.txt", "rw"); won't work in any case.
The usual way to do this probably depends on your operating system. On Unix-like systems, the simple way is to invoke your program with a command line like "my_pgm *.txt" and let the shell find the matching files. (You'll get multiple arguments, each one being a file name.) I understand that microsoft OSes would require the program to find the files itself.
To do that more or less portably, I'd probably use opendir() and readdir() to examine directory entries and see whether they matched the desired pattern.
Input files to test project 2. Intended to be used via redirection.
My professor gave us a txt file to use to test if our program works. It reads in ~1000 numbers (so we wouldn't have to manually enter them). But I don't know the linux command on how to use this txt file.
ccarri7#ubuntu:~/C$ ls
ccarri7lab2 ccarri7lab2.c lab2input.txt
ccarri7#ubuntu:~/C$
This is the folder where my executable/source/txt file are.
./ccarri7lab2 < lab2input.txt
will use the text in lab2input.txt as arguments for ccarri7lab2
http://linux.about.com/od/itl_guide/a/gdeitl42t01.htm
Did you try
ccarri7lab2 < lab2input.txt
Hope this is what you want, else you can give some more info.
My own tar file can currently use the 'c' and 't' commands correctly (creating he archive, reading the archive), but I'm completely unaware of how to implement 'x' (extracting the archive).
Using C code, how can I recreate a directory / file? I know I can successfully read what is in my .tar file, I'm just not sure of the C function that is used to create directores / files.
NOTE: I've asked multiple people, they couldn't help me. Googled the problem for an hour, but the question is vague enough that I got about 10000 websites answering a different problem.
Use fopen to create a file (and write to it); use mkdir to create a directory.
on unix systems ,you'd use mkdir()