在這個程序中,我需要存儲我在一個數組中讀取的ascii字符的頻率(以便打印最頻繁的)。問題是,我從讀取得到的不是ascii(最可能是某種地址),所以在buf []數組中,我越界了。 有人可以幫助我嗎?我如何從閱讀中獲取ascii字符?
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<strings.h>
#define MODEMDEVICE "/dev/ttyAMA0"
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
int main()
{
int fd,c, res=0,i;
struct termios oldtio,newtio;
int buf[128] ;
for (i=0;i<128;i++) buf[i]=0;
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (fd <0) {perror(MODEMDEVICE); exit(-1); }
tcflush(fd, TCIFLUSH);
while (STOP==FALSE) { /* loop until we have a terminating condition */
int r=read(fd,&c,1);
write(1,&c,1);
if (c=='\n') {
for(i=0;i<128;i++)
if (res < buf[i])
res = buf[i];
printf("first char %d \n", res);
}
else {
buf[c]++;
}
}
}
你爲什麼這樣做'INT S = C;'? –
對不起,我正在試驗,我有點忘了它。它絕對沒有理由在那裏。 thnx注意。 – petyhaker
請不要使用這樣的線索折磨讀者,它們不會使代碼更具修飾性,特別不可讀,並且使用更多的空格,編譯器會忽略它們。 –