Thread: errno

  1. #16
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You are trying to strcat() onto a string constant. Even if it didn't cause a segfault, it would make no sense

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Exclamation

    I can't do:

    strcat(descricao[errno]," - Cód.: ");
    strcat(descricao[errno],itoa(errno));

    That's because I'm trying to modify a literal (descricao), which is not possible.

    Right??



    Then, I've tried this, but not works too:

    Code:
    char *erro;
    
    erro = descricao[errno];
    strcat(erro," - Cód.: ");
    strcat(erro,itoa(errno));
    return erro;
    Why?

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Question

    ok brewbuck

    I had not seen your post ... hehe



    but why this not work?


    Code:
    char *erro;
    
    erro = descricao[errno];
    strcat(erro," - Cód.: ");
    strcat(erro,itoa(errno));
    return erro;

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because you are trying to extend a string constant, and those are generaly read-only, not to mention that there's unlikely to be "space after" your string. Pass in a string that can be filled in, or have a static local string, or something like that.

    --
    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.

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Question

    How can i do that? Can you post a example?

  6. #21
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by galindro View Post
    Then, I've tried this, but not works too:

    Code:
    char *erro;
    
    erro = descricao[errno];
    strcat(erro," - Cód.: ");
    strcat(erro,itoa(errno));
    return erro;
    Why?
    The line "erro = descricao[errno]" just makes erro point at the same string constant. You need to actually strcpy(erro, descricao[errno]) -- the rest of the code is correct, provided you actually allocate some space for erro. Currently, it's just an uninitialized pointer.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    The line "erro = descricao[errno]" just makes erro point at the same string constant. You need to actually strcpy(erro, descricao[errno]) -- the rest of the code is correct, provided you actually allocate some space for erro. Currently, it's just an uninitialized pointer.
    And of course, erro in this case needs to be some valid memory, and if you are going to return it, better not make it a automatic local variable - either allocate memory, make it static or pass in a string to store it.

    --
    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.

  8. #23
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Thumbs up

    This is the solution:


    Code:
    char *abrir_arquivo(char *arquivo)
    {
        FILE *fp;
        char *erro;
        char *codstr    = " - Cod.: ";
        char *desconstr = "Erro desconhecido";
        char *abertostr = "Aberto";
    
    
        if ((fp = fopen(arquivo, "r")) == NULL) {
            if ((errno >= 0) && (errno <= 2)) {
                erro = malloc(strlen(descricao[errno]) + 
                              strlen(codstr) +
                              strlen(itoa(errno) + 1));
                strcpy(erro, (descricao[errno]));
                strcat(erro, codstr);
                strcat(erro, itoa(errno));
            }
            else {
                erro = malloc(strlen(desconstr)+1);
                strcpy(erro, desconstr);
            }
        }
        else {
            erro = malloc(strlen(abertostr) + 1);
            strcpy(erro, abertostr);
        }
        return erro;
    }
    
    int main(void)
    {
        char *message;
        message = abrir_arquivo("asdasdasd");
        printf("%s\n", message);
        free(message);
        return EXIT_SUCCESS;
    }
    It works perfectly. Thanks for all.


  9. #24
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    Code:
    strlen(itoa(errno) + 1)
    I doubt this retuns what you expect...
    itoa is not standard
    and better check the return value of malloc before using the buffer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to clean errno?
    By kotoko in forum C Programming
    Replies: 20
    Last Post: 05-14-2009, 12:21 PM
  2. errno function, error TLS/non TLS
    By fran.b in forum C Programming
    Replies: 13
    Last Post: 03-25-2009, 08:06 AM
  3. /usr/bin/ld: errno: TLS definition in /lib/libc.so.6
    By nasim751 in forum C Programming
    Replies: 4
    Last Post: 02-25-2009, 04:15 AM
  4. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM