objective-c - OC中如何一行一行读一个文本文件
黄舟
黄舟 2017-04-22 09:00:06
[iOS讨论组]

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

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

文本就像上面那样的

还没学到iOS,只是命令行

黄舟
黄舟

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

全部回复(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];
}
PHP中文网

用NSFileHandle 来找到一个文件,然后用NSData来读取文本全部内容,然后用将NSData转化为NSString,再将nsstring用'\n'分开放倒一个NSAarray中去,这样就可以循环遍历了.

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号