copy a string to the end of another string

This is a discussion on copy a string to the end of another string within the C Programming forums, part of the General Programming Boards category; Hi there! I'm working with strcat but I have some problems. I want to make a function to read a ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    33
    Hi there! I'm working with strcat but I have some problems. I want to make a function to read a line from a file. At the begining, I don't know how long the line is, because of that, I want to start with a small array and read in small slices. These slices I will attach to a final line containing the line of the text. If I use an statically reserved array 'aux', this works properly. But if I use calloc to reserve memory for that array it strcat doesn't work. Anybody knows WHY???

    THANK YOU!

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by bartleby84 View Post
    Anybody knows WHY???
    Yes I do. There is a bug in your code. If you'd post some code we might be able to find that bug.
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    33
    Here is the code SORRY :P

    Code:
    char *leer(char *line,FILE *fp){
    
        char  *aux;
        int length,i;
        char *r;
    
    aux = calloc(longitud,sizeof(char));
    r=fgets(aux,longitud,fp);
    
    if(r!=NULL){
    
     strcpy(line,aux);
     largo=longitud;
     while (strrchr(aux,10)==NULL){
      r=fgets(aux,longitud,fp);
      if (r==NULL)
        break;
      largo = largo+longitud;
      realloc(linea,largo*sizeof(char));
      strcat(linea,aux);
     }
    }
    free(aux);
    return r
    }
    the variable 'longitud' is a globlal variable that is use to determine the minimum size of reading buffer. If there's something you don't understand ask for it.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I can't be shure because you call realloc on a variable linea but you pass line into that function. If your intention is to modify the passed in pointer then this doesn't work this way because you modify a copy. You have to pass a pointer to a pointer.
    Kurt

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    33
    But if I statically reserve memory for aux, all works right! This is the real problem. I realloc line bcause I want to keep in that array (that is reserved out of this function) the entire line.

    Code:
    char *leer(char *line,FILE *fp){
    
        char  aux[10];
        int length,i;
        char *r;
    
    
    longitud=10;
    r=fgets(aux,longitud,fp);
    
    if(r!=NULL){
    
     strcpy(line,aux);
     largo=longitud;
     while (strrchr(aux,10)==NULL){
      r=fgets(aux,longitud,fp);
      if (r==NULL)
        break;
      largo = largo+longitud;
      realloc(line,largo*sizeof(char));
      strcat(line,aux);
     }
    }
    return r
    Last edited by bartleby84; 04-04-2007 at 03:26 AM.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    As I said
    Code:
    realloc(line,largo*sizeof(char));
    Does not change the address of the passed in pointer.
    I don't understand why you say it works if you don't dynamically allocate aux.
    Post a compilable example that shows how you use that function.
    kurt
    EDIT: Yes I do unterstand why it appears to work if you use a statically allocated buffer for aux. If you use a static buffer the realloc propably just expands the allocation and returns the same pointer. if you dynamically allocate aux then realloc cannot expand that buffer and moves the data to another location.
    Last edited by ZuK; 04-04-2007 at 03:46 AM.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    33
    Here is the code you can try first with aux dinamically allocated and then statically.

    Code:
    #include <stdio.h>
    #include<unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int longitud;
    
    // You have to add the leer function!!!!
    
    main(){
    static char  *arg[]={"write here the file you want to create"};
    static char *argori[]={"write here the file you want to read from"};
    char *linea;
    FILE *fp, *fpori;
    char *r;
    fpori = fopen(argori[0],"r");
    fp = fopen(arg[0],"w");
    printf("Set the variable longitud: ");
    scanf("&#37;d",&longitud);
    
    linea = calloc(longitud,sizeof(char));
    r = leer (linea,fpori);
    while (r!=NULL){
    fputs(linea,fp);
    free(linea);
    linea=calloc(longitud,sizeof(char));
    r=leer(linea,fpori);
    }
    fclose(fp);
    fclose(fpori);
    free(linea);
    }
    Last edited by bartleby84; 04-04-2007 at 03:59 AM.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    33
    Quote Originally Posted by ZuK View Post
    As I said
    EDIT: Yes I do unterstand why it appears to work if you use a statically allocated buffer for aux. If you use a static buffer the realloc propably just expands the allocation and returns the same pointer. if you dynamically allocate aux then realloc cannot expand that buffer and moves the data to another location.
    But why happen that if I allocate aux and the pointer I use in reallocate is another one?

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by bartleby84 View Post
    But why happen that if I allocate aux and the pointer I use in reallocate is another one?
    You allocate some memory for line then you allocate the buffer aux, your memorymanager will propably allocate the space for aux just after the space for line. When you now call realloc then the memorymanager cannot expand line ( because the spcae is occupied by aux ) so it has to move the data to another location and return anoter pointer that you assigne to line in leer, but this line is not the line from main ( it's a copy ).
    Kurt

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    33
    Thank you very much for your clear explanation. I'm gona work on that.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Split from this OLD THREAD
    Read the forum rules.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM

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