Question: 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++

Answer: 
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()](http://www.manpagez.com/man/3/fgets/).

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

```
pword[strlen(pword) - 1] = '\0';

```
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.