Open files inside directory (UNIX platform)

This is a discussion on Open files inside directory (UNIX platform) within the C Programming forums, part of the General Programming Boards category; I need to open a file inside a directory . I am writting this utility that takes two inputs : ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    31

    Open files inside directory (UNIX platform)

    I need to open a file inside a directory. I am writting this utility that takes two inputs : from-file to-fileIf from-file is a directory and to-file is not, the utility compares the file in from-file whose file name is that of to-file, and vice versa.


    Code:
    #include <sys/file.h>
    #include <unistd.h>
    Code:
       FILE *instream;
       FILE *instream2; 
        instream=fopen(a,"r");
        instream2=fopen(b,"r");
        /*if the file is not there, but inside a directory*/
        if(instream==NULL){
            chdir(b);
            instream=fopen(a,"r");
        }
        if(instream2==NULL){
            chdir(a);
            instream2=fopen(b,"r");
            }
        /*check whether the file open is success or not*/
        if((instream == NULL) || (instream2 == NULL))
            {
                printf("File Open Error!");
                exit(2);
            }
    There are some logical error in the code I have written. It cant seem to open it, and everything it seems like FILE OPEN ERROR message comes up. Any help please is appreciated.

    Thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    What operating system are you using? And are you assuming there's only 1 file inside the directory?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    31

    unix OS

    I am using UNIX. Yes it is assumed that there is only one file inside the directory. And it is assumed that the file name inside the directory is the same as the filename of the other argument. WHat I mean by that is:
    User can type

    myCommand dirName1 fileName1

    I need to open the file with fileName1 inside the dir with dirName1

    or user can type in

    myCommand fileName2 dirName2

    I need to open fileName2 inside dirName2

    thanks.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    You can use the stat() function on fileName1. Assuming the user typed a valid file or directory name it will fill the structure you pass to it with a whole bunch of useful data.

    You're going to be interested in the S_ISDIR() macro (which you should pass the st_mode member to). Take a look at 'man 2 stat' for more information.

    Good luck!
    If you understand what you're doing, you're not learning anything.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what's the problem?
    Code:
    strcat( buf, dirName2 );
    strcat( buf, "/" );
    strcat( buf, fileName2 );
    On an aside, perhaps this FAQ entry would help.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    Code:
     char *buf;
    
       if(instream2==NULL){
            strcat( buf, a);
            strcat( buf, "/" );
            strcat( buf, b );
            instream2=fopen(buf,"r");
            }
    gives me a seg error?
    gcc -ansi -Wall -pedantic yourFile.c

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    buf should be a null-terminated string (with extra room to grow) before you try strcat()'ing it.

    An easier way might be to just use sprintf():
    Code:
    char buf[BUFSIZ];
    
    sprintf(buf, "%s/%s", a, b);
    Be careful of overflowing the space.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    Could you be a little more specific? I am guessing you are stating that buf needs to have a \0 at the end like a string.....

    how can i do that? Can you give me a small code of it?

    thanks

    EDIT: thanks.. I replied before reading ur reply. sorry
    gcc -ansi -Wall -pedantic yourFile.c

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Well, if you just have char *buf; then it's a pointer to some random location. You either need to use a character array or malloc() some memory to use.
    If you understand what you're doing, you're not learning anything.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by swagatob
    Code:
     char *buf;
    
       if(instream2==NULL){
            strcat( buf, a);
            strcat( buf, "/" );
            strcat( buf, b );
            instream2=fopen(buf,"r");
            }
    gives me a seg error?
    Um, hello? You're:
    a) Using an uninitialized pointer.
    b) You haven't allocated any memory to strcat into.

    Naturally it's going to fail. What did you expect?

    [edit] Curses, foiled again! [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    back to sqaure 1.

    Code:
       char *a=argv[2];
       char *b=argv[3];
        char buf[20];
        sprintf(buf, "%s/%s", a, b);
        char buf2[20];
        sprintf(buf2, "%s/%s", b, a);
    
    
    
       FILE* instream=fopen(a,"r");
        FILE *instream2=fopen(b,"r");
        /*if the file is not there, but inside a directory*/
        if(instream==NULL){
            instream=fopen(buf2,"r");
        }
        if(instream2==NULL){
            instream2=fopen(buf,"r");
            }
        /*check whether the file open is success or not*/
        if((instream == NULL) || (instream2 == NULL))
            {
                printf("File Open Error!");
                exit(2);
            }
    produces FILE OPEN ERROr?
    Last edited by swagatob; 10-25-2004 at 01:36 AM.
    gcc -ansi -Wall -pedantic yourFile.c

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you using such tiny buffers? Unix doesn't limit you to 8.3 character file names, you know. This isn't DOS. Additionally, your exit status is non-Standard. You'll usually want to stick to the standard as much as possible.

    Furthermore, you have a horrible coding style. You must have tried learning C++ first, right? I abhor your scattering of variables all over the place. That's one thing I wish they had never added to C99. It makes for ugly code.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    well how to solve the problem Why am I getting FILE OPEN ERROr? filenames I am testing on are very small.... "lao" "tzu" "dir"
    gcc -ansi -Wall -pedantic yourFile.c

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Probably because you aren't giving it a correct path. "./" ring a bell?

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    nah that doesnt solve it..... thanks mate anyway
    gcc -ansi -Wall -pedantic yourFile.c

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  2. open files in a loop in C
    By podiyan in forum C Programming
    Replies: 2
    Last Post: 10-19-2007, 02:50 AM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  4. Replies: 9
    Last Post: 12-19-2006, 04:23 PM
  5. Using Configuration Files with C on UNIX
    By emaxham in forum C Programming
    Replies: 1
    Last Post: 10-16-2001, 03:28 PM

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