简体   繁体   English

在C ++中使用OSX目录需要帮助

[英]Need help using OSX directories in c++

I am currently trying to output the contents of the hosts file and eventually once I figure this part out, I will add entries and delete them later (which is why I am using fstream (I think?)) I am not extremely versed in programming and need some direction as I have never programmed for OSX before. 我目前正在尝试输出hosts文件的内容,最终,当我弄清这部分内容后,我将添加条目并在以后将其删除(这就是为什么我使用fstream(我认为?)的原因)。我并不精通编程并且需要一些指导,因为我以前从未为OSX编程过。

My program seems to not be able to navigate to the directory and returns with "Unable to open file" when I run it. 我的程序似乎无法导航到该目录,并且在运行该程序时返回“无法打开文件”。 How can I specify the correct directory? 如何指定正确的目录? I am used to Windows where you can specify C:, D:, E; 我习惯于Windows,可以在其中指定C:,D:,E; drives. 驱动器。

fstream f ("/private/etc/hosts");
        if (f.is_open())
        {
            while (f.good())
            {
                getline (f,line);
                cout << line << endl;
            }
            f.close();
        } else
            cout << "Unable to open file" << endl;

Backslash \\ is Windows-specific directory separator. 反斜杠\\是Windows特定的目录分隔符。 Use proper slash / . 使用适当的斜杠/

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

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