Return a function's value to main()

closed account (G6RLy60M)
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
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.
closed account (G6RLy60M)
Hmm, you are right, but how can I pass the str value present in function string FDecode(char *DESTINATION) to console??
Isn't that what you're doing at line 26?

BTW, line 26 needs an endl to flush the line.
 
cout << DESTINATION << endl;



Use Indentation to help us read your code.
closed account (G6RLy60M)
Nope... please check my function again

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
string FDecode(char *DESTINATION){ 

string line,str; 
ifstream myfile(DESTINATION); 

if (myfile.is_open()) 
{ 
//cout<<DESTINATION; // this outputs correctly 
while (getline (myfile,line)) //myfile.good() !myfile.eof() 
{ 
str = line; 
str.erase (str.begin()+0, str.end()-9); 
//cout<<str;  //But this is outputting null to the Console
} 
myfile.close(); 
remove(DESTINATION); 
return str; 
} 

//else cout << "Unable to open file"; 
//return; 
//return str.c_str(); 

} 


I need str value inside the while loop...
closed account (G6RLy60M)
@metulblur: I did the Indentation of code? Am I wrong any where, please let me know...
How long are the lines you are reading?
If a line is less than 9 characters in length, str is going to be empty.
closed account (G6RLy60M)
The contents of my file are: Current name of the file = ABCD-1234.
Its more than 9 chars...
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.
closed account (G6RLy60M)
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)
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
closed account (G6RLy60M)
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
Lets recheck:
You have
1
2
3
4
5
6
7
8
9
string line,str; 
ifstream myfile = ... 

while ( getline( myfile, line ) ) 
  {
    str = line; 
    str.erase( str.begin()+0, str.end()-9 ); 
    // use of str fails
  }

and a line of input looks like:
Current name of the file = ABCD-1234

Therefore, you presumably want the str to contain
ABCD-1234

Is it really so that exactly the last 9 characters of each line are the text you want and that the beginning of line has varying content?

I would use
1
2
3
4
5
while ( getline( myfile, line ) ) 
  {
    str = line.substr( x ); // where x is length of the unnecessary prefix
    // use the str
  }

Topic archived. No new replies allowed.