文件处理中的流(指向文件结构的指针),给出奇怪的值 我试图用VisualC++将缓冲区输出到文件。 我的代码是- 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; }

文件处理中的流(指向文件结构的指针),给出奇怪的值 我试图用VisualC++将缓冲区输出到文件。 我的代码是- 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; },c++,filestream,fopen,file-handling,fputs,C++,Filestream,Fopen,File Handling,Fputs,我试着用不同的方式检查结果,以了解到底发生了什么问题。调试时,我得到的值为(在读取模式下): stream=0x000000005c5c76f0{{u ptr=0x0000000000000000{u cnt=0{u base=0x0000000000000000…} 我不知道它是gib=ves坏指针,即使它转到循环的其他部分。为什么? 和在写入模式下 stream=0x0000000000000000{{u ptr=???\u cnt=???\u base=???…} 因此,转到循环的if部分

我试着用不同的方式检查结果,以了解到底发生了什么问题。调试时,我得到的值为(在读取模式下):

stream=0x000000005c5c76f0{{u ptr=0x0000000000000000{u cnt=0{u base=0x0000000000000000…}

我不知道它是gib=ves坏指针,即使它转到循环的其他部分。为什么?

和在写入模式下

stream=0x0000000000000000{{u ptr=???\u cnt=???\u base=???…}

因此,转到循环的if部分


此外,我的道路是正确的,我有足够的权限来完成我想要的任务。但为什么它给出了错误的指针呢?为什么我有这些奇怪的流值,我应该怎么做才能将缓冲区的内容
HtmlFileContents
复制到
z.txt
?有什么想法吗?

您正在以只读模式打开文件:
fopen(“C:\\Users\\sshekha\\Desktop\\z.txt”,“r”)。这里
“r”
表示您只打算从文件中读取。要能够写入内容(即,
fputs(…)
),请按如下方式以写入模式打开文件:fopen(“C:\Users\sshekha\Desktop\z.txt”,“w”)
(或
“a”`如果要追加)。有关详细信息

编辑:我看到您尝试了读和写模式。您的代码只显示读取模式,因此我对只读问题的假设。让我再做一点调查,然后回来

请在您的
if
语句中填写以下代码:

perror(“发生以下错误:”)

如果没有控制台,请使用此命令存储错误字符串:

char*errorCause=strerror(errno);MessageBoxA(m_Hwnd预览,错误原因,“BTN WND”,MB_图标信息)

让我们知道你认为的原因

编辑2:既然您提到您正在使用VisualStudio2010,那么您是否正在以自己的身份运行它?这个stackoverflow答案表明VS2010在调试应用程序时有不同的选项

注意:该功能仅在“Pro”版本上可用

下面是一个工作示例:

如果文件是“只读”的,则使用写权限打开该文件将失败

要查看情况是否如此,请在windows下:

  • 右键单击该文件
  • 压力机性能
  • 在底部,查看我是否用“v”标记了“只读”属性 (如果希望写入文件,请取消选中该选项)
参考:


关于如何更改代码中的文件权限

您正在以只读模式打开文件并将其写入。这是你的问题。不,我这样做只是为了向你解释不同模式下的行为(w/r)。在“w”模式下,它转到if循环(我的意思是流返回零),而在“r”模式下,它转到第二个循环,这意味着流有一些内容,但它有一些您可以在上面看到的内容。为什么会这样呢??当模式为“w”时,为什么控件不转到其他状态?“r”肯定是错误的模式。你试过“w+”吗?我试过了。当我这样做的时候,控制转到“如果”状态。我的意思是,在这种情况下,流返回零。我认为您没有正确阅读问题,请查看“行”,而在“w”模式下,它执行“if”条件,@shekharingshekhawat:Yup。我的“编辑”部分有这个部分。