How can i get the number of errno to pass to a function?
Printable View
How can i get the number of errno to pass to a function?
errno is a global variable that is always available to your program. Just use it like you would an int that you defined yourself.
And, since it's global, your function already has access to it.
Todd
#include <errno.h>
I know they are global variable, but i need to control an array with the value passed by errno. But, they not works. Here is the code:
This code returns this following error message:Code:#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
const char *descricao[] = {"Operação não permitida", "Arquivo ou diretório inexistente"};
const char *abrir_arquivo(char *arquivo)
{
FILE *fp;
if ((fp = fopen(arquivo, "r")) == NULL)
return descricao[errno];
else
return "Ok";
}
int main (void)
{
printf("%s\n", abrir_arquivo("asdasdasd"));
return 1;
}
Segmentation fault (core dumped)
Its probably the errno is greater than 1, then the error is displayed.
This follows worked perfectly, but has much work (I need to do this in 92 messages) and the code is not professional:
My question is:Code:char *erro(int erro)
{
switch(erro)
{
case 1:
return "Operação não permitida";
break;
case 2:
return "Arquivo ou diretório inexistente";
break;
}
}
const char *abrir_arquivo(char *arquivo)
{
FILE *fp;
if ((fp = fopen(arquivo, "r")) == NULL)
return erro(errno);
else
return "Ok";
}
int main (void)
{
printf("%s\n", abrir_arquivo("asdasdasd"));
return 1;
}
How can I pass the correct number of errno to my function, so that this error is not more show?
Eh? Why not just call strerror()? The whole purpose of strerror() is to convert errno values to strings. It uses the locale, so it should display in the correct language. As a bonus, your error messages will work in other languages as well.
Or just perror?
I do not want to call strerror nor perror because I must translate the error messages to Portuguese.
Do you know any way to translate them?
Thanks for the help.
How can I access the locale to translate then?
Thanks and sorry for my bad reply...
Sounds like a couple hours of work for someone who knows English and Portuguese.
I know english and portuguese. Im from Brazil.
The locale is supposed to be set automatically. If you are running a Portuguese version of Windows, then perror() should automatically show error messages in Portuguese. If you're running some form of UNIX, then you just need to make sure your locale is configured properly. How you do it is OS-dependent.
Point begin, you don't have to do anything in the code to make it happen.
Your code is correct, except that judging from your working code:
Your array probably should be:Code:char *erro(int erro)
{
switch(erro)
{
case 1:
return "Operação não permitida";
break;
case 2:
return "Arquivo ou diretório inexistente";
break;
}
}
Your previous code would attempt to use the third element of an array with 2 elements - which damn well should segfault.Code:const char *descricao[] = {"OK", "Operação não permitida", "Arquivo ou diretório inexistente"};
Also, in Linux/Unix, most likely setting the environment variable C_LOCALE to something sensible. More info by doing "man locale" and listing the locale names by "locale -a".
--
Mats
matsp, thank you for the help.
The errno messages of FreeBSD, returns number between 1 and 92. The index of arrays in C, begins with 0. Here is the error. The errno returns me the number 2, but the max value of my array is 1!!!
Here is the correct code:
Thanks for allCode:const char *descricao[] = {"", "Operação não permitida", "Arquivo ou diretório inexistente"};
const char *abrir_arquivo(char *arquivo)
{
FILE *fp;
int *a;
if ((fp = fopen(arquivo, "r")) == NULL)
{
if ((errno >= 0) && (errno <= 2))
return descricao[errno];
else
return "Erro desconhecido";
}
else
return "Aberto";
}
int main (void)
{
printf("%s\n", abrir_arquivo("asdasdasd"));
return 1;
}
Best Regards!
:D
Now, I've got the same error, when I try to concat strings:
Segmentation fault (core dumped)
Here is the code:
Code:#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define INTSIZE 2
char *itoa(int value)
{
int count, i, sign;
char *ptr, *string, *temp;
count = 0;
if ((sign = value) < 0)
{
value = -value;
count++;
}
temp = (char *) malloc(INTSIZE + 2);
if (temp == NULL)
{
return(NULL);
}
memset(temp,'\0', INTSIZE + 2);
string = (char *) malloc(INTSIZE + 2);
if (string == NULL)
{
return(NULL);
}
memset(string,'\0', INTSIZE + 2);
ptr = string;
do
{
*temp++ = value % 10 + '0';
count++;
} while (( value /= 10) >0);
if (sign < 0)
*temp++ = '-';
*temp-- = '\0';
for (i = 0; i < count; i++, temp--, ptr++)
{
memcpy(ptr,temp,sizeof(char));
}
return(string);
}
char *descricao[] = {"", "Operação não permitida", "Arquivo ou diretório inexistente"};
char *abrir_arquivo(char *arquivo)
{
FILE *fp;
char *erro;
if ((fp = fopen(arquivo, "r")) == NULL)
{
if ((errno >= 0) && (errno <= 2))
{
strcat(descricao[errno]," - Cód.: ");
strcat(descricao[errno],itoa(errno));
return descricao[errno];
}
else
return "Erro desconhecido";
}
else
return "Aberto";
}
int main (void)
{
printf("%s\n", abrir_arquivo("asdasdasd"));
return 1;
}
Does anyone know what might be happening?
You are trying to strcat() onto a string constant. Even if it didn't cause a segfault, it would make no sense
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:
Why?Code:char *erro;
erro = descricao[errno];
strcat(erro," - Cód.: ");
strcat(erro,itoa(errno));
return erro;
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;
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
How can i do that? Can you post a example?
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.
This is the solution:
It works perfectly. Thanks for all.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;
}
:D
I doubt this retuns what you expect...Code:strlen(itoa(errno) + 1)
itoa is not standard
and better check the return value of malloc before using the buffer