Re: fopen
Re: fopen
- Subject: Re: fopen
- From: Fritz Anderson <email@hidden>
- Date: Mon, 02 Jul 2012 14:10:53 -0500
On 2 Jul 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)
I misread this to mean you were using the command-line development tools, but now I understand you are using Xcode itself, and started from the Command Line Tool template.
> 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.
No, it meant that your program tried to use an illegal pointer, and crashed. Errors in fopen() (like missing files) cause it to return NULL. The code you present isn't complete. The error may be somewhere else. Perhaps you tried to use inputFile without bothering to see if it was valid? Are you looking at the debugger when you run your program?* Where does it say the crash occurred? What does the variables pane say — are all the values what you expect? Is inputFile NULL (0)?
* (Xcode is in the habit of spontaneously hiding either the console or the variables pane. Use the center part of the three-part control at the top-right of the Debugger area to show both.)
> 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.
This code finds your file only by coincidence. It happens that the HFS+ file system is case-insensitive. The real path is "/Users/robertkruegertiger/Desktop/test.txt", assuming the file name itself is all lower-case. On most other file systems, your all-lower-case path will not find /Users, much less your file.
> 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
Before you do anything else, you should familiarize yourself with the concept of the working directory. That is the directory your app will use if you specify just a file name and not the full path. Normally, the working directory Xcode sets (I think) is inside the derived data library, where your compiled app was placed. If you ask for "test.txt", your app will look for it in "/Users/robertkruegertiger/Library/(…etc…)/test.txt", and it won't find the file.
Use the Scheme editor to set the working directory to something else: Product > Edit Scheme…, Run action, Options tab, Use custom working directory.
> postscript: And when the program finds the file and works properly, will it automatically write subsequent files in the same folder?
If you give only filenames, and not fully-qualified paths, the program will attempt to write them into the working directory, whatever that might be.
— F
--
Fritz Anderson -- Xcode 4 Unleashed: Now in stores! -- <http://x4u.manoverboard.org/>
_______________________________________________
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>) |