The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Using fopen on C disk
Discuss Using fopen on C disk in the C Programming forum on Dev Shed. Using fopen on C disk C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 20th, 2012, 12:18 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 14
Time spent in forums: 2 h 56 m 35 sec
Reputation Power: 0
|
|
|
Using fopen on C disk
A somewhat general problem, but here it is:
I tried to use fopen to create and write a file on the C disk (in the root directory), but it failed. Then, using the same program, I was able to write to a folder in C. Why is this (I have administrator rights), and how can I create a file under the C disk directly?
|

October 20th, 2012, 01:27 AM
|
 |
Contributed User
|
|
|
|
Well I suppose the first question is why you would want to be writing anything into C:\ to begin with.
Because if you screw it up, the next question you'll be asking yourself is "WTF are my OS installation disks?".
Next, you should provide an actual program, and actual error messages.
Code:
#include<stdio.h>
#include<stdlib.h>
#include <errno.h>
#include <string.h>
int main(void)
{
FILE *fp = fopen("readonly.txt","w");
if ( fp == NULL ) {
fprintf(stderr,"Unable to open:%s\n", strerror(errno));
return 1;
}
fclose(fp);
return 0;
}
$ gcc foo.c
$ ./a.out
Unable to open:Permission denied
Because "I tried something and it didn't work" is not a useful diagnostic we can help you with.
|

October 20th, 2012, 01:40 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 14
Time spent in forums: 2 h 56 m 35 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem Well I suppose the first question is why you would want to be writing anything into C:\ to begin with.
Because if you screw it up, the next question you'll be asking yourself is "WTF are my OS installation disks?".
Next, you should provide an actual program, and actual error messages.
Code:
#include<stdio.h>
#include<stdlib.h>
#include <errno.h>
#include <string.h>
int main(void)
{
FILE *fp = fopen("C:\\readonly.txt","w");
if ( fp == NULL ) {
fprintf(stderr,"Unable to open:%s\n", strerror(errno));
return 1;
}
fclose(fp);
return 0;
}
$ gcc foo.c
$ ./a.out
Unable to open:Permission denied
Because "I tried something and it didn't work" is not a useful diagnostic we can help you with. |
Thanks for the reply. Running your program, I do get the same error. I was just wondering why there is write-protection on the C: disk when (at least some of the folders) within it are not protected. Is there any way of bypassing or removing this protection?
It just happens that my hard disk was not partitioned into C: and D: disks when I got my laptop (and I didn't do it straight-away><), and pretty soon it became full enough to the point that any partitioning would be awkward.
|

October 20th, 2012, 04:56 PM
|
 |
Lord of the Dance
|
|
|
|
|
The problem is not that you only have the c: disk, the "issue" is why you want to write to the root of the drive. There are no need for files to be saved in that place other than the standard system files.
You didn't specify which OS you are using, but assuming Windows 7 there are several environment variables that can (should?!) be used to save files into, like %APPDATA% and %TEMP%.
You can get the whole list at:
http://technet.microsoft.com/en-us/library/dd560744%28v=ws.10%29.aspx
If you are not using this OS, I will still be surprised if your OS doesn't have similar option.
Last edited by MrFujin : October 20th, 2012 at 04:59 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|