LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 01-23-2004, 04:41 PM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Rep: Reputation: 30
Reading from ext file


I'm doing a favor for a mate and trying to get an app made for him... So i'm making him a little C app to do it...

What I need to know is how I would read a variable from external file i.e.

Code:
Settings.ini:

NAME = foo
then in the actual main prog i wanna read the argument:

Code:
#include <stdio.h>
#include "Settings.ini"

int main() {
  char name[128];

  printf("Name = %s", name);
  return 0;
}
I know about using headers but this is going to be run under windoze So i'd want it to be an ini file...

Thanx in advance
 
Old 01-23-2004, 05:18 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I actually wrote a little app that did this exact thing. Here's an exerpt:

Code:
char **drives = NULL;
char *destination = NULL;
char **folders = NULL;
char **extensions = NULL;
char **exclusions = NULL;
int ndrives = 0;
int nfolders = 0;
int nextensions = 0;
int nexclusions = 0;

int change_section(char *str)
{
  if(!strcmpi(str, "[Drives]"))
    return 1;
  if(!strcmpi(str, "[Destination]"))
    return 2;
  if(!strcmpi(str, "[Folders]"))
    return 3;
  if(!strcmpi(str, "[Extensions]"))
    return 4;
  if(!strcmpi(str, "[Exclusions]"))
    return 5;
  return 0;
}

void load_ini(void)
{
  FILE *fp;
  char buf[4096], *b, **dest;
  int line = 0, section = 0;

  if(!(fp = fopen("settings.ini", "r")))
  {
    fprintf(stderr, "Couldn't open \"settings.ini\" for reading!\n");
    exit(1);
  }

  while(fgets(buf, sizeof(buf), fp))
  {
    if(feof(fp))
      break;

    line++;
    b = &buf[strlen(buf)-1];
    while(*buf && (!isprint(*b) || isspace(*b)))
      *b-- = '\0';
    b = buf;
    while(isspace(*b))
      b++;
    if(!*b || *b == '#')
      continue;

    if(*b == '[')
    {
      section = change_section(b);
      continue;
    }

    switch(section)
    {
      case 1:
        if(ndrives)
          drives = realloc(drives, sizeof(char *)*(ndrives+1));
        else
          drives = malloc(sizeof(char *));
        dest = &drives[ndrives++];
        break;
      case 2:
        if(destination)
          free(destination);
        dest = &destination;
        break;
      case 3:
        if(nfolders)
          folders = realloc(folders, sizeof(char *)*(nfolders+1));
        else
          folders = malloc(sizeof(char *));
        dest = &folders[nfolders++];
        break;
      case 4:
        if(nextensions)
          extensions = realloc(extensions, sizeof(char *)*(nextensions+1));
        else
          extensions = malloc(sizeof(char *));
        dest = &extensions[nextensions++];
        break;
      case 5:
        if(nexclusions)
          exclusions = realloc(exclusions, sizeof(char *)*(nexclusions+1));
        else
          exclusions = malloc(sizeof(char *));
        dest = &exclusions[nexclusions++];
        break;
      default:
        fprintf(stderr,
          "Line %d: Found setting outside of section header.\n", line);
        fclose(fp);
        exit(1);
    }

    *dest = malloc(strlen(b)+1);
    strcpy(*dest, b);
  }

  if(!destination)
  {
    fprintf(stderr, "No destination drive was specified.\n");
    exit(1);
  }

  fclose(fp);
}
Then you can simply call load_ini() and viola. Here's an example of the ini file that this program uses:

Code:
# Lines beginning with a hashmark (#) are considered to be comments and 
# are ignored. Blank lines are also ignored.

# --- Configuration Begins ---

# List of drives to search on the PC. Drives that don't exist will simply 
# be ignored. Do NOT include the trailing blackslash (\)

[Drives]
C:
# D:
# E:

# The folder to copy all the found files to. If the folder doesn't exist 
# when the program starts, it will automatically be created. If 
# multiple folders are listed, ONLY the last one will be used. Do NOT 
# include the trailing backslash (\)

[Destination]
C:\User
I hope that answers your question anyway
 
Old 01-23-2004, 09:27 PM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
Can ya post the main() section aswell...
 
Old 01-26-2004, 08:34 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
If you are doing it in Windows, why not use the Win32 APIs for reading ini files. The APIs you will want to look at are the [Get/Write]PrivateProfile* functions.
 
Old 01-26-2004, 11:12 AM   #5
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
To be honest, didn't know anything about the windoze API's...

I'm not looking to get into Windoze programming - thought I could use the knowledge that I have so far to do it as i'm still learning C on Linux and don't wanna confuse it ny taking months to learn windoze API's etc, let alone keep my PC in windoze when i don't have to

Cheers anyway
 
  


Reply



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
NTFS vs Ext File Systems Ken Ju-On Linux - General 15 05-24-2009 11:47 PM
File system damage ext mandrake 10.0 and VMware gaelen Mandriva 1 04-09-2005 11:09 AM
Is arrangement of file systems will differ if we copy a file from FAT 32 to ext 3 ? anindyanuri Linux - Software 2 02-20-2005 11:39 AM
Modifying kernel to support old ext file system mrogersii Linux - Hardware 1 12-09-2004 07:10 PM
Blocking file ext (exe, vbs, bat, com..) with sendmail PcHammer Linux - Software 2 08-29-2002 06:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:14 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
Open Source Consulting | Domain Registration