Thread: look please to this code

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    10

    look please to this code

    Hello every body
    i have the following problem :
    i have different directries .In each directry there are files .I want my program to
    1.scan these directries
    2.then take these files
    3. then open file named result.txt where each line in this file contain two points :
    filename1 contents of filename1
    filename2 contents of filename2
    filename3 contents of filename3
    ....etc
    where fileame , filename2 , filename3 are files names available in directries and in the same line the contents for the same fle apperar .

    i try the follwoing :

    #include <sys/types.h>
    #include <sdtlib.h>
    #include <stdio.h>
    #include <ctype.h>

    main() {
    int i ;
    FILE *d , *t , *f ;
    char fileName[20] ;
    char c ;
    // because i work in unix in the follwoing three lines i list each
    // directry and takes its file and put all of them in dir.txt file

    system("ls dir1 > dir.txt") ;
    system("ls dir2 > dir.txt") ;
    system("ls dir3 > dir.txt") ;

    // now file dir.txt contains all files names .Therefore, the next step is to back dir.txt and read each line in it where each line contain one of the files name then put this name in new file name result.txt and also open file to read its contents and put it in the same line

    t = fopen("result.txt","w");

    d=fopen("dir.txt","r");

    while((c=getc(d) != OEF) {

    if(c != '\n') {
    // if character not new line this mean you still read file name
    putc(c,t);
    fileName[i]=c ; i++ ;
    }
    else {
    i = 0 ;
    // when i need to start read new file name and save in fileName then i value must back to zero
    putc(c,t) ; // c here has new line value
    }
    // after i get the file name i need to open it and read its contents

    f=fopen(fileName,"r") ;!!!!!!!
    while((c=getc(f)) != EOF) {
    ......
    }
    fclose(f) ;
    }
    fclose(t);
    fclose(d) ;
    }
    the prevoius code doesn't work because of line


    f=fopen(fileName,"r") ;!!!!!!!

    because fopenaccept file name as char *f and in my code fileName is array .As a result when i run the program i get segmantation fault
    the question now :
    how i can decide the files want to be open and read at run time where these files names read from text file ???????

    or
    how i can convert fileName array to char *fileName


    i hoooooope i get support
    Thanks a lot
    Nada

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Exclamation

    The segmentation fault is not because conversion problems. You don't need to convert a char array to a char *. The segmentation failt is probably because your array is not null-terminated. Try a null-character after the last character in your array (fileName):

    if(c != '\n') {
    // if character not new line this mean you still read file name
    putc(c,t);
    fileName[i]=c ; i++ ;
    }
    else {
    fileName[i] = '\0';
    i = 0 ;
    // when i need to start read new file name and save in fileName then i value must back to zero
    putc(c,t) ; // c here has new line value
    }

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    Although it is not portable C your compiler may support the findfirst() and findnext() function. Look in your compiler documentation for examples of usage... it would make things alot easier than using files to store the names of the files and system calls....
    one fish two fish
    red fish blue fish

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > Although it is not portable C your compiler may support the findfirst() and findnext() function.
    opendir / readdir / closedir are the unix equivalents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM