3

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.

Below is the warning that is displayed in 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'

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;
}

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.

Please help regarding the same.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
sam
  • 289
  • 5
  • 16
  • 1
    You are working in `C:` do you have admin privilege? – Grijesh Chauhan Oct 12 '13 at 05:46
  • @GrijeshChauhan:No i dont have admin privilages. I'll try with D: – sam Oct 12 '13 at 05:49
  • Are you creating some kind of weird Windows project? I find it hard to believe that even Microsoft would just try to deprecate standard C like that. – Crowman Oct 12 '13 at 05:52
  • @PaulGriffiths: i'm creating an usual simple empty, console window project. – sam Oct 12 '13 at 05:53
  • If you use Windows 7 or newer, this might be a permission problem (or even some kind of virtual environment). – urzeit Oct 12 '13 at 05:56
  • 1
    You should move `fclose(fp);` into the else branch. Calling `fclose` with a null pointer results in undefined behavior. – fredoverflow Oct 12 '13 at 06:33
  • @PaulGriffiths: Microsoft inspired [TR24731](http://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions) which became Annex K in the C11 standard. Yes, it really does deprecate a whole raft of standard C library functions, some with better cause than `fopen()` — `strcpy()` etc are in the list too. You have to do something like `#define _CRT_SECURE_NO_WARNINGS 1` to avoid the warnings (or change the code to use the 'preferred' `*_s()` variants of the functions). – Jonathan Leffler Oct 12 '13 at 14:10
  • @JonathanLeffler: I'm horrified. – Crowman Oct 12 '13 at 14:55
  • @PaulGriffiths. Fair enough. There are some advantages to some of the TR24731 functions, and using them helped Microsoft reduce the bugginess of their software. As noted in my answer to my own question about TR24731, I have reservations about some of the functions, and the incompatibilities between Microsoft's version and the standard's version of some other functions renders them useless to me (the only reason to use them is for compatibility between MS and non-MS, but they're incompatible anyway). I've lived with the horror long enough that the pain is dulled. – Jonathan Leffler Oct 12 '13 at 15:05

2 Answers2

3

If you do not have admin privileges

Create file in you compilation directory

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

This will create file in mysample\mysample

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


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

if you use filename fopen.c, you will get fopen.exe

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

Gangadhar
  • 10,248
  • 3
  • 31
  • 50
1

Your code works, but if you want create the file in C:\ you need Admin Privileges, try saving the file in the same file directory of your program, or run an Administrator cmd and execute your *.exe there.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
JuanCrg90
  • 1,006
  • 13
  • 24