Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Linux C newbie question

Reply
Thread Tools

Linux C newbie question

 
 
Ed LaBonte
Guest
Posts: n/a
 
      07-09-2004
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?
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      07-09-2004

"Ed LaBonte" <> wrote in message
news m...
> 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


 
Reply With Quote
 
 
 
 
Ed LaBonte
Guest
Posts: n/a
 
      07-09-2004
On Fri, 09 Jul 2004 16:56:16 +0000, Mike Wahler wrote:


> 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.
 
Reply With Quote
 
Darrell Grainger
Guest
Posts: n/a
 
      07-10-2004
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
 
Reply With Quote
 
Gwar
Guest
Posts: n/a
 
      07-10-2004


On Fri, 10 Jul 2004, Darrell Grainger wrote:

> 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.



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.
>
> --
> Send e-mail to: darrell at cs dot toronto dot edu
> Don't send e-mail to
>

 
Reply With Quote
 
Dan P.
Guest
Posts: n/a
 
      07-10-2004

"Darrell Grainger" <> wrote in message
news...
> 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,
>


<Code snipped>

>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


 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-12-2004
In <> (Darrell Grainger) writes:

>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
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie question About O'Reilly "Python for Unix and Linux SystemAdministration" ftp Mirror question moonhkt Python 2 09-16-2012 03:41 PM
Re: Is Netscape Leaving Firefox Behind on GNU/Linux? THE LINUX PROPAGANDA MACHINE CONTINUES. FIREFOX IGNORING LINUX............. traci.manicotti@gmail.com Computer Support 2 10-20-2007 02:12 PM
Linux... yeah linux.. Linux Have a nice cup of pee NZ Computing 19 04-17-2006 10:16 AM
Newbie linux user question. James Messick Firefox 2 01-27-2004 05:20 PM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57