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

CSwift Programming

Avatar of undefined
Last Comment
smithdale87

8/22/2022 - Mon
SOLUTION
F. Dominicus

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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"];

smithdale87

ASKER
Also something I just tried...

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


//works
UIImage *image = [UIImage imageNamed:@"myImage.png"];
F. Dominicus

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
smithdale87

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.