LinuxQuestions.org
Register a domain and help support LQ
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 12-29-2010, 07:05 AM   #1
manohar
Member
 
Registered: Dec 2010
Posts: 42

Rep: Reputation: 2
Smile How to Read Config file


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...
Attached Images
File Type: jpg fig.JPG (113.9 KB, 16 views)
 
Old 12-29-2010, 07:34 AM   #2
harry edwards
Member
 
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 48
Have a read of this previous post http://www.linuxquestions.org/questi...buffer-172985/ It should help.
 
Old 12-29-2010, 06:55 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
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

Last edited by Nominal Animal; 03-21-2011 at 01:50 AM.
 
Old 12-29-2010, 07:22 PM   #4
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Blog Entries: 62

Rep: Reputation: 54
<removed, dupe>

Last edited by Kenny_Strawn; 12-29-2010 at 07:23 PM.
 
Old 12-29-2010, 07:22 PM   #5
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Blog Entries: 62

Rep: Reputation: 54
What about just using readelf?

Code:
readelf #filename
 
Old 12-29-2010, 10:14 PM   #6
manohar
Member
 
Registered: Dec 2010
Posts: 42

Original Poster
Rep: Reputation: 2
Exclamation

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...
 
Old 12-29-2010, 10:18 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by manohar View Post
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.
 
Old 12-30-2010, 06:47 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,985

Rep: Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753
Quote:
Originally Posted by Kenny_Strawn View Post
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.
 
  


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
Read parameters from config file (file parser?) alaios Programming 8 07-09-2012 11:29 AM
read-only file system error when editing a config file smallfrowne Linux - Newbie 5 02-19-2008 06:06 PM
read options from config file biiiep Programming 4 05-05-2005 03:30 AM
Snort refuses to read config file stakhous Linux - Security 4 05-07-2004 08:48 AM
Unable to read config file............ FXRS Linux - Software 7 07-03-2003 08:36 AM


All times are GMT -5. The time now is 12:13 PM.

Main Menu
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