Thread: file merger

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Australia
    Posts
    9
    Rep Power
    0

    file merger


    Im writing a file merger mainly for concating phrack issues =P - but it compiles and doesnt wanna open files and keeps crashing =\ quite odd - if anyone can help would be appriecated - here is the code :
    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    void readstdin(char *array, char *strbuff);

    int main()
    {
       
    int ic2;
       
    char c 'y';
       
    char *base;
       
    char *filem;
       
    char *strbuff = ( char* ) calloc1024sizeof(char) );
       
    FILE *fp1;
       
    FILE *fp2;

       
    printf("Choose base file ?\n>>");
       
    readstdin(basestrbuff);
       
    freestrbuff );
       
    fp1 fopen(base"a+");
       while( !
    fp1 ) {
             
    printf("- failed opening file -\n");
             
    printf("Choose base file ?\n>>");
             
    readstdin(basestrbuff);
             
    freestrbuff );
             
    fp1 fopen(base"a");
       }

       while( 
    != 'n' ) {
          
    printf("Choose file to merge ?\n>>");
          
    readstdin(filemstrbuff);
          
    freestrbuff );
          
    fp2 fopen(filem"rb");
          while( !
    fp2 ) {
             
    printf("- failed opening file -\n");
             
    printf("Choose file to merge ?\n>>");
             
    readstdin(filemstrbuff);
             
    freestrbuff );
             
    fp2 fopen(filem"rb");
          }

          while( (
    c2 fgetc(fp2)) != EOF ) {
             
    fputc(c2fp1);
          }
          
    fclose(fp2);

          
    printf("\nWould you like to merge a file ?");
          
    scanf("%c");
       }
       
    fclose(fp1);

       
    system("pause");
      return 
    0;
    }

    void readstdin(char *array, char *strbuff)
    {
       
    scanf"%s"strbuff );
       array = ( 
    char* ) callocstrlen(strbuff), sizeof(char) );
       
    strcpy( array, strbuff );

    NB: used PHP tags but its C
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Location
    australia
    Posts
    16
    Rep Power
    0

    HI ALEN


    Well i would help u if u werent such a newb
    how found da best book on python, it called "core python programming"
    heard of it??
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    one problem i can see at the first glance:
    PHP Code:
    void readstdin(char *array, char *strbuff)
    ...
       array = ( 
    char* ) callocstrlen(strbuff), sizeof(char) ); 
    you assign to the char*, but this makes "array" a local copy (!) in the scope of "readstdin()".

    you need to pass a pointer to the pointer or a reference to the pointer.
    eg.
    PHP Code:
    void readstdin(char **array, char *strbuff)
    {
       
    scanf"%s"strbuff );
       *array = ( 
    char* ) callocstrlen(strbuff), sizeof(char) );
       
    strcpy( array, strbuff );

    or you can allocate the memory in the main function instead before calling readstdin()
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Australia
    Posts
    9
    Rep Power
    0
    hmm - I tried that but same thing: it gets the base file now and crashes when opening the file to be appended.
    Plus one minute Dev-C++4 was throwing an error -

    d:\my documents\projects\untitled1.c: In function `main':
    d:\my documents\projects\untitled1.c:18: warning: passing arg 1 of `readstdin' from incompatible pointer type
    d:\my documents\projects\untitled1.c:25: warning: passing arg 1 of `readstdin' from incompatible pointer type
    d:\my documents\projects\untitled1.c:32: warning: passing arg 1 of `readstdin' from incompatible pointer type
    d:\my documents\projects\untitled1.c:39: warning: passing arg 1 of `readstdin' from incompatible pointer type
    d:\my documents\projects\untitled1.c: In function `readstdin':
    d:\my documents\projects\untitled1.c:62: warning: passing arg 1 of `strcpy' from incompatible pointer type

    - then iI compile again and it does, very odd.
    Also wondering if that is a problem why doesnt Dev-C++ catch it ?
    Last edited by empty; November 15th, 2002 at 08:46 PM.

IMN logo majestic logo threadwatch logo seochat tools logo