Sep 5, 2013 at 11:52am UTC
How can I pass the str value in the function char FDecode(char *DESTINATION) to main().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <fstream>
using namespace std;
void FCrypt(char *str){
int len = strlen(str);
int ch;
for (int i=0;i<len;i++){
ch = str[i];
ch = ~ch;
str[i]=ch;
}
}
string FDecode(char *DESTINATION){
string line,str;
ifstream myfile(DESTINATION);
if (myfile.is_open())
{
//cout<<DESTINATION;
while (getline (myfile,line)) //myfile.good() !myfile.eof()
{
str = line;
str.erase (str.begin()+0, str.end()-9);
//cout<<str; // this is outputting null to the Console
}
myfile.close();
remove(DESTINATION);
}
else cout << "Unable to open file" ;
//return str.c_str();
return str; // this has to be shown to console...
}
int FDecrypt(char *SOURCE, char *DESTINATION){
char Byte;
FILE *inFile = fopen(SOURCE,"rb" );
FILE *outFile = fopen(DESTINATION,"wb" );
if (inFile==NULL||outFile==NULL){
if (inFile) fclose(inFile);
if (outFile) fclose(outFile);
return 1;
}
else {
while (!feof(inFile)){
Byte = (char )fgetc(inFile);
char newString[256];
sprintf(newString, "%c" , Byte);
FCrypt(newString);
fputs(newString, outFile);
}
fclose(inFile);
FDecode(DESTINATION);
}
return 0;
}
int main()
{
//char *d;
FDecrypt("/home/highlander/NetBeansProjects/simple_erase/dist/Debug/GNU-Linux-x86/text.dat" ,"/home/highlander/NetBeansProjects/simple_erase/dist/Debug/GNU-Linux-x86/text_1.dat" );
//string deco = FDecode("");
//FDecode(deco);
//cout<< deco;
return 0;
}
Last edited on Sep 5, 2013 at 1:06pm UTC
Sep 5, 2013 at 12:00pm UTC
I'm not understanding what you're asking.
At line 74, you're assigning the string returned to deco. Is that not what you want?
Your implicit open of DESTINATION at line 22 is going to fail if you pass in a null string at line 74.
Sep 5, 2013 at 12:02pm UTC
Hmm, you are right, but how can I pass the str value present in function string FDecode(char *DESTINATION)
to console??
Sep 5, 2013 at 12:08pm UTC
Use Indentation to help us read your code.
Sep 5, 2013 at 12:18pm UTC
@metulblur: I did the Indentation of code? Am I wrong any where, please let me know...
Sep 5, 2013 at 12:20pm UTC
How long are the lines you are reading?
If a line is less than 9 characters in length, str is going to be empty.
Sep 5, 2013 at 12:25pm UTC
The contents of my file are: Current name of the file = ABCD-1234
.
Its more than 9 chars...
Sep 5, 2013 at 12:33pm UTC
It has already been mentioned that the value you feed to FDecode , which is ""
is not a valid file name, so that the body of the while loop will never be entered as the file will not be opened .
Sep 5, 2013 at 12:40pm UTC
Thats right cire, please check my code now, I've updated it... I need value of str
in while loop
of function: string FDecode(char *DESTINATION)
Sep 5, 2013 at 12:54pm UTC
You're trying to treat binary data as if it was text. Don't do that.
[Edit: at least if the file content of the source file is what you mention above.]
Last edited on Sep 5, 2013 at 12:59pm UTC
Sep 5, 2013 at 1:02pm UTC
Well cire, that is not the problem, please let me know how can I show the str
value to console. Even though the DESTINATION is a .txt file, I guess its unable to read? the file permissions are set to 0777!
Last edited on Sep 5, 2013 at 1:07pm UTC