开发者

Not able to write to a file

开发者 https://www.devze.com 2023-03-14 00:35 出处:网络
My programs have been running properly for over a year. Today I copied the files onto a different system and compiled every program.

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开发者_JAVA百科'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.


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.


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.


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++.


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.


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.


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\

0

精彩评论

暂无评论...
验证码 换一张
取 消