so how do you use cout to a file??
Main Topics
Browse All TopicsI am trying to make an encryption/decription tool, but I can't make it work. It compiles perfectly, but it does not create the output file. Here is the code for the encryption function:
void encriptar(char* str){
FILE *source, *dest;
char ch;
int i;
source=fopen(str,"rt");
if(source==NULL){
cout<<"No se pudo abrir el archivo "<<str<<endl;//could not open file
cin;
system("cls");
return;
}
dest=fopen("encriptado.txt
if(dest==NULL){
cout<<"No se pudo abrir el archivo \"encriptado.txt\""<<endl;
cin;
system("cls");
return;
}
rewind(source);
while(!feof(source)){
ch=fgetc(source);
i=ch*ch;
fprintf(dest, "%i",i);
}
fcloseall();
cout<<"Se ha terminado de encriptar con éxito"<<endl;//successfull
cin;
}
Sorry for the low points, but I don't have much..
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You can use the _getcwd() function to get the current directory, and concat the current directory with the file name.
If you want it to go into the program directory, you can get the program directory by looking at the path pass to the main function.
Example:
int main(int argc, char* argv[])
{
printf("My path is %s\n", argv[0]);
return 0;
}
Better yet:
#include <direct.h>
std::string GetCwd(const char* FileNameOnly)
{
char buffer[1024];
getcwd(buffer, sizeof(buffer));
strcat(buffer, "\\");
strcat(buffer, FileNameOnly);
return buffer;
}
int main(int argc, char* argv[])
{
std::string CurrentWorkingDir = GetCwd("encriptado.txt");
std::cout << CurrentWorkingDir << std::endl;
return 0;
}
I tried using your function, axter, but it doesn't work. I had to modify it a little (added buffer[] to the arguments, instead of declarating it), but now my program crashes even before it starts showing text on the console.
BTW, I am not using strings, only char arrays, because I don't feel confortable with strings.
Then you have to pass in the target buffer that you want to take the value.
Example:
#include <direct.h>
char* GetCwd(char * TargetBuffer, int SizeOfBuffer, const char* FileNameOnly)
{
getcwd(TargetBuffer, SizeOfBuffer);
strcat(TargetBuffer, "\\");
strcat(TargetBuffer, FileNameOnly);
return TargetBuffer;
}
int main(int argc, char* argv[])
{
char FullPathAndFileName[1024];
std::cout << GetCwd(FullPathAndFileName
return 0;
}
If the above does not work, please give details of what exactly doesn't work.
Give ALL the details that you can. The more information the better.
>>char* GetCwd(const char* FileNameOnly, char buffer[1024])
That's incorrect. Use the method I posted instead:
char* GetCwd(char * TargetBuffer, int SizeOfBuffer, const char* FileNameOnly)
And pass in the variable as posted in above example:
char FullPathAndFileName[1024];
GetCwd(FullPathAndFileName
You should try the above method first, and then try experimenting with it after you have a working method.
Business Accounts
Answer for Membership
by: person1994Posted on 2002-11-02 at 15:34:54ID: 7401535
One thing you may want to try, is not to use a mix of cin/cout with fptinf. I've ran into problems when I mix the two. So just to try it out comment out all your cin/cout lines.
Good luck