如何在Mac上的Code :: Blocks上设置工作目录? [英] How Do I Set the Working directory on Code::Blocks on Mac?

查看:113
本文介绍了如何在Mac上的Code :: Blocks上设置工作目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C编程,所以我是一个初学者.

I am learning C programming so I'm a total beginner.

我在Mac上使用Code :: Blocks 12.11,我已经获得了Xcode并下载了命令行程序包,因此一切似乎都可以按照我的意愿进行.

I am using Code::Blocks 12.11 on Mac, I have got Xcode and downloaded the command line packages so everything seems to be doing what I want it to.

我的问题是,当我打开.txt文件时,它没有出现在包含源代码和其他文件的目录中,我在Finder中的我的所有文件"文件夹中找到了它.

My problem is when I open a .txt file it doesn't appear in the directory with the source code and other files, I have found it in the All My File folder in Finder.

我正在使用以下步骤创建项目>文件>新建>项目>控制台应用程序>然后命名项目并将其保存到为所有程序创建的文件中.

I am creating projects using the following steps > File > New > Project > Console Application > Then naming the Project and saving it to a File I have created for all my programmes.

我以前的所有项目都保存在该目录中的纯文件夹中,.txt文件除外.

All my previous projects are saved in neat folders in this directory except the .txt file.

这是我的代码.

/**

Automatic Control and Systems Engineering126 - Problem Sheet 2.Q2
Project: Opening a Text File for Writting
Author: Craig McAllister
Date: 23.02.17

**/

#include <stdio.h>

main()
{
      FILE *myFile;
      myFile=fopen("first.txt","w"); // open first.txt for writing (or create it if it doesn't exist) - this will be in the same folder as the c program
      fprintf(myFile,"Hello World!!!\n");
      fclose(myFile);
}

我读了另一个类似的问题,如果我给它一条直接的道路,它将起作用,并且在某种程度上它会起作用.

I read on another similar question that if I give it a direct path it will work and it has, to some extent;

myFile = fopen("/Users/craigmcallister/Documents/CodeBlocks/First_Text/first.txt","w");

此行代码将.txt文件保存在包含我的源代码的同一项目文件中,这比我想要的解决方案更近一步.

this line of code saves the .txt file in the same project file that contains my source code which is a step closer to the solution I want.

我希望.txt文件自动保存在工作目录中,而其余的项目也保存在工作目录中,在这种情况下,.txt文件会自动打开(如果可能的话)?

I would like .txt files to automatically save in the working directory the rest of the project is saved in and in this case for the .txt file to automatically open (if this is possible)?

我怀疑我的工作目录设置不正确,但是由于我对此很陌生,所以我不知道它是什么样,或者如何更改设置.

I suspect that my working directory is not set up properly but as I am very new to this, I have no idea what this would look like or how I would go about changing the settings.

如果有魔术师/巫师可以帮助消除这种痛苦,我将永远感激不已:)

If any magician/wizard can help put an end to this pain I would be eternally grateful :)

推荐答案

如果您未指定工作目录.

If you do not specify an absolute path, fopen() will open/create files relative to the current working directory.

在Code :: Blocks中看起来像,默认的工作目录不是项目目录.快速浏览Code :: Blocks论坛似乎表明工作目录默认为macOS上用户的主目录.

It looks like in Code::Blocks, the default working directory is not the project directory. A quick search of the Code::Blocks forums seems to indicate that the working directory defaults to the user's home directory on macOS.

看起来您可以在项目">属性">构建目标">执行工作目录"中设置用于运行程序的工作目录.如果将其设置为源文件所在的目录,则也应在该目录中创建文件.

It looks like you can set the working directory that is used to run your program in Project > Properties > Build Target > Execution Working Directory. If you set that to the directory where your source files are located, your file should be created in that directory as well.

这篇关于如何在Mac上的Code :: Blocks上设置工作目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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