Linux C newbie question

E

Ed LaBonte

I'm a rank beginner in C programming and I'm using gcc in a Linux OS. I am
getting my feet wet with a little program which trains people to calculate
days of the week from dates in their head. The practice exercizes are in c
and the tutorial is a text file in the same directory as the executable.
I access the tutorial from within the program by using the system()
function with "less". system("less tutorial"). That works fine so long as
the user is in the right directory when he is running the program. I
realize that the best way would be to specify the complete path, but I
don't know where the user will be installing the program directory. Is
there any way I can specify that it is in the same directory as the
executable? What would be the best way to handle this problem?
 
M

Mike Wahler

Ed LaBonte said:
I'm a rank beginner in C programming and I'm using gcc in a Linux OS. I am
getting my feet wet with a little program which trains people to calculate
days of the week from dates in their head. The practice exercizes are in c
and the tutorial is a text file in the same directory as the executable.
I access the tutorial from within the program by using the system()
function with "less". system("less tutorial"). That works fine so long as
the user is in the right directory when he is running the program. I
realize that the best way would be to specify the complete path, but I
don't know where the user will be installing the program directory. Is
there any way I can specify that it is in the same directory as the
executable? What would be the best way to handle this problem?

On many implementations, 'main()'s second parameter, 'argv's first
array element, i.e. 'argv[0]' will point to the executable file name
used to invoke it. This string often includes the 'full path', e.g.
"C:\MyDir\MyProg.exe". This may or may not be the case on your system.

Take a look with something like this:

#include <stdio.h>

int main(int argc, char *argv[])
{
printf("Executable name: %s\n", (argc > 0) && (argv[0][0])
? argv[0]
: "[not available]");

return 0;
}

-Mike
 
E

Ed LaBonte

On many implementations, 'main()'s second parameter, 'argv's first
array element, i.e. 'argv[0]' will point to the executable file name
used to invoke it. This string often includes the 'full path', e.g.
"C:\MyDir\MyProg.exe". This may or may not be the case on your system.

Take a look with something like this:

#include <stdio.h>

int main(int argc, char *argv[])
{
printf("Executable name: %s\n", (argc > 0) && (argv[0][0])
? argv[0]
: "[not available]");

return 0;
}

-Mike

No, but thanks for the response. argv[0] just returns whatever was typed
in. If the program is in the path and you just type "programname",
argv[0] is just "programname". If you type in the whole path then that
will be in argv[0] also, but only if you enter the whole path.

But thanks again for the response.
 
D

Darrell Grainger

I'm a rank beginner in C programming and I'm using gcc in a Linux OS. I am
getting my feet wet with a little program which trains people to calculate
days of the week from dates in their head. The practice exercizes are in c
and the tutorial is a text file in the same directory as the executable.
I access the tutorial from within the program by using the system()
function with "less". system("less tutorial"). That works fine so long as
the user is in the right directory when he is running the program. I
realize that the best way would be to specify the complete path, but I
don't know where the user will be installing the program directory. Is
there any way I can specify that it is in the same directory as the
executable? What would be the best way to handle this problem?

If something isn't working, try something different.

In other words, don't use the system() command. The system() command is
not very flexible and gives you little options when something has gone
wrong.

Instead, consider learning to open a file using fopen(), reading the
contents in using fgets() and then printing it to the screen using puts().
This way, when you attempt to open the file and it is not in the current
directory you can print a helpful message to the user. For example,

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
const char filename[] = "tutorial";
FILE *fp;

fp = fopen(filename, "r");
if(fp == NULL) {
fprintf(stderr, "Open '%s' for read failed.\n", filename);
exit(EXIT_FAILURE);
}

/* do the rest here */

fclose(fp);
return 0;
}

Another option is to learn about using command line arguments. You could
then have it so the user has to specific the location of the tutorial file
when they run the program.
 
G

Gwar

If something isn't working, try something different.

In other words, don't use the system() command. The system() command is
not very flexible and gives you little options when something has gone
wrong.


I don't necessarily disagree with the approach this poster is bringing up,
but as an alternative to system, you might want to check out execl,
execle, execlp, execv, execve, or execvp functions, declared in unistd.h.



Instead, consider learning to open a file using fopen(), reading the
contents in using fgets() and then printing it to the screen using puts().
This way, when you attempt to open the file and it is not in the current
directory you can print a helpful message to the user. For example,

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
const char filename[] = "tutorial";
FILE *fp;

fp = fopen(filename, "r");
if(fp == NULL) {
fprintf(stderr, "Open '%s' for read failed.\n", filename);
exit(EXIT_FAILURE);
}

/* do the rest here */

fclose(fp);
return 0;
}

Another option is to learn about using command line arguments. You could
then have it so the user has to specific the location of the tutorial file
when they run the program.
 
D

Dan P.

Darrell Grainger said:
If something isn't working, try something different.

In other words, don't use the system() command. The system() command is
not very flexible and gives you little options when something has gone
wrong.

Instead, consider learning to open a file using fopen(), reading the
contents in using fgets() and then printing it to the screen using puts().
This way, when you attempt to open the file and it is not in the current
directory you can print a helpful message to the user. For example,

Another option is to learn about using command line arguments. You could
hen have it so the user has to specific the location of the tutorial file
when they run the program.


In addition to your advice, the OP probably needs to check out a Linux
newsgroup, or better yet just search Google, to find the Linux command which
returns the application's path. Of course, that solution is not portable,
but I think it's a little nicer than asking the user of the program to type
in the full path of the file everytime they run the program.

Another option is to set up a configuration file where one of the parameters
is the full path to the file. That way, they can type it in just once in
that file. Then if it can't find it there, it can exit out gracefully and
display an error message like you had in your program.



Dan
 
D

Dan Pop

In said:
In other words, don't use the system() command. The system() command is
not very flexible and gives you little options when something has gone
wrong.

What is this mythical "system() command" you're talking about?

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top