There is a valid file with the name level1.lvl line in it:

10;24;cube1 14;55;cube2 итд... 

It is required to read this file and fill in 3 arrays: This is what you must do to contain db1-3 arrays after reading 1 line:

db1 [0] = 10;
db2 [0] = 23;
db3 [0] = cube1;

I am engaged in Objective-C for a couple of days, I can’t catch up on how to implement this.

Supplemented.

Right?

 NSString* fileName = @"filename.txt"; NSString *fileString = [NSString stringWithContentsOfFile: fileName]; NSArray *lines = [fileString componentsSeparatedByString:@"\n"]; NSUInteger count = [lines count]; NSUInteger i; for(i = 0; i < count; i++) { NSArray *readed = nil; NSString *element = [lines objectAtIndex:i]; NSArray *readed = [element componentsSeparatedByString: @";"]; [block_x objectAtIndex:i] = [readed objectAtIndex:0]; [block_y objectAtIndex:i] = [readed objectAtIndex:1]; [block_type objectAtIndex:i] = [readed objectAtIndex:2]; } 
  • Sorry for the stupid question (advice), and fgets () and sscanf () in Objective-C will work? - avp pm
  • will be - ArtFeel
  • Then read the file line by line fgets (buf, sizeof (buf), File) and select the data rc = sscanf (buf, "% d;% d;% s", & db1 [i], & db2 [i], str). db3 [i] = strdup (str); If rc == 3 OK, otherwise - an error in the format. The fgets () / sscanf () pair is used to reliably and simplify error analysis in the input format in order to avoid looping on fscanf () when it is trivially applied. - avp

2 answers 2

I now have no opportunity to jot down and check the code, so I’ll just state how I would try to solve this problem:

  1. Would read the file using [NSString stringWithContentsOfFile: encoding: error:];
  2. would get an array of strings like 10; 24; cube1 using componentsSeparatedByString: (NSString *) separator using string breaks as separator
  3. looping through the array would break each line into substrings with the same function, the separator ";" and would stuff these substrings into arrays (NSMutableArray)

    Not. Firstly, you will not read the file like that. Before it you need to specify the path:

      //если лежит в документах проекта NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *storePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"filename.txt"]; //если лежит в бандле NSString *storePath = [[NSBundle mainBundle] pathForResource:@"db" ofType:NULL]; 

    Secondly, direct assignment does not work [block_x objectAtIndex: i] = [readed objectAtIndex: 0]; , this is not a sishic array, rather something of type vector from c ++, should be [block_x addObject: [readed objectAtIndex: 0]]; (adds to the end of the array, there is a variant of the function and indicating where to add it, but it does not rewrite the value under the index, but shifts it by one position). In addition, to read the file, you must specify the encoding and you can replace the piece

     NSUInteger i; for(i = 0; i < count; i++) { NSArray *readed = nil; NSString *element = [lines objectAtIndex:i]; NSArray *readed = [element componentsSeparatedByString: @";"]; 

    on

     for(NSString *element in lines) { NSArray *readed = [element componentsSeparatedByString: @";"];