Im using xcode 4.6 and im having problems reading files using the fopen function in c
Im using xcode 4.6 and im having problems reading files using the fopen function in c 😢
MacBook Pro (13-inch, Mid 2012), OS X Mountain Lion (10.8.2)
Im using xcode 4.6 and im having problems reading files using the fopen function in c 😢
MacBook Pro (13-inch, Mid 2012), OS X Mountain Lion (10.8.2)
What problems?
So for example web web I run running a code such as
FILE *fp;
Fp=fopen("hello_world.txt","r");
It always gives me the message "build failed"
Just to clear a couple of things ,the txt file is located in the same directory as main.c
And I use fclose at the end of the program.
I've tried running the code on terminal and different IDE's such as eclipse and its working perfectly .
Just to clear a couple of things ,the txt file is located in the same directory as main.c
That is most likely your problem. The location of the source files is not the same as the location of the compled code. Xcode places the compiled code in a different directory from the source.
It is always a good idea to either use the absolute path for files your program will open or to use a relative path you can construct in your program. For example putting the file in your home directory or some other well know place.
Yes, but that would not cause the "build failed" message.
Are you including stdio.h?
Can you post your whole program?
Are you checking fopen() for a NULL result in case it fails?
Yes, but that would not cause the "build failed" message.
right I missed that part. Looks like two problems then.
//
// main.m
// ASCTestFopen
//
// Created by Frank Caggiano on 3/2/13.
// Copyright (c) 2013 Frank Caggiano. All rights reserved.
//
#import <Foundation/Foundation.h>
#include <stdio.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
FILE *fp, *fopen();
// insert code here...
if((fp = fopen("~/test.txt","r")) == NULL)
NSLog(@"open failed");
}
return0;
}
This will compile ad run in Xcode with a project type of comand line tool
So for example web web I run running a code such as
FILE *fp;
Fp=fopen("hello_world.txt","r");
It always gives me the message "build failed"
Try File *fp = fopen("hello_world.txt","r");
if((fp = fopen("~/test.txt","r")) == NULL)
fopen doesn't understand ~ (twidle) - that is a shell feature for the users home directory and will not get translated by the fopen call.
open doesn't understand ~ (twidle) - that is a shell feature for the users home directory and will not get translated by the fopen call.
Right good catch. Get so use to typing that my fingers were ahead of my brain.
thanks
Im using xcode 4.6 and im having problems reading files using the fopen function in c