Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C程序将标准输出从小写转换为大写的小问题_C_Linux_Char_Buffer_Std - Fatal编程技术网

C程序将标准输出从小写转换为大写的小问题

C程序将标准输出从小写转换为大写的小问题,c,linux,char,buffer,std,C,Linux,Char,Buffer,Std,我正在学习linux中的系统调用,并编写了一个快速程序将stdin复制到stdout 我想在编写标准文本之前将其小写字母转换为大写字母 我使用指向数组的指针实现了一个函数,以大写字符。但它只大写了第一个字母,我不明白为什么。据我所知,由于read()系统调用,我不需要for循环 #include <unistd.h> #define SIZE 512 char charToUpper(char *); int main () { int nread; char bu

我正在学习linux中的系统调用,并编写了一个快速程序将stdin复制到stdout

我想在编写标准文本之前将其小写字母转换为大写字母

我使用指向数组的指针实现了一个函数,以大写字符。但它只大写了第一个字母,我不明白为什么。据我所知,由于read()系统调用,我不需要for循环

#include <unistd.h>
#define SIZE 512
char charToUpper(char *);

int main () {
    int nread;
    char buf[SIZE];
    while (nread = read(0, buf , SIZE)) {
        charToUpper(buf);
        write(1,buf,nread);
    }
    return 0;
}

char charToUpper(char *a){
    if ((*a > 96) && (*a <123)) {
        *a = *a-32;
        return *a;
    }
}
#包括
#定义大小512
char Chartupper(char*);
int main(){
国际nread;
字符buf[大小];
而(nread=读取(0,buf,大小)){
沙图珀(buf);
写入(1,buf,nread);
}
返回0;
}
字符图表组(字符*a){

如果(*a>96)和(*a您的
chartupper
收到一个指向
char
的指针,并且您发送了它
buf
,它将衰减为指向
buf
中第一个字符的
char
的指针,从而得到您的结果

<>记住,在<代码> c>代码>中,你没有得到你所通过的数组的大小,你也必须把它传递下去。想想你在 CuutpPult/Cuth>中的所有操作都在<代码> *<代码> >,这是一个类型:<代码> char < /C>,一个字符。
char charToUpper(char *, unsigned int size);

因此,您知道您实际读取了多少字符并且需要更改。现在尝试更改您的程序以适应此情况。有一个提示-例如,您的
返回值可能需要移动。

除了循环部分,该程序非常好

char* charToUpper(char *a){
    char *t=p;
    while(*a!='\0'){
    if ((*a > 96) && (*a <123)) 
        *a = *a-32;
    a++;
    }
    return t;
}
char*chartupper(char*a){
char*t=p;
而(*a!='\0'){

如果(*a>96)和(*a以下建议代码:

#include <unistd.h>
#include <ctype.h>   // toupper()

#define SIZE 512

void charToUpper(char *);

int main ( void ) 
{
    ssize_t nread;
    char buf[SIZE];
    while ( (nread = read(0, buf , SIZE) ) > 0) 
    {
        // NUL terminate the character string
        buf[nread] = '\0';

        // convert all characters to uppercase
        charToUpper(buf);

        // output the (all uppercase) string to stdout
        write(1,buf, (size_t)nread);
    }
    return 0;
}


void charToUpper( char buf[] )
{
    for( size_t i = 0; buf[i]; i++ )
    {
        buf[i] = (char)toupper( buf[i] );
    }

}
  • 干净地编译
  • 正确终止字符的输入数组
  • 使用头文件中的工具:
    ctype.h
  • 执行所需的功能
  • 现在,拟议的守则:

    #include <unistd.h>
    #include <ctype.h>   // toupper()
    
    #define SIZE 512
    
    void charToUpper(char *);
    
    int main ( void ) 
    {
        ssize_t nread;
        char buf[SIZE];
        while ( (nread = read(0, buf , SIZE) ) > 0) 
        {
            // NUL terminate the character string
            buf[nread] = '\0';
    
            // convert all characters to uppercase
            charToUpper(buf);
    
            // output the (all uppercase) string to stdout
            write(1,buf, (size_t)nread);
        }
        return 0;
    }
    
    
    void charToUpper( char buf[] )
    {
        for( size_t i = 0; buf[i]; i++ )
        {
            buf[i] = (char)toupper( buf[i] );
        }
    
    }
    
    #包括
    #包括//toupper()
    #定义大小512
    无效图表组(字符*);
    内部主(空)
    {
    ssize_t nread;
    字符buf[大小];
    而((nread=read(0,buf,SIZE))>0)
    {
    //NUL终止字符串
    buf[nread]='\0';
    //将所有字符转换为大写
    沙图珀(buf);
    //将(全部大写)字符串输出到标准输出
    写入(1,buf,(大小)nread);
    }
    返回0;
    }
    无效图表组(字符buf[]
    {
    对于(大小i=0;buf[i];i++)
    {
    buf[i]=(char)toupper(buf[i]);
    }
    }
    
    好吧,通过自己缓冲输入数据,您已经把事情复杂化了,而您可以使用
    getchar()
    putchar()
    已经缓冲的函数。此外,使用数字而不是字符文本,隐藏了代码的实际目标(转换为大写,反对在代码中添加奇怪的减法值),并将其与ASCII字符码环境联系起来:

    #include <stdio.h>
    
    int main()
    {
        int c;
        while((c = getchar()) != EOF) {
            if (c >= 'a' && c <= 'z')  /* char literals are your friends */
                c += 'A' - 'a';        /* also here                      */
            putchar(c);
        }
        return 0;
    }
    

    如果使用
    read()
    相反。如果读取完整的缓冲区(例如,从文件中读取,而不是从终端读取),则此程序的缓冲区溢出。您将
    \0
    字符仅放在
    缓冲区
    末尾的一个字符处。请更正此错误。
    #include <unistd.h>
    #include <stdio.h> /* for printing errors to stderr */
    #include <string.h> /* for strerror */
    #include <errno.h> /* for errno definition */
    
    #define SIZE 512
    
    void charToUpper(char *, size_t);
    
    int main () {
        ssize_t nread, nwritten;
        char buf[SIZE];
        while ((nread = read(0, buf , SIZE)) > 0) {
            charToUpper(buf, nread);
            nwritten = write(1, buf, nread);
            if (nwritten < 0) {  /* check for writing errors */
                fprintf(stderr,
                    "Error: write: %s\n",
                    strerror(errno));
                return 1;  /* write error */ 
            }
        }
        if (nread < 0) {  /* check for reading errors */
            fprintf(stderr,
                "Error: read: %s\n",
                strerror(errno));
            return 2; /* read error */
        }
        return 0; /* no error */
    }
    
    void charToUpper(char *a, size_t sz){  /* better declare it void, as we don't return anything */
    
        /* this code assumes uppercase chars and lower case are together in the
         * char map, and contiguous (this is false in EBCDIC mappings) so better
         * to use toupper(*a) in all cases */
        for(;sz--; a++) {  /* for every char in a, up to sz chars. */
            if ((*a >= 'a') && (*a <= 'z')) {  /* if *a is lower case letter */
                *a += 'A' - 'a'; /* convert to uppercase */
                /* better if you use *a = toupper(*a); */
            }
        }
    }