简单的fopen和fprintf不工作 [英] Simple fopen and fprintf not working

查看:137
本文介绍了简单的fopen和fprintf不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序,它打开一个文件并将文本写入文件。但是,fopen总是返回 BadPtr ,如在 Microsoft Visual c ++,2010 中的调试模式。

I have a simple program which opens a file and write a text into the file. However, the fopen is always returning BadPtr as seen in debug mode in Microsoft Visual c++, 2010.

以下是 VS C ++,2010中显示的警告

mysample\mysample\main.c(6): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'

我认为警告是一个问题,并使用 fopen_s ,但这也不解决问题。下面是我的代码:

I thought the warning is a problem and used fopen_s, however this also doesnt solve the problem. Below is my code:

#include <stdio.h>

int main()
{
    FILE *fp;
    fp=fopen("c:\\hello.txt","w+");

    if(fp==NULL)
    {
        printf("\nfopen() error\n");
        return 1;
    }
    else
    {
        fprintf(fp,"\nThis is a sample text file\n");
    }

    fclose(fp);
    return 0;
}

在上面的代码中,流程不会输入 if(fp == NULL)条件,而是转到else部分,但是文件没有被创建。

In the above code, the flow doesn't enter the if(fp == NULL) condition, rather it goes to the else part, but the file is not being created.

请帮助解决这个问题。

推荐答案

有管理员权限

在编译目录中创建文件

fp=fopen("hello.txt","w+");

这将创建 mysample\ mysample

您应该具有管理员权限,以在 C:\

You should have admin privileges, to create file in C:\

如果您有管理员权限,请使用Run as Administator命令提示,然后只需输入名称即可执行您的可执行文件。

if you have admin privileges open command Prompt with Run as Administator and then execute your executable by just giving name.

如果你使用文件名 fopen.c
,你会得到 fopen.exe

只需在命令提示符下运行 fopen ,就会在 C:\

simply run fopen from command prompt, it will create file in C:\

这篇关于简单的fopen和fprintf不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆