![]() |
![]() |
#1 |
A1200 040 SAM440EP 667
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
|
Buffer lenght
Hello, I´m trying to make a program to display some variables of Temperature and voltage stored inside 3 folders in ENV:
All is ok but I don´t know why some garbage square chars appear after the printed variables as could be seen from the image attached. I even printed the lenght of the variables buffer in another window but it´s normal, without the size of the garbage squares that I don´t know where they come from ![]() Does someone know how I can get rid of that garbage chars?? Code:
#include <proto/all.h> #include <exec/types.h> #include <intuition/intuition.h> #include <libraries/dos.h> #include <stdlib.h> #include <stdio.h> #define screenWidth 320 #define screenHeight 200 #define screenLeft 0 #define screenTop 0 // Recebe os files com as variaveis, abre-os, extrai e envia as variaveis char *readFile(const char *envName) { struct FILE *file; char fileName[100]; char *buffer = NULL; size_t length = 0; size_t bytesRead; //Abre SensorFile para ler variavel sprintf(fileName,"ENV:%s",envName); file=fopen(fileName,"r"); if(file==NULL){ perror("Nao consegui abrir a variavel"); return -1; } // Determina o tamanho do arquivo fseek(file, 0, SEEK_END); length = ftell(file); fseek(file, 0, SEEK_SET); printf ("Lenght: %i\n",length); // Aloca memória para o buffer buffer = (char *)malloc(length + 1); // +1 para o caractere nulo de terminação // Passa o conteúdo do arquivo para o buffer bytesRead = fread(buffer, 1, length, file); // Adiciona caracter nulo de terminação no final do buffer buffer[bytesRead] = '\0'; fclose(file); return buffer; } int main() { struct Window *myWindow; int hasEnded = FALSE; struct IntuiMessage *msg; ULONG msgClass; char *str; int i; char *varName[]={ "AK_5V_supply_V", "AK_Internal_temp_C", "AK_RTC_battery_percent" }; // open window on the public workbench screen struct NewWindow winlayout = { screenLeft, screenTop, screenWidth, screenHeight, 0,1, IDCMP_CLOSEWINDOW, WFLG_SIZEGADGET | WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_ACTIVATE, //NULL, //NULL, NULL, NULL, "Volts / Temp", //NULL, NULL,NULL, 0,0, 600,400, WBENCHSCREEN }; myWindow = OpenWindow(&winlayout); i=0; while(i<3){ str=readFile(varName[i]); if(i==0){ Move(myWindow->RPort, 40, 60); Text(myWindow->RPort, str, 5); } if(i==1){ Move(myWindow->RPort, 40, 80); Text(myWindow->RPort, str, 10); } if(i==2){ Move(myWindow->RPort, 40, 100); Text(myWindow->RPort, str, 14); } i++; } while (hasEnded == FALSE) { // wait for and event Wait(1L << myWindow->UserPort->mp_SigBit); // get message msg = GT_GetIMsg(myWindow->UserPort); // get class of message; msgClass = msg->Class; // handle message (close it); GT_ReplyIMsg(msg); if (msgClass == IDCMP_CLOSEWINDOW) { CloseWindow(myWindow); free(str); hasEnded = TRUE; } } return(0); } |
![]() |
![]() |
#2 |
Registered User
Join Date: Jun 2016
Location: europe
Posts: 1,094
|
You're telling gfxlib to print 5/10/14 chars while the actual strings are shorter. You should do strlen() or something similar, and then send that to Text() as the 3rd parameter (length).
|
![]() |
![]() |
#3 |
A1200 040 SAM440EP 667
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
|
Thanks a/b, already solved with "Text(myWindow->RPort, str, strlen(str)); "
|
![]() |
![]() |
#4 |
Moderator
Join Date: Nov 2004
Location: Eksjö / Sweden
Posts: 5,755
|
If you have lots of strings, some library functions that require the length provided can be a nuisance.
The problem is the same as on any system, though. There are two solutions: 1. Store strings as BCPL, Pascal, etc together with a length value, e.g. before the first character of the string. This value is read when printing and updated when the string is manipulated to change its length. Low performance cost. 2. Put a termination character after the string, e.g. 0 (ASCIIZ). Requires scanning the entire string before even printing it, but allows free manipulation. High performance cost. In C, a char array is zero-terminated, i.e. strlen() is category 2 / high performance cost. |
![]() |
![]() |
#5 | |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,862
|
Quote:
|
|
![]() |
![]() |
#6 |
Registered User
Join Date: Jun 2016
Location: europe
Posts: 1,094
|
Nah, keep it simple and just include STL's basic_string<template <template <template werwer, fsdfsd>, template <we,msdfsdf>, template <fesdfsdf> > >, template^25 <have, fun, with, 10, pages of, error, messages> >.
|
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
X-Copy track lenght option | kremiso | support.Apps | 0 | 10 March 2023 12:01 |
How to assign some buffer for a string? | peceha | Coders. Blitz Basic | 3 | 21 August 2018 11:19 |
Print buffer... | Pheonix | support.WinUAE | 0 | 05 December 2016 23:56 |
TFMX decoder - module lenght? | trodas | request.Modules | 4 | 28 August 2014 09:12 |
Double buffer copper?? | h0ffman | Coders. General | 8 | 19 July 2011 19:10 |
|
|