BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / soft-design / #14480同步于 2007/1/12
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖

怎样读取文件中以\n为分界的字符串?

michaeldy
2007/1/12镜像同步3 回复
假设文件中存有字符串:this is a test1\n and this is a test2\n...... 用fscanf(stream,"%s\n",mystr);读取。 我想把从第一个字符到回车\n为止的this is a test1整个句子付给mystr,而实际结果是仅把this付给mystr. 程序该怎么写呢?最好不用sizeof()函数。 谢谢!
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
jeeper机器人#1 · 2007/1/12
strtok 【 在 michaeldy (michael) 的大作中提到: 】 : 假设文件中存有字符串:this is a test1\n and this is a test2\n...... : 用fscanf(stream,"%s\n",mystr);读取。 : 我想把从第一个字符到回车\n为止的this is a test1整个句子付给mystr,而实际结果是仅把this付给mystr. : ...................
afone机器人#2 · 2007/1/12
【 在 michaeldy 的大作中提到: 】 : 假设文件中存有字符串:this is a test1\n and this is a test2\n...... : 用fscanf(stream,"%s\n",mystr);读取。 : 我想把从第一个字符到回车\n为止的this is a test1整个句子付给mystr,而实际结果是仅把this付给mystr. : ................... 读取文件后的操作: char databuf[30]; while(!infile.eof()) { infile.getline (databuf,sizeof(databuf), '\n'); strcpy(temp, databuf); //temp存放要的字符串 ...... } } 上面用到了sizeof函数,但这是getline函数必须有的参数,lz参考参考
jeeper机器人#3 · 2007/1/13
从文件读,fgets就可以了 【 在 michaeldy (michael) 的大作中提到: 】 : 假设文件中存有字符串:this is a test1\n and this is a test2\n...... : 用fscanf(stream,"%s\n",mystr);读取。 : 我想把从第一个字符到回车\n为止的this is a test1整个句子付给mystr,而实际结果是仅把this付给mystr. : ...................