Asterix
Asterix

Reputation: 3

Not able to write to a file

My programs have been running properly for over a year. Today I copied the files onto a different system and compiled every program.

When I compile and run from Dev-c++ it writes data onto a text file like its supposed to, but when I click on the executable it creates, it does not write data onto the file. Everything else like input/output seems to work.

What procedure have I missed?

Ok i've given the program Full permision but it still does not write.

I'm quite puzzled, atleast if it didn't run when i compile it in the C++ environment i can keep checking my code, but only the .exe does not work, any other suggestions ?

 #include <iostream> 
 #include <fstream>
 using namespace std; 
 int main() {  
 ofstream bss2; 
 bss2.open("expat.txt",ios::app);
 bss2 << 2 ; 
 bss2.close();
 } 

This is the sample code i tested out.

How do i find the Current working directory ?

Ok i changed a line to

bss2.open("c:\\expat2.txt",ios::app);

and now it works properly in the exe file.

but there's over 50 files and i prefer i didn't have to spell out the new path to each one, what workaround is there to set the directory to the one previously used ?

update 4 :

 #define _POSIX_SOURCE
 #include <unistd.h>
 #undef _POSIX_SOURCE
 #include <stdio.h>

 main() {
   char cwd[256];
   int y;
   if (chdir("/tmp") != 0)
     perror("chdir() error()");
   else {
     if (getcwd(cwd, sizeof(cwd)) == NULL)
       perror("getcwd() error");
     else
       printf("current working directory is: %s\n", cwd);
   }
   scanf(y);
 }

Ok i used the getcwd() and this is the message it gives me

chdir() error(): No such file or directory

How do i set the directory now.

Upvotes: 0

Views: 247

Answers (6)

Prasanth Madhavan
Prasanth Madhavan

Reputation: 13309

The best and easiest way is to give the full path of the output file rather than just the filename. That way, you can be sure where the file went, and not have to search for it everywhere. If you are using Windows, the output file might be somewhere in system32. But I could be wrong.

Upvotes: 0

howderek
howderek

Reputation: 2244

It might be that Windows uses backslash, so try "\tmp" instead of "/tmp".

Also if all your files are in the same directory, then you can use find & replace and replace open(" with open("c:\\your_directory_here\

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308081

As others have said, the working directory is likely incorrect.

If you create a shortcut to the .exe, you can set the working directory in the shortcut properties. Right-click on the shortcut, select "Properties", and change the "Start in" property.

Of course a better answer is to put the full path of the file into the filename string when you open it.

Upvotes: 0

Kien Truong
Kien Truong

Reputation: 11383

Maybe your looking at the wrong location. The program will write the file to the current working directory, which may be different between when you double click on the executable and run from Dev-C++.

Upvotes: 1

damian
damian

Reputation: 3674

Sounds like your working directory isn't being set correctly when you double-click on the file. If you can access a log, use getcwd() and log what it returns.

Upvotes: 2

Pedro d&#39;Aquino
Pedro d&#39;Aquino

Reputation: 5220

I don't have Raymond Chen's psychic debugging powers yet, but I do know of a tool that may help you: Process Monitor. Use it to see precisely which files your application is trying to write to, and why it fails.

Upvotes: 1

Related Questions