Re: fopen
Re: fopen
- Subject: Re: fopen
- From: Ken Thomases <email@hidden>
- Date: Mon, 02 Jul 2012 14:20:11 -0500
On Jul 2, 2012, at 12:11 PM, rckrueger wrote:
> I am just learning c, and this is a beginner's question. I am using Xcode 4 (Command Line tool) to create a program that will read a simple text file (a number 1234). When I use the code:
>
>
> #include <stdio.h>
>
> int main(int argc, const char * argv[])
> { FILE *inputfile; // a pointer to the input file
>
> inputfile = fopen("test.txt", "r");
>
> to find and open the test.txt file which I have in the Project folder, same level as the main.c file, I get the error message:
> Thread 1: EXC_BAD_ACCESS (code=1, address=0x68
> which I guess means that it can't find the file.
Sort of. If it can't find the file, fopen() returns a NULL file pointer. It won't crash just because of that. However, if you proceed to use the NULL file pointer, by passing it to fread(), that will dereference the pointer (which is invalid) and that causes the crash. Xcode should be showing you the exact line of the crash. It should not be the fopen() line.
The lesson here is that you should check for errors from the functions that you call.
> When I use this line to look for the file on the desktop:
> inputfile = fopen("/users/robertkruegertiger/desktop/test.txt", "r");
>
> the program works as expected.
>
> How do I get the program to find the test.txt file in the Project folder. I have already used the "Add Files to FileReadExample..", which copied it from the Desktop folder to the same folder as the main.c code. What more needs to be done? TIA
>
> postscript: And when the program finds the file and works properly, will it automatically write subsequent files in the same folder?
Paths are either absolute or relative. Absolute paths start with a slash and include all of the directories from the root of the file system to the file in question. ("Starting from this one well-known location, take these steps…") Relative paths don't start with a slash. They are interpreted relative to some previously established starting point. ("From where you are now, take these steps…")
You have specified a relative path. It has no directories in it, at all. So, it basically means the file named "test.txt" at the starting point.
Your starting point is the current working directory. In a shell, you change your current working directory using the "cd" command. You can change it programmatically using chdir() or fchdir().
When a command-line tool is invoked from the command line, the user will be aware of and in control of the current working directory. When your tool is invoked by Xcode for debugging, it gets an unpredictable current working directory. (It might be stable for a given version of Xcode, but it has changed throughout Xcode's history.) If you want the program to access a file relative to the user's current working directory, it's OK to use a relative path. If you need the program to access a very specific file, you have two problems: 1) installing that file along with your tool, and 2) figuring out where that file is.
With an application, this is easier because applications are bundles of files that are kept together. For command-line tools, you typically decide on an install location (such as /usr/local/bin and /usr/local/share) at build time, arrange for your installer to put the files there, and then compile knowledge of that location for your files into your program.
Regards,
Ken
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >fopen (From: rckrueger <email@hidden>) |