Re: fopen
Re: fopen
- Subject: Re: fopen
- From: Thomas Wetmore <email@hidden>
- Date: Mon, 02 Jul 2012 15:11:46 -0400
The error message is not coming from the attempt to open the file, but most likely later when you try to use the file pointer to read the file. You should check the return from the call to fopen and if the value is null, which it almost surely is in this case, you can check the file system's error value to see what the problem is. Which will almost certainly be what you are assuming, that the file can't be found.
When you use fopen you either have to supply a full path or "know where you are" in the file system. Your file likely exists, but not where fopen is looking for it. You can run a system("pwd") command for the heck of it to see where your current directory is. It probably won't be the place you think the test file is.
There is nothing "automatic" going on that will help you. The program won't "find" the file and it won't "automatically write subsequent files in the same folder." You will have to figure out where you are in the file system, and where you want to read your files from, and where you want to write your files to, and you will have to correctly specify these paths.
Good luck,
Tom
On Jul 2, 2012, at 1: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.
>
> 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?
> _______________________________________________
> 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
_______________________________________________
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>) |