1

In an MFC application I have a file browse button. When I click it and select a file, the file is loaded and things are fine.

After this I have a function, which attempts to open another file using fopen(), but can not open the file although file is present. If I select cancel in the file browse dialog, fopen() works fine but as soon as I select a file using file browse dialog fopen does not work.

Can someone tell why this is happening.

6
  • 2
    Can you share the code please. It is hard to debug a question and a lot easier to read code. Commented Sep 20, 2012 at 11:36
  • Unrelated, but I'm curious about why are you using fopen instead of fstream? Commented Sep 20, 2012 at 11:42
  • could be something as simple as a permissions issue... check the errno for clues Commented Sep 20, 2012 at 11:52
  • I only had to check if file is present or not so i was doing FilePopinter = fopen("filename"); if(FilePopinter ){...}else{...} but it was not working. I tried using ofstream::good() as Joachim Pileborg said and its working fine i dont know how :| Anyways Thankyou everyone . Commented Sep 20, 2012 at 12:21
  • What is your second argument to fopen()? Because it normally takes 2 of them (one for filename, one for mode) Commented Sep 20, 2012 at 13:00

1 Answer 1

2

Maybe you specify second file by relative path and after opening first file using dialog, working directory is changed. When you press Cancel then working directory is not changed and it works.

Solution to this issue is e.g. to specify this second file using full, non-relative path.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried using non-relative path but it dint work. Instead i used fsteam::good to check if file is present or not and its working fine. I dont know what was wrong with fopen but fstream works fine. Anyways Thanks Psur.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.