Re: fopen
Re: fopen
- Subject: Re: fopen
- From: "Justin C. Walker" <email@hidden>
- Date: Mon, 02 Jul 2012 12:31:25 -0700
On Jul 2, 2012, at 10:11 , rckrueger wrote:
> I am just learning c, and this is a beginner's question.
There are better fora for this kind of question. Check out the newsgroup comp.lang.c (search for it with Google) for more information. This forum is primarily for the use of Xcode.
> 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.
It may mean that, but you can't be sure until you verify where the error occurred. Use a debugger, or put printf() calls in your code to narrow down where that happens.
What that exception really means, though, is that there is an error in your program. In this case, I would guess that you are using the return value from fopen() without verifying that it is valid for the use you have in mind. You should check the "man page" for fopen().
>
> 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
It sounds like you also need to learn a little about using Unix, and the Unix file system.
> postscript: And when the program finds the file and works properly, will it automatically write subsequent files in the same folder?
This is again a basic question about both C and Unix. Books abound for each, as do websites with documentation and fora for asking questions and getting answers about the basics.
Even your seemingly simple 'PS' question has an involved answer.
Justin
--
Justin C. Walker, Curmudgeon-At-Large
Institute for the Absorption of Federal Funds
--------
If you're not confused,
You're not paying attention
--------
_______________________________________________
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>) |