How Do I Set the Working directory on Code::Blocks on Mac?

半城伤御伤魂 提交于 2021-01-04 07:21:33

问题


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

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.

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.

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

This is my code.

/**

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");

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.

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 :)


回答1:


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

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.




回答2:


As an Alternative Solution:

  • create an empty file in the Project Worskspace.
  • run the project, then Codeblocks will ask if you want to keep the changed content?.
  • Click yes, then go to the empty file tab to see the program output.


来源:https://stackoverflow.com/questions/42468635/how-do-i-set-the-working-directory-on-codeblocks-on-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!