tags:

views:

70

answers:

2

I am coding for a rar password cracker. I am reading the password from a file and passing it to sprintf function. This is the code.

FILE* fp = fopen("password.txt","r");
    while ( fgets ( pword, sizeof(pword), fp ) != NULL )
    {
          sprintf(command, "rar e -p%s realp.rar", pword);
          printf(command);
          //system(command);                                               
    }

This code looks fine but it's not working. Therefore I commented the system fubction and printing the variable "command". The output is like this :

rar e -pfirstpassword
     realp.rarrar e -psecondpassword
     realp.rarrar e -pthirdpassword
     realp.rarrar e -pfourthpassword realp.rar

I can see it's breaking.The output should come like this.

rar e -pfirstpassword realp.rar
rar e -psecondpassword realp.rar
rar e -pthirdpassword realp.rar
rar e -pfourthpassword realp.rar

can anybody help me to solve this? Thanks in advance.

  • operating system : windows 7
  • compiler : dev c++
+2  A: 

The newline found by fgets() is kept in 'pword'. Remove it and then print each line with a \n instead and see if it works.

See the man page for fgets().

Try adding the following line after the fgets() call.

pword[strlen(pword) - 1] = '\0';
Plow
Thanks for the help, but I can't understand. Can you please elaborate.
Let me try this.
Thank You very much buddy. It worked. Thanks again.
@user417552 Great.
Plow
+1  A: 

Your pword each time through the while loop is followed by a new line. Thus the output:

rar e -pfirstpassword [NEWLINE]
     realp.rar

You do not end command with a new line. Thus the output:

[command1][command2][command3]

Combining the two problems (adding braces around each loop iteration you get:

{rar e -pfirstPassword [NEWLINE]
         realp.rar}{rar e -psecondPassword [NEWLINE]
         realp.rar}

To fix the problem. Remove the new lines from the end of each password line.


To expand: fgets documentation for windows is available here: http://msdn.microsoft.com/en-us/library/c37dh6kf(VS.80).aspx

From the documentation:

The fgets function reads a string from the input stream argument and stores it in str. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in str is appended with a null character. The newline character, if read, is included in the string.

The Newline character is therefore included as part of the string written to pword. As you don't remove this character there is a newline in the middle of command when you write it with printf.

sir.jamesgreen
means I have to modify the file that contains password?I can't do that because the code i have written to generate all possible combination of a word, prints all the strings in new line. I can change that but I don't think it will be a good idea. Can I change this code and solve the problem??
Yes, as shown in the answer above, all you have to do is remove the newline character from the string. The code fragment shown. -- pword[strlen(pword) - 1] = '\0'; --will do that in a simple way.
sir.jamesgreen