4

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++
6
  • 1
    Not working is not a very precise problem description. Commented Aug 29, 2010 at 13:19
  • I formatted the output. sorry for inconvenience. Commented Aug 29, 2010 at 13:21
  • This still doesn't answer my question. What is not working when you use the system command? Are you getting an error message, does your program/computer crash, do apples start to fall from the sky, ... ? Commented Aug 29, 2010 at 13:23
  • What's the content of the file? Commented Aug 29, 2010 at 13:26
  • I think, I did not explain the problem properly. This code should extract the rar file but it's not extracting. Instead it's opening all the options of rar command. Commented Aug 29, 2010 at 13:28

2 Answers 2

2

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';
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help, but I can't understand. Can you please elaborate.
1

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.

2 Comments

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.