LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-21-2006, 11:28 PM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
How to read content from environment file?


Hi, guys.
I plan to read some words from environment files, such as

$(HOME)/.bash

How can I open such files and fetch lines? Thanks a lot!

Last edited by ArthurHuang; 06-22-2006 at 12:42 PM.
 
Old 06-22-2006, 03:46 AM   #2
elluva
Member
 
Registered: Aug 2003
Location: Belguim, Ostend and Ghent
Distribution: Ubuntu
Posts: 600

Rep: Reputation: 30
In what programming language?
 
Old 06-22-2006, 09:44 AM   #3
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
And what shell?
 
Old 06-22-2006, 10:37 AM   #4
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
o, is it related to language and shell?

It's C language on linux shell. And the complete path is
$(HOME)/.adcl/config.adcl

Thanks!
 
Old 06-22-2006, 11:41 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
How long have you been doing GNU/Linux & how long have you been doing C?

I don't mean that to sound rude, but ...

There is no "linux shell" per se, only a default linux shell, which is bash. Is this the shell you are referring to?

Also, opening files & reading them is almost the next lesson after "Hello World" in learning a programming language; if you need to do this in C, I suggest a text book. (And, unfortunately, I don't do C, so I can't make any recommendations.)

Finally, are you sure "$(HOME)" is correct syntax? In bash that would be "${HOME}", or "$HOME", or just "~".
 
Old 06-22-2006, 12:54 PM   #6
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
archtoad6:

Thanks for reply.

I wrote shell scripts before, seldom people call that bash scripts. So when you ask what shell, I also doubt us there any else shell?

Sure, I know how to open/read/write user-defined files in C. However, I don't know how to access environment variables such as $HOME and .adcl in c programming. That's why I post my questin here.

More specified, my question is how to open $HOME/.adcl/config.adcl in C?
 
Old 06-22-2006, 01:43 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by ArthurHuang
I don't know how to access environment variables such as $HOME and .adcl in c programming.
".adcl" is just a normal directory name. The fact that it starts with a dot, just makes the command like "ls" not show it by default. Nothing more..

Quote:
Originally Posted by ArthurHuang
More specified, my question is how to open $HOME/.adcl/config.adcl in C?
Here's a complete example:
Code:
/* example.c - How to use an environment variable in C */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>

int main(int argc, char *argv[])
{
    char c;
    int result;
    char *homedir;
    char filepath[PATH_MAX];
    FILE* fp;

    homedir = getenv("HOME");
    if (homedir == NULL) {
	   fprintf(stderr, "Environment variable HOME not found\n");
	   exit(1);
    }
    result = snprintf(filepath, PATH_MAX, "%s/.adcl/config.adcl", homedir);
    if (result >= PATH_MAX) {
	   fprintf(stderr, "Warning: path \"%s\" too long. Truncated.\n", filepath);
    }

    fp = fopen(filepath, "r");
    if (fp == NULL) {
	   fprintf(stderr, "Could not open %s. %s\n", filepath, strerror(errno));
	   exit(1);
    }
    printf("Outputting contents of %s:\n\n", filepath);
    while ((c = fgetc(fp)) != EOF) {
	   fputc(c, stdout);
    }
    fclose(fp);
    return 0;
}
Compile with:
Code:
 gcc -Wall -o example example.c

Last edited by Hko; 06-22-2006 at 01:50 PM.
 
Old 06-22-2006, 01:56 PM   #8
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Ah, a C person answered while I was composing this. I think you need to use Google to learn about the variety shells available in GNU/Linux.

You also should have used it to get the answer to your underlying question before posting here.

Remember, I already said I don't write C, but I still found your basic answer w/ 1 search Google Linux:

C "access envirronment variables"

Last edited by archtoad6; 06-22-2006 at 02:09 PM. Reason: other poster while composing
 
Old 06-22-2006, 10:28 PM   #9
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
Thank you HKO very much.
I don't expect to have the complete or exactly answer here, but you know, any suggestion or advice is a good advice to go ahead in a correct direction. And we can also learn more from others' posts. I thinks that's the reason why many people post questions here.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mounting a tarball to read its content satimis Linux - Newbie 9 02-04-2006 07:18 AM
C++ file I/O, read content as int value Artanicus Programming 7 12-18-2005 02:31 PM
How do i read the content of a CD? therealkilkenny Linux - Newbie 6 12-09-2005 10:43 AM
Read KDE clipboard content neranjana Linux - General 3 05-10-2005 01:12 PM
find the same content in the file ust Linux - Newbie 5 03-23-2005 04:03 AM


All times are GMT -5. The time now is 07:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration