LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to Read Config file (http://www.linuxquestions.org/questions/programming-9/how-to-read-config-file-853007/)

manohar 12-29-2010 08:05 AM

How to Read Config file
 
1 Attachment(s)
Hi,

I am facing problem in reading binary formatted file in C. Scenario is like,

Config file(shown in the link) is located in /tmp/config.cng location. I want to read this .cnf file and decode the values and assign to the different structures...!!. Could u plz help me on this...

harry edwards 12-29-2010 08:34 AM

Have a read of this previous post http://www.linuxquestions.org/questi...buffer-172985/ It should help.

Nominal Animal 12-29-2010 07:55 PM

Well, since it's binary, you need to know the structure of the file first. It is very difficult to guess based on the file contents only, so don't ask me.

When you do, define a C structure which corresponds to the binary data. For example,
Code:

#include <stdint.h>

struct data_file {
int32_t identifier;
int16_t something[6];
int8_t other[12];
/* ... */
}

Use intN_t and uintN_t types from C99 stdint.h, so that your code is portable.
(If the structure has a suitable identifier, its easy to make this even byte order independent -- but that's not important here.)

I recommend using a function to read the data file, something like this:
Code:

#include <errno.h>
#include <stdio.h>

int read_data_file(const char *const filename, struct data_file *const data)
{
FILE *handle;

/* Invalid parameters? */
if (!filename || !data) {
errno = EINVAL;
return -1;
}

/* Open file in binary mode (in Linux "b" is irrelevant) */
handle = fopen(filename, "rb");
if (!handle)
return -1;

/* Verify file length matches the structure exactly. */
if (fseeko(handle, (off_t)0, SEEK_END)) {
const int saved_errno = errno;
fclose(handle);
errno = saved_errno;
return -1;
}
if (ftello(handle) != (off_t)sizeof(struct data_file)) {
fclose(handle);
errno = EBADF; /* Closest to "File has bad length". */
return -1;
}
rewind(handle);

/* Read the data structure */
if (fread(data, sizeof(struct data_file), 1, handle) != 1) {
fclose(handle);
errno = EBADF; /* Closest to "File has bad format" */
return -1;
}

/* Close file. Note delayed read errors. */
if (fclose(handle))
return -1;

return 0;
}

You can leave out the file length check, but I recommend doing some kind of verification that the file is a configuration file. Almost all binary files have some kind of identification block near the beginning of the file. Note: errno is the standard way to report errors to callers; this reuses EBADF ("Bad file descriptor") to tell that the file does not look like a proper configuration file.

Then, in your program, you can simply call the function:
Code:

int main(void)
{
struct data_file my_data;
const char *my_data_file_name = "/tmp/config.cng";

/* Read the config file; print an error and exit if any problems. */
if (read_data_file(my_data_file_name, &my_data)) {
fprintf(stderr, "%s: %s.\n", my_data_file_name, strerror(errno));
exit(1);
}

/* Okay, got my_data. */
printf("identifier = ", my_data.identifier);
printf("other = 0x%04x 0x%04x ... 0x%04x\n", my_data.other[0], my_data.other[1], my_data.other[11]);

return 0;
}

Hope this helps,
Nominal Animal

Kenny_Strawn 12-29-2010 08:22 PM

<removed, dupe>

Kenny_Strawn 12-29-2010 08:22 PM

What about just using readelf?

Code:

readelf #filename

manohar 12-29-2010 11:14 PM

Hi,

I tried with using fopen , fread but this wasn't worked...!!. The data which iam getting is looks different than data present in config.cfg file...

Sergei Steshenko 12-29-2010 11:18 PM

Quote:

Originally Posted by manohar (Post 4207276)
Hi,

I tried with using fopen , fread but this wasn't worked...!!. The data which iam getting is looks different than data present in config.cfg file...

I do not see your code, and I do not see its output.

michaelk 12-30-2010 07:47 AM

Quote:

Originally Posted by Kenny_Strawn (Post 4207172)
What about just using readelf?

Code:

readelf #filename

Not all binary files are ELF object files. Since the configuration file does not have an ELF structure the command will not work.


All times are GMT -5. The time now is 07:18 AM.