4 registered members (ozgur, Ayumi, VHX, monarch),
1,161
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Cryptography - How to encipher a .txt-file?
#293329
10/10/09 18:37
10/10/09 18:37
|
Joined: Feb 2009
Posts: 38 Germany
Phonech
OP
Newbie
|
OP
Newbie
Joined: Feb 2009
Posts: 38
Germany
|
Hi there folks! I would like to have a programme that can encipher a text read out of a .txt-file and wrtie the enciphered in an other .txt-file. Ok, imagine the content of the cleartext.txt is "ABC" and I want the programme to convert A into X, B into Y and C into Z. The output.txt should now contain the letters "XYZ" as a result of our working script.^^ I've looked at Aum83 (2D Games)which contains a lot of helpfull stuff to solve the problem, but I haven't figured it out anyway. =P That's what I have...
STRING* data_str = "#10"; // the games can use up to 20 horizontal tiles
STRING* log_str = "#1";
STRING* temp_str = "#20"; // just a temporary string
function read_and_convert()
{
var source_handle, output_handle, index, string_size;
source_handle = file_open_read("cleartext.txt");
output_handle = file_open_append("output.txt"); // open both files
wait (3); // wait a bit
while (file_str_read(source_handle, data_str) != -1) // run loop till the end
{
string_size = str_len(data_str);
while (index < string_size) // go through all the characters, one by one
{
str_cpy (temp_str, data_str);
str_clip(temp_str, index);
str_trunc(temp_str, string_size - index - 1);
if (str_cmp(temp_str, "A") == 1) // an A?
{
str_cpy(log_str, "X"); //convert it into X!
}
if (str_cmp(temp_str, "B") == 1)
{
str_cpy(log_str, "Y");
}
if (str_cmp(temp_str, "C") == 1)
{
str_cpy(log_str, "Z");
}
file_str_write(output_handle, log_str);
index += 1;
}
file_close(source_handle); // so let's close the file
file_close(output_handle);
}
}
The output is " Z". Thanks for helping!
Last edited by Phonech; 10/10/09 18:38.
|
|
|
Re: Cryptography - How to encipher a .txt-file?
[Re: Phonech]
#293337
10/10/09 19:16
10/10/09 19:16
|
Joined: Oct 2007
Posts: 5,210 Ä°stanbul, Turkey
Quad
Senior Expert
|
Senior Expert
Joined: Oct 2007
Posts: 5,210
Ä°stanbul, Turkey
|
local vars are not guaranteed to be zero, init index like
index = 0;
also,
while (file_str_read(source_handle, data_str) != -1) this loop will work only one time, because after the first loop, you close the files.
3333333333
|
|
|
Re: Cryptography - How to encipher a .txt-file?
[Re: Quad]
#293341
10/10/09 19:34
10/10/09 19:34
|
Joined: Feb 2009
Posts: 38 Germany
Phonech
OP
Newbie
|
OP
Newbie
Joined: Feb 2009
Posts: 38
Germany
|
Got it! Thanks for your advice. If someone is interested here is the fixed code:
function read_and_convert()
{
var source_handle, output_handle, index, string_size;
source_handle = file_open_read("cleartext.txt");
output_handle = file_open_append ("output.txt"); // open both files
index = 0;
wait (3); // wait a bit
while (file_str_read(source_handle, data_str) != -1) // run loop till the end
{
string_size = str_len(data_str);
while (index < string_size) // go through all the characters, one by one
{
str_cpy (temp_str, data_str);
str_clip(temp_str, index);
str_trunc(temp_str, string_size - index - 1);
if (str_cmp(temp_str, "A") == 1) // an A?
{
str_cpy(log_str, "X"); //convert it into X
}
if (str_cmp(temp_str, "B") == 1)
{
str_cpy(log_str, "Y");
}
if (str_cmp(temp_str, "C") == 1)
{
str_cpy(log_str, "Z");
}
file_str_write(output_handle, log_str);
index += 1;
wait(1);
}
wait(1);
}
file_close(source_handle); // so let's close the file
file_close(output_handle);
}
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|