Dexter's Lab Posted August 18, 2013 at 09:48 PM Report #522041 Posted August 18, 2013 at 09:48 PM Boas, estou a fazer uma função que gera um rectângulo e dentro deste coloca o título do programa e utilizador. Porém, estou com um problema que não tenho conhecimentos para saber o que poderá ser, mas sei que me poderão ajudar com os vossos. A função: void title(char *titlename) { /* declaração das variáveis de tamanho */ const int width = 40; const int height = 6; /* declaração variáveis */ int lenght = strlen(titlename), i = 0; char matrix[width][height]; char user[20], car; /* cria retângulo geométrico de asteriscos */ for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { matrix[x][y] = (y == 0 || y == height-1) ? '*' : ' '; if(matrix[x][y] == ' ') matrix[x][y] = (x == 0 || x == width-2) ? '*' : ' '; if(x == width-1) matrix[x][y] = '\n'; } } /* procura nome do utilizador para uma string */ FILE *file_username = fopen("temp.txt", "w+"); system("whoami > temp.txt"); while((car = getc(file_username)) != EOF){ // POSSIVEL ORIGEM DO ERRO user[i] = car; i++; } fclose(file_username); system("rm -f temp.txt"); /* preenche o retângulo com os nomes */ for(int x = 0; x < lenght; x++) matrix[(width/2 - lenght/2) + x][1] = titlename[x]; for(int x = 0; x < strlen(user); x++) // ERRO matrix[2 + x][height - 3] = user[x]; /* preenche ecrã com os caracteres */ for(int y = 0; y < height; y++) { line(0, 1); for(int x = 0; x < width; x++) { printf("%c", matrix[x][y]); } } } Bom, acontece que quando coloco o conteúdo do ficheiro (nome utilizador) para uma variável, ele vem com mais 5 caracteres estranhos. Tudo corre bem se eu colocar para incrementar x até strlen(user) - 5; mas quero mesmo resolver a situação, não disfarçá-la. Alguém me pode dar uma ajuda em perceber o que está a acontecer e como posso resolver? Obrigado pelo tempo
HappyHippyHippo Posted August 18, 2013 at 10:28 PM Report #522047 Posted August 18, 2013 at 10:28 PM (edited) existem várias coisas que não percebo ... por isso é melhor apresentar uma solução mais elegante: #include <stdlib.h> #include <string.h> #include <stdio.h> #include <pwd.h> #define TITLE_WIDTH 40 #define TITLE_HEIGHT 5 void block(const char * titlename, int width, int height) { char matrix[height][width]; register struct passwd *pw; int x, y, length; memset(matrix, 0, width * height); length = strlen(titlename); if ((pw = getpwuid(geteuid())) != NULL) length += strlen(pw->pw_name) + 3; x = (width - length) / 2 < 1 ? 1 : (width - length) / 2; if (pw != NULL) snprintf(&matrix[height / 2][x], width - 1, "%s - %s", titlename, pw->pw_name); else snprintf(&matrix[height / 2][x], width - 1, "%s", titlename); for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { if (x == 0 || x == width - 1 || y == 0 || y == height - 1) printf("*"); else printf("%c", matrix[y][x] == 0 ? ' ' : matrix[y][x]); } printf("\n"); } } int main(int argc, char ** argv) { block("TITLE", TITLE_WIDTH, TITLE_HEIGHT); return 0; } edit : correcção do snprintf para o caso de erro de leitura do nome do utilizador Edited August 18, 2013 at 10:49 PM by HappyHippyHippo IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
Dexter's Lab Posted August 19, 2013 at 03:04 PM Author Report #522116 Posted August 19, 2013 at 03:04 PM HHHippo, agradeço a atenção e vou estudar o teu código. Podes dizer-me porque poderá estar a acontecer isto: - estou a utilizar o system para enviar para um documento de texto o nome do utilizador. - depois de estar num ficheiro (que abro sem problemas e está lá de facto o nome), extraio caractere a caractere até o caractere ser EOF - só que não vem só o nome.. vêm também 5 outros caracteres que aparentemente não estão no documento texto Porque será que isto acontece? O EOF não deveria terminar no final do nome do utilizador? Obrigado desde já
HappyHippyHippo Posted August 19, 2013 at 04:03 PM Report #522123 Posted August 19, 2013 at 04:03 PM o que deverá existir numa string para dizer que já acabou ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now