C 从读取系统调用的缓冲区中查找我的缓冲区中的换行符图表



这是家庭作业!

目前我正在做一项作业,我需要使用 Read Call 从文件中读取字符,然后将字符一次写到屏幕上......好的,没问题,但是在每行之后,我需要将一个行计数器增加一,并且每 20 行我需要暂停输出,直到用户按下空格

        char buffer[1];
        int n_char = 0;
        //read (fileDesc, buffer, 5);
        while( (n_char=read(fileDesc, buffer, 1))!=0) 
        {   
            if (buffer[1] == 'n')
            {
                //this is not incementing?
                lineCount++;
            }
            if (lineCount % 20 == 0)
            {
                //wait for a space to be pressed
                //for the time being sleep to make sure im counting lines correctly
                sleep(5);
            }
            n_char=write(1,buffer,n_char); 
        } 

目前,我在弄清楚为什么我无法检测到换行符时遇到了问题,任何帮助将不胜感激!

声明

buffer

char buffer[1];

这意味着一个字符,要访问第一个字符,您必须说

if (buffer[0] == 'n')

数组索引从 0 开始。

最新更新