我安装了新版本的操作系统X(最初为10.7然后更新为10.7.5) - 我在终端丢失了人员fgets,它不再存在(不是很好的fgets,还有其他一些)。我正在使用xcode 4.6.3,更新了各种文档。在文档中我只得到了FGETS(3),而不是fgets! 当我写这段代码时:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
FILE *wordFile = fopen ("/tmp/words.txt", "r");
char word[100];
while (fgets(word, 100, wordFile))
{
word[strlen(word) - 1] = '\0'; // strip off the trailing \n
NSLog (@"%s is %lu characters long", word, strlen(word));
}
fclose (wordFile);
return 0;
}
我得到了输出:
Joe-Bob "Handyman" Brown
Jacksonville "Sly" Murphy
Shinara Bain
George "Guitar" Book is 84 characters long
为什么?
答案 0 :(得分:0)
我在@Martin R.的钱。
fgets()
未在文件“/tmp/words.txt”中找到已转换的\n
,即使它已在文本模式“t”中打开。用于创建/编辑文件的编辑器以\r
结束行。
见Do line endings differ between Windows and Linux?
中的@Michael Haren BTW:word [strlen(word) - 1] ='\ 0';虽然不是在这种情况下可能是一个问题,因为strlen(word)
可能是0.(@ wildplasser)