Overclock.net banner

1 - 3 of 3 Posts

·
Registered
Joined
·
3 Posts
Discussion Starter #1
I need to write a program to decrypt a 12 character message located in a file named “encrypted.txt†then print the decrypted message to the screen and a new file named “decryptedâ€, however I can't get the encrypted.txt file to open.

this is what I have so far:

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fp;

if ((fp = fopen("encrypted.txt", "r")) ==NULL)
{
printf("Input file could not be opened\
");
exit(1);
}
return 0;
}

I keep getting the fopen command to return a null, so should I create a file called encrypted? if so where? I wasn't able to find where stdio.h was located.

If anyone has any input it would be appreciated, thank you.
 

·
Banned
Joined
·
12,758 Posts
The path you give for the .txt file must be relative to the .exe I believe. So it is likely only to work like you have it if you put the .txt file in the same directory as the .exe.
 

·
Premium Member
Joined
·
2,767 Posts
You're trying to open "encrypted.txt" in read mode "r". If the file doesn't exist, it's going to return a null file descriptor because there's nothing to read. As lordikon said, you'll need to create the file in the same directory as the exe, or give an absolute path in the filename string. ie "/pathtofile/encrypted.txt" on *nix or "c:\\\\pathtofile\\\\encrypted.txt" on Windows.

Edit: Note the double backslash for the absolute path in the Windows version of the filename string. In C, the \\ is used an an escape character to represent special sequences that aren't literally translatable into readable text. The \\\\ means "don't look for an escape sequence, I actually mean to have a backslash here".
 
1 - 3 of 3 Posts
Top