简体   繁体   English

Mac OS X-在文件中获取行尾

[英]Mac OS X - get end of line in file

I try to open and read a file on Mac OS X: 我尝试在Mac OS X上打开并读取文件:

FILE * pFile = fopen (path_.c_str() , "r");
if (pFile == NULL)
{
   perror ("Error opening file");
}
else
{
   char line [buffer_size]={0};
   size_t length=0;
   std::vector<unsigned> nodes;
   while ((line[length++] = fgetc (pFile)) != EOF )
   {
      printf("%d",line[length-1]);
      if (line[length-1] == '\r'||line[length-1] == '\n'
#ifdef __APPLE__
    ||line[length-1] == '$'
#endif
      )
{
   line[length-1]=0;
   unsigned number = 0;
   for(unsigned i=0;i<length;++i)
   {
      if(isdigit(line[i]))
   {
      number = number *10+line[i]-'0';
   }
else
   {
      nodes.push_back(number);
      number = 0;
   }
}
      memset(&line,0,buffer_size);
      length=0;
}
}
fclose (pFile);

I use #ifdef __APPLE__ define, this code work perfectly on Linux, but on Mac OS XI now must read some symbol which defines end of line for my file/line. 我使用#ifdef __APPLE__ define,此代码在Linux上运行完美,但是在Mac OS XI上现在必须读取一些符号,该符号定义了文件/行的行尾。 How can I get which symbol finished file, without this trick? 没有这个技巧,如何获得哪个符号完成文件?

Do it all in C++: 在C ++中完成所有操作:

unsigned number;
std::vector<unsigned> nodes;
std::ifstream file(path_.c_str());
while (file >> number)
    nodes.push_back(number);

As you can see it here , OSX new line character is now \\n , don't worry about that. 正如你可以看到它在这里 ,OSX换行符现在\\n ,不用担心。

So, you don't have to make any Apple specific code, except if you want to support a Mac OS version <= 9 . 因此,除非您要支持Mac OS版本<= 9 ,否则无需编写任何Apple特定代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM