Problem with Xcode, reading files.

Hello guys, it's My first post here and i hope it will be usefull. I study informatic engineering and so i'm learning to programming, i use xcode and i've got a problem: everything works Good but when i ask to search for a file that i created it gives me an error, Like The file has not been created or like xcode can't read it. I also checkede on internet and i saw that a lot of people have got My same problem. I hope someone can help me please.

Here is how I write to a file:

//   NSDictionary *myParameters=   make  an NSDictionary that you wish to save or just point this to your dictionary
    NSString *error=@"";
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"MyFileName"];
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:myParameters format:NSPropertyListXMLFormat_v1_0 errorDescription:&error ];

    if(plistData){
         if( [plistData writeToFile:plistPath atomically:YES]){
             // do something here because the file was correctly written
         }else{
             //    error that file was not correctly written to the device
    }else{
          // error here that the dictionary could not be tyrned into a plist object
   }
  


and I read it this way:


        NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                               NSUserDomainMask, YES) objectAtIndex:0];
        NSString *plistPath = [rootPath stringByAppendingPathComponent:@"MyFileName"];
        if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
            NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
            NSString *errorDesc = nil;
            NSPropertyListFormat format;
            NSMutableDictionary *tempReceipts =
                (NSMutableDictionary *)[NSPropertyListSerialization
                          propertyListFromData:plistXML
                          mutabilityOption:NSPropertyListMutableContainersAndLeaves
                          format:&format
                          errorDescription:&errorDesc];
            if(tempReceipts){
                 [myParameters setDictionary:tempReceipts]; //  or some other way of recovering the file
             }else{
                 // handle error - unable to reconstruct the dictionary
              }
        }else{
             //  handle the error  - unable to find the  file  
         }

I forgot to say that i'm studying The c++ and our teacher said us a easier Way, why should't it work on Xcode too?

#include #include Using namespace std; Typedef int list[50]; Int main(){ list l, int riemp,int i; Ifstream input; Input.open("filename.txt"); If(input.fail()) Exit(1); i=0; While(!input.eof()){ Input>>l [i]; I++; } Riemp=i;

Since you aren't providing the path to "filename.txt" of course it cannot find it. When you run

something from xCode is runs under the build folder for the application you're building. If your

file isn't in that folder, it would never be found without giving the full path to its location.

I did it from the options but it doesn't change nothing..

with other programs like "dev c++" you only create the file in the folder and it finds it.

Can someone link me a video or a guide where i can see how to do it?

You haven't constructed any path search into your code so, the only way the file could

ever be found is if you give it the full path in input.open().

Assuming the file is in the users documents folder then the path would be

~/Users/username/Documents/filename.txt

Can't i do it Through the xcode's options? is it something that must be done just in xcode?

No, those options are for building not runtime. They aren't passed to your app in main().

You could supply the path in main(argc,argv[]) with command line arguments in xCode.

But that would require you to parse the passed arguments to obtain the path.

So should i just write in the command line?

If the answer is yes can you tell me what should i write if the file is in the desktop in the folders of xcode?

Thank you so much.

You could pass ~/Desktop as the path, this should give you the Desktop folder relative

to the current user.

Problem with Xcode, reading files.
 
 
Q