C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 20th, 2012, 12:18 AM
electriceel electriceel is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 14 electriceel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old October 20th, 2012, 01:27 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,835 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 15 h 19 m 43 sec
Reputation Power: 1774
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old October 20th, 2012, 01:40 AM
electriceel electriceel is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 14 electriceel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old October 20th, 2012, 04:56 PM
MrFujin's Avatar
MrFujin MrFujin is online now
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 22 h 35 m 43 sec
Reputation Power: 1736
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Using fopen on C disk

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap