LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to read content environment file? (https://www.linuxquestions.org/questions/programming-9/how-to-read-content-environment-file-457112/)

ArthurHuang 06-21-2006 10:28 PM

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!

elluva 06-22-2006 02:46 AM

In what programming language?

archtoad6 06-22-2006 08:44 AM

And what shell?

ArthurHuang 06-22-2006 09:37 AM

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!

archtoad6 06-22-2006 10:41 AM

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

ArthurHuang 06-22-2006 11:54 AM

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?

Hko 06-22-2006 12:43 PM

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

archtoad6 06-22-2006 12:56 PM

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"

ArthurHuang 06-22-2006 09:28 PM

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.


All times are GMT -5. The time now is 02:54 AM.