如何使用相对路径使用Xcode在C中打开文件? [英] How to use relative path to open files in C using xcode?

查看:85
本文介绍了如何使用相对路径使用Xcode在C中打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用C语言打决赛.用户应输入他要播放的地图文件.

I need to make a game in C for my finals. The user should input the map file he wants to play.

这是我的简单代码:

int main(){

FILE *map;

char fileToRead[100];

do{
    printf("Insert file name: ");
    fgets(fileToRead, 100, stdin);

    map = fopen("/Users/rajunior/Desktop/map_2.txt", "r");
    //map = fopen(fileToRead, "r");
    printf("%s", fileToRead);

如果我使用硬编码的" map = fopen("/Users/rajunior ...)",则可以使用!但是我需要使用第二个(注释)选项;第一个对我而言毫无用处.

If I use the "map = fopen("/Users/rajunior...)" hardcoded, it works! But I need to use the second (commented) option; the first one is useless for my purpose.

换句话说,我需要将 fileToRead 与.c放在同一目录中,但是如何?

In other words, I need the fileToRead to be in the same directory as my .c, but how?

屏幕截图: https://imgur.com/a/DbX9tw4

推荐答案

选项1:安装命令行工具.将C文件和文本文件放在同一目录中.打开一个终端窗口.从命令行编译并运行.如果我没记错的话,可以在 Preferences .../Downloads 中找到命令行工具下载.

Option 1: Install the command line tools. Put the C file and the text file in the same directory. Open a terminal window. Compile and run from the command line. If I recall correctly, the command line tools download can be found in Preferences.../Downloads.

选项2:转到 Product/Scheme/Edit Scheme ... 菜单.出现对话框时,在左侧选择 Run ,在顶部选择 Options .然后查找工作目录.将工作目录设置为指向文本文件所在的目录.

Option 2: Go to the Product/Scheme/Edit Scheme... menu. When the dialog box appears, select Run at the left and Options at the top. Then look for Working Directory. Set the working directory to point to the directory where the text file is.

这篇关于如何使用相对路径使用Xcode在C中打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆