objective-c - OC中如何一行一行读一个文本文件
黄舟
黄舟 2017-04-22 09:00:06
0
2
635

每读出一行,用空格分割变成数组,然后将数组中的对应的内容放入到 Student 对象中

10001 王晓明 18
10001 王晓明 18
10001 王晓明 18
10001 王晓明 18
10001 王晓明 18
10001 王晓明 18

文本就像上面那样的

还没学到iOS,只是命令行

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
刘奇
char id_c[30], name_c[30];
int age;
NSString *id_c, *name_c;
Student *student;
file = fopen("Student.txt", "r");
while (!feof(file)) {
    fscanf(file, "%s %s %d ", id_c, name_c, &age);
    id = [NSString stringWithUTF8String:id_c];
    name = [NSString stringWithUTF8String:name_c];
    student = [[Student alloc] initWithId:id Name:name Age:age];
    [student print];
}
Ty80

Use NSFileHandle to find a file, then use NSData to read the entire text content, then use NSData to convert NSData to NSString, and then use 'n' to separate the nsstring into an NSAarray, so that you can loop through it.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template