1,105,644 Community Members

File not created with fopen and relative reference syntax not working

Member Avatar
vincenzorm117
Newbie Poster
23 posts since Feb 2010
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey very simple problem. I've been searching the site to see if I did anything wrong and I had to fail because I still get the wrong results (semantic error). Problem #1: Every time I use fopen with the "w" (or write) mode the file with whatever name given is not created which brings us to the second problem. Problem #2: Correct me if I am wrong (syntax might of changed due to updates), but as I know according to Dave Mark's "Learn C on the Mac" he specifies as filename paths as the following:

All of the following are the same:
Location from root: /Users/davemark/test/myFile.text
Location from source code file: ./myFile.text meaning myFile.text is in the same folder as the source code file
or: ../myFile.text meaning myFile.text is in the parent directory from the source code file
Location from home directory: ~/test/myFile.text


I think that problem number #1 is a result of problem #2 and it is because every time I use fopen I specify the filename path from the source code file. I don't want to use the location from root every time so heres my code, what am I doing wrong?:

#include <stdio.h> 
#include <string.h>

#define kFilenameSize   256




int main(int argc,char ** argv) 
{ 
    char filename[kFilenameSize]="The file is named qwerty"; 
    FILE *userfile = 0; 
    
    
    userfile = fopen("./qwerty.text", "w"); 
    
    fputs(filename, userfile);
   
    fclose(userfile);
   
}
Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

How about simple "qwerty.txt", without path, shouldn't it mean the same? What error message are you getting? You should check the return value in variable userfile for the error.

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

1) until you completely understand how paths work in relation to your IDE vs Program, use the full path.
2) You don't check to see if the file was opened. It probably was, just not where you expected. Always error check after anything that might not work.

Member Avatar
vincenzorm117
Newbie Poster
23 posts since Feb 2010
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I did try just the name but I think WaltP is right: I have to use the full path; also I figured out why the path wasn't working. Dave Mark specifies that the different filename paths in reference to the file are limited to the unix and mac os x compiler (via terminal) so xcode doesn't ever read the filename as such. Also WaltP I did check to see if it was open and there was an address assigned the file (*userfile). Thank you all

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: