On Fri, 9 Jul 2004, Ed LaBonte wrote:
> 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.
--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to