intereacting with fily system in c

This is a discussion on intereacting with fily system in c within the C Programming forums, part of the General Programming Boards category; Hey guys, I have a list of several thousand files that I need to merge selectively into one file. The ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    452

    intereacting with fily system in c

    Hey guys,

    I have a list of several thousand files that I need to merge selectively into one file. The files are in three categories, called

    a????.dat
    e????.dat
    i????.dat

    where ???? is an integer that increases linearly, ei

    a0001.dat
    a0002.dat
    ...etc

    I am wondering how I can build a loop that opens these files in order based on the index. Basically my question is, how do I turn the index I am using in my for loop into a string of four integers that ends in my index and then gets .dat appended to it?

    for example, if my index == 108, then the string I need would be

    0108.dat, and I could say

    Code:
    afile = fopen("strcat(a,indexstring)",r");
    Also, I hae never done system calls in C. How can I call the file system to find out how many of these files there are in the current directory at runtime? I am googling this as we speak, so no smartass comments lol, I just thought posting here might speed up the search.

    I am just starting the code now, so suggestion are appreciated in the 20 minutes before I go home from work

    I'll post more details tomorrow as my code evolves.

    EDIT: looks like system() is what I want (go figure?)

    would sprintf with a format string to force 4 digits work to make the index a string?
    Last edited by KBriggs; 06-25-2009 at 02:45 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can use sprintf to build the filename to open.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Hehe I edited with that question right as you posted, thanks

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    If you're on a unix-like system, you can use glob() to get hold of all the files (use the pattern [aei]????.dat). glob() will tell you how many files it matched, it will give you access to the filenames, and it will sort them.

    Of course, if you need to detect certain conditions (say, a gap in the sequence; that is, a missing file), glob() won't tell you. But if it meets your needs, glob() should make life fairly easy.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    there may be gaps, but glob looks like a good starting point. Basically, all I need form the system call is the highest index I will need, or the largest number, basically. Gap handling can come after that.

    Actually I can't seem to find glob in the cplusplus reference website, is it not a standard library function?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As cas mentioned, it's a unix system call, not a C function.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    As tabstop suggested...
    Code:
    char filename[100];
    int index;
    FILE *afile;
    
    for (index = 1; index < 10000; index++) {
        sprintf(filename, "a%04d.dat", index);
        afile = fopen(filename, "r");
    
        // do stuff
    
        fclose(afile);
        }
    As for finding the number of files... do you have FindFirstFIle(), FindNextFile() type calls? You may have to use wildcard "*.dat", or "a*.dat" if you wish to grab them all and count them.
    Last edited by nonoob; 06-25-2009 at 03:53 PM.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    ah, that explains why I couldnt find it haha.

    I think I may just write a shell script to do this - seems like with C I will spend more time messing with pointers than doing any work. But we'll see - my shell scripting is not quite up to par haha.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    As cas mentioned, it's a unix system call, not a C function.
    It is not a system call, it's a helper function provided by the Unix C library set. But you are right in that it is not a C standard library function.

    man glob(3)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operating System
    By Houssen in forum C Programming
    Replies: 18
    Last Post: 04-22-2008, 12:51 PM
  2. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 12:04 PM
  3. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 06:56 PM
  4. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 06:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21