Problem with reading bundle text file

Apprentice
Posts: 6
Joined: 2010.01
Post: #1
Hi all,

I have some text files contain level data, i'm able to get the path of bundle file, but i couldn't success to read it in both objective-c or c++ ways. Cry

Here is my code:

Code:
- (void) loadLevel: (NSString*) levelFilename
{
    NSString *filePath = [[[NSBundle mainBundle] resourcePath]
                               stringByAppendingPathComponent:levelFilename];
    const char *filename = [filePath UTF8String];
    map->loadMap(filename, blocks);

        /***** I'm new to objective-c, i copied this code from apple document *****
         NSString *myText = [NSString stringWithContentsOfFile:filePath];
     NSError *error;
     NSString *myText = [[NSString alloc] initWithContentsOfFile:filePath                                                            
                                                        encoding:NSUTF8StringEncoding              
                            error:&error];
     NSLog(@"%@", myText);  // it shows NULL here...
        ***************************************************************************​*/
}

// map.mm
void Map::loadMap(const char *filename, bool blocks[16][16])
{
    cout << "Filename: " << filename << endl;
    ifstream infile(filename);    
    if(infile.fail())
    {
        cout << "Error: " << filename << " can't open!" << endl;
        exit(0);
    }
    
    string test;
    while(getline(infile, test))
    {
        cout << test << endl;
    }
    infile.close();
}
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
Since you know where the problem is, inspect the file path you're getting back and check to see if your file is in that location.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2010.01
Post: #3
Thanks for replied, i just checked the .app file, and my text file isn't there.
I added the text file in Resource folder already, how come it didn't bundle with the app?

Do i have to set anything?
Quote this message in a reply
⌘-R in Chief
Posts: 1,261
Joined: 2002.05
Post: #4
Check if it's actually in the Copy Resources build phase in the app Target.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2010.01
Post: #5
i deleted the files and recopy them few times, and finally the problem is solved.

Thanks for all the replies!!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Reading a ".puz" file tcIgnatius 0 3,609 Jul 5, 2010 02:49 PM
Last Post: tcIgnatius
  Bundle Identifier used by another app Bersaelor 3 3,779 May 7, 2010 01:01 PM
Last Post: Bersaelor
  Save game state using Property list or Text File? Graphic Ace 2 3,688 Apr 6, 2009 03:48 AM
Last Post: Graphic Ace