why my Xcode doesn't find the .txt files that I create? - c

my program seems to "see" only the .txt files which he creates with the fopen("file.txt","w") function. I changed the working directory and put added the file to the project, but if I create the file.txt the program can't see it. the only way is to edit the one he creates with the open function

You just have a problem concerning the current directory, it is not what you expect.
When you create "file.txt" it is created in the current directory because the path is not specified, so you can open it after because it is where the program look at by default.
{edit add}
You are under MacOS probably, if you start the program by hand from a shell like /Applications/......./prog the current directory is the current directory where you are in the shell, but if you start it through its icon etc it will depends on the installation directory

Related

How to change the current directory to a designated directory using chdir() in c?

EDIT
The existed directory is not necessarily a sub-directory of the home directory. It can be a sub-directory of a sub-directory of the home directory.
End of EDIT
I am reading a user input (e.g., cd existedDirectory) to change the current directory to the "existedDirectory". I know that
chdir(getenv("HOME"));
can change the current directory to home directory, so I tried the following methods:
chdir(getenv("/existedDirectory"));
chdir(getenv("existedDirectory"));
chdir(getenv("~/existedDirectory"));
chdir("/existedDirectory");
chdir("existedDirectory");
chdir("~/existedDirectory");
Nothing worked. Any help will be greatly appreciated.
chdir takes a string argument with a path to change to. It does no other special handling (environment vars or home or anything else), so you if you want any of those things, you need to build a string to pass into it.
chdir("/existingDirectory");
which change to and existing directory in the root directory -- it has an absolute path. So it has to be one of the directories you see when you run ls /.
chdir("exisitingDirectory");
will change to an existing subdirectory of the current director.
If you want to change to a subdirectory of your home directory, you need something like:
char path[PATH_MAX];
sprintf(path, "%s/%s", getenv("HOME"), "subdir");
chdir(path);
chdir() in C only affects the process, which calls it. Your shell and your program are two different processes, which run separately. So, the environmental variable $PWD, which indicates the working directory, will only be changed for your application. Check this resource, too.
Note:chdir() doesn’t change the working directory of current shell. Because when the program is executed in the shell, the shell follows fork on exec mechanism. So, it doesn’t affect the current shell.

Link a binary to execute from it's orignal directory

I have a program written in C. It's directory structure is like this:
root/
program.sh
textures/
texture1.png
program.sh imports textures in a way like: load_textures("./textures/texture1.png") (relative paths). Hence, the program only works whenever I execute program.sh from the root directory.
I want to create a link to program.sh such that, whenever I execute the link, I am able to execute the program.sh from the root directory. In other words, I want to execute program.sh from outside the root directory.
Now, I would prefer a solution to do this without changing the source code (if there is a way), otherwise, how would you recommend that I open files/textures in C, given the current scenario?
Had a similar question here, but it's unanswered.
As it became clear from comments, the goal was to execute the program from a different directory, but with the program's current working directory pointing to its own directory.
While this can be done in C (in a POSIX system, as assumed due to the .sh extension, via a combination of the chdir and execvp syscalls), it's much more easily achieved in a wrapper shell script, like so:
#!/bin/sh
pushd /path/to/executable
./program.sh $#
popd

How to make my Linux C program accessible from bash

Say I made and compiled a small program in C to count the bytes of a file, called filebyte. To run it I would use ./filebyte
Now I want to make it universal on bash, like for example to run a php file, I would use bash command php file.php, same way I would like to run my program, filebyte filename.
How do I do this?
Thanks!
I often create a bin/ directory in my home directory, for small custom applications.
You then need to add that directory to your PATH, which is a list of colon-separated paths that your shell searches for executables when you type a name on thr command line.
This is usually accomplished by putting this in your ~/.bashrc file:
PATH="$PATH:~/bin"
Check the environment variable PATH and put the executable in one of the directories listed. You can also put it in a custom directory and then append it to PATH. You can check it by executing printenv PATH
If you want it for your current active shell alone, do
export PATH=$PATH:</path/to/file>
For permanently making the file available add the above line to ~/.bashrc
Why add it in PATH variable, man bash says why,
PATH The search path for commands. It is a colon-separated list of
directories in which the shell looks for commands (see COMMAND
EXECUTION below). A zero-length (null) directory name in the
value of PATH indicates the current directory. A null directory
name may appear as two adjacent colons, or as an initial or
trailing colon. The default path is system-dependent, and is set
by the administrator who installs bash. A common value is
''/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin''.

Minix 3: add new command

I'm currently trying to add a new command to Minix. I want to add a command named smile, which I want to display continuously a smiling face :-)the same way the command yes do with y.
I already created the smile dir in src/commands, with smile.c and Makefile, and added smile to src/commands/Makefile.
But after compiling I still get
# smile
smile: not found
Is there another file/header/script where I must add a reference to smile?
Thank you in advance!
You either need to specify the directory, or the directory must be part of the PATH environment variable.
Try
./smile
if the program is in the current directory.
or
export PATH=".:$PATH"
smile
to always check the current directory for commands during this bash session.
If the program is in a subdirectory (eg. src/commands/smile) relative to the current directory,
you can add that to the path instead. Either explicitly,
src/commands/smile/smile
or by adding to the search path
export PATH="src/commands/smile:$PATH"
smile
placing your executable in the /usr/bin folder will allow the shell to recognize it. you should be able to type #smile now

Running C programs in Linux/MacOSX

Here is another(probably) noob question. Let us assume that I have a simple 1 file program (called myProg.c) written in C. When I want to compile this program in Linux/MacOSX, I type "gcc -o haha myProg.c". The executable file that is generated is now named "haha". When I wish to run this program, I will need to type "./haha" in the console.
What do I need to do to stop typing "./" always? How can I make sure that by just typing "haha", my program will be invoked? I have checked the permissions of the file "haha" and it is executable. Am I correct in thinking that the "./" indicates that the path of executable file i.e., the file is present in the current directory (".")??
The current directory is by default not a part of PATH in unix-derived OS'es. This is a security measure, which you can but should not change by modifying PATH in your .bash_profile or .bashrc
The reason not to include the current directory in path: Assume that you are root, and you have a malicious user. This user creates e.g. a ls executable in his home directory, which does something not nice. If you're looking at what this user is up to, and type ls while in his home directory, his ls will be executed.
If you want to just change it, add PATH="${PATH}:." to your .bashrc and .bash_profile

Resources