Link to home
Start Free TrialLog in
Avatar of smithdale87
smithdale87

asked on

File I/O

I'm having trouble reading a text file when I use it's relative path name instead of the absolute path.

(See code Below)

I'm not sure why this is not working, but it is the most annoying thing ever!
What am I doing wrong?
// does not work
NSString *fileContents = [NSString stringWithContentsOfFile:@"myFile.txt" encoding:NSUTF8StringEncoding error:NULL];  
 
 
//works
NSString *fileContents = [NSString stringWithContentsOfFile:@"/Users/myProfile/Desktop/myFile.txt" encoding:NSUTF8StringEncoding error:NULL];

Open in new window

SOLUTION
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smithdale87
smithdale87

ASKER

Nope, still nothing.

The odd thing is that all my images are loaded with relative path names, and they are all in the same directory as the text file.

For instance the following works correctly
UIImage *image = [UIImage imageNamed:@"myImage.png"];

Also something I just tried...

//does not work
UIImage *image = [UIImage imageWithContentsOfFile:@"myImage.png"];


//works
UIImage *image = [UIImage imageNamed:@"myImage.png"];
I tried your program like this:


System GNUstep and here's the output

 ./test
2008-07-20 10:03:10.332 test[7043] this is t1.txt

That's correct.

So I can not tell what may go wrong on your machine.
#include <stdio.h>
#include <Foundation/Foundation.h>
 
int
main(int argc, const char *argv[])
{
  NSAutoreleasePool *pool = [NSAutoreleasePool new]; //  init];
 
  NSString *fileContents = [NSString stringWithContentsOfFile:@"t1.txt"];
  NSLog(fileContents);
 
  [pool release];
 
  return 0;
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial