0

i am trying to output a buffer to a file using visual c++. my code for doing it is-

FILE *stream;

stream=fopen("C:\\Users\\sshekha\\Desktop\\z.txt","r");

//I also tried with "w" mode
//the differencein behavious on executing these two is that when it is in read mode it 
//executes the else condition in the code below whereas in "w" mode it executes the "if"
// condition, 
//moreover even if i change the path it don't execute the "else" condition-that means this path
//is effective to the code. and  the another surprising thing is when i open the file manually
// and then run the code with "r" mode it still executes the "else" part (which it shouldn't
// because the file is already open.) 

if( stream == 0 )
{
    MessageBox(m_hwndPreview,L" the file is not opened ",L"BTN WND",MB_ICONINFORMATION);
}
else
{
    MessageBox(m_hwndPreview,L" the file is opened ",L"BTN WND",MB_ICONINFORMATION);
    int check=fputs (HtmlFileContents,stream);
    fclose(stream);
    return 0;
}

I tried to check the results using different mode in order to understand whats teh probem going on . when i debug it , i get the value of (in Read mode) :

stream = 0x000000005c5c76f0 { _ptr=0x0000000000000000 _cnt=0 _base=0x0000000000000000 ...}

I don't know it gib=ves bad pointer and even then it go to else part of the loop. Why ?

and in write mode

stream = 0x0000000000000000 {_ptr=??? _cnt=??? _base=??? ...}

So go to the if part of the loop.

Moreover my path is correct and i have enough permission to do the task I wish. But why does it give bad pointer ? Why have I these strange values of stream and what should I do to copy the content of my buffer HtmlFileContents in to z.txt ? Any ideas ?

JBL
  • 12,588
  • 4
  • 53
  • 84
Sss
  • 1,519
  • 8
  • 37
  • 67
  • You're opening the file in read-only mode and writing to it. That's your problem. – Vite Falcon Jul 25 '13 at 07:21
  • No i done that just to explain you the behaviour in different modes(w/r). in "w" mode it go to the if loop (i mean stream returns zero) whereas in "r" mode it go to second loop that means the stream has some contents but it has some you can see above . why it do so ?? and why the control don't go to else condition when the mode is "w" ?? – Sss Jul 25 '13 at 07:28
  • "r" is definitely the wrong mode. Have you tried "w+"? – Vite Falcon Jul 25 '13 at 07:30
  • Vite i hqve done it ..the control go to "if" condition when i do this. i mean stream returns zero in this case. – Sss Jul 25 '13 at 07:33
  • Can you also let us know which compiler you're using? I'm assuming Visual C++ but which version? – Vite Falcon Jul 25 '13 at 07:34
  • visual c++ 2010 . i don't know which compiler it contains i am working first time on visual studio but its not a consolle application its a Mfc appliucation. If still you want to know then could you please tellme how to check the compiler in visual studio ?? – Sss Jul 25 '13 at 07:37
  • Please try the suggestion in my answer. – Vite Falcon Jul 25 '13 at 07:37
  • sorry couldn't understand you?????which suggestion ??? i am using visual studio 2010 . and i am trying to do my task since last morning and still not able to find the problem . i feel thayt the code is rigth there is some other problem. – Sss Jul 25 '13 at 07:40
  • Use `perror` to print the cause of error or `strerror()` to retrieve the error message and display it using `MessageBoxA(m_hwndPreview,"Failed to open file because...", errorCause);`. See my edits in my answer on how to use `perror` and `strerror`. – Vite Falcon Jul 25 '13 at 07:44
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34136/discussion-between-vite-falcon-and-shekhar-singh-shekhawat) – Vite Falcon Jul 25 '13 at 07:46

2 Answers2

0

You're opening the file in read-only mode: fopen("C:\\Users\\sshekha\\Desktop\\z.txt","r");. Here "r" says you only intend to read from file. To be able to write contents (i.e. fputs(...)), open the file in write mode like so: fopen("C:\Users\sshekha\Desktop\z.txt","w")(or"a"` if you want to append). For more information, read fopen documentation.

EDIT: I see that you've tried both read and write modes. You're code only shows read-mode and hence my assumption on the read-only problem. Let me do a bit more research and get back.

Please write the following code in your if statement:

perror("The following error occurred:");

if you don't have a console, use this to store the error string:

char* errorCause = strerror(errno); MessageBoxA(m_hwndPreview, errorCause, "BTN WND", MB_ICONINFORMATION);

and let us know what you see as the cause.

EDIT 2: Since you've mentioned that you're using Visual Studio 2010, are you running it as yourself? This stackoverflow answer shows that VS2010 has different options when debugging applications; https://stackoverflow.com/a/3704942/210634

NOTE: That feature is only available on 'Pro' versions.

Here's a working example: https://ideone.com/hVLgc4

Community
  • 1
  • 1
Vite Falcon
  • 6,575
  • 3
  • 30
  • 48
  • i think you haven't read the question properly please see the lines "whereas in "w" mode it executes the "if" condition,". – Sss Jul 25 '13 at 07:30
  • 1
    @ShekharSinghSHEKHAWAT: Yup. I have that part on my 'EDIT' section. – Vite Falcon Jul 25 '13 at 07:31
0

If the file is "read only", opening with it with write permissions should fail.

to see if that is the case,under windows:

  • right click the file
  • press properties
  • at the bottom, see if "Read-only" attribute i marked with "v" (uncheck it if you desire writing to the file)

refer to :

http://msdn.microsoft.com/en-us/library/aa365535(v=vs.85).aspx

on how to change file permissions from your code

Rayee Roded
  • 2,440
  • 1
  • 20
  • 21