Simple fopen and fprintf not working

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.


If you do not have admin privileges

Create file in you compilation directory


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:\


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.


  • win32 C/C++ read data from a “locked” file
  • how to load twitter data from hdfs using pig?
  • SQL Server 2008 generate script wizard gives me a script that results in “unclosed quotation marks”
  • Making bash script to check connectivity and change connection if necessary. Help me improve it?
  • Does the program counter always have to change (upon a clock tick)?
  • Merging Users in Kinvey
  • How do CL commands build their exact parameter lists?
  • Play youtube video in offline mode?
  • TextPad “find in files” not matching on simple OR regex
  • JSONPath Syntax when dot in key
  • Compiling Haskell programs in Windows: is it possible without downloading something such as Cygwin?
  • Get or convert Week of year to ISO week
  • Why is my req.body always empty on POST?
  • How to distribute Java-based software?
  • SSRS 2008 - Sorting within a group
  • not able to create VC++ project, with VS11
  • Visual studio 2015 keystroke with mouse button
  • Where these are stored?
  • How to pass solution folder as parameter in command line arguments (for debug)?
  • powershell Get-Counter -ComputerName parameter on Windows 7
  • Xamarin MonoAndroid Azure mobile service InsertAsync
  • Possible to “watch” both HAML and SASS at the same time?
  • Private IP address in reserved subnet range
  • How To Customize ASP.NET Chart Databound To SqlDataSource
  • ASP.NET MVC 2 Preview 2 - display directory list rather than home/index
  • PHP buffered output depending on server setting?
  • Master page gives error
  • jQuery show() function is not executed in Safari if submit handler returns true
  • javascript inside java/jsp code
  • WinForms: two way TextBox problem
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • Matrix multiplication with MKL
  • How to disable jQuery.jplayer autoplay?
  • Busy indicator not showing up in wpf window [duplicate]
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?