【问题标题】:My c program is generating an unwanted output我的 c 程序正在生成不需要的输出
【发布时间】:2017-03-25 16:13:23
【问题描述】:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int main()
{
    system("cls");
    int i1,n;
    scanf("%d\n",&n);
    for(i1=0;i1<n;i1++)
    {
        char *s;
        s=(char *)malloc(sizeof(char)*20);
        gets(s);
        int l=strlen(s);
        int l1=l;
        int i,j;
        for(i=0;i<l;i++)
        {
            if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='O'||s[i]=='I'||s[i]=='U')
            {
                for(j=l1-1;j>=0;j--)
                {
                    if(s[j]=='a'||s[j]=='e'||s[j]=='i'||s[j]=='o'||s[j]=='u'||s[j]=='A'||s[j]=='E'||s[j]=='O'||s[j]=='I'||s[j]=='U')
                    {
                        printf("%c",s[j]);
                        l1=j;
                        break;
                    }
                }
            } 
            else
            {
            printf("%c",s[i]);
            }
        }
        printf("\n");
        free(s);
    }
    getch();
    return 0;
}

这是一个反转字符串元音顺序的程序 (education -&gt; odicatuen)。在下图中,左侧是输入文件,右侧是输出文件。可以看到开头有一个向上的箭头

程序中没有错误。它工作正常。我有一个输入文本文件,我通过命令提示符将我的输出保存在一个输出文本文件中。我的输出文件开头出现意外的“上箭头字符”

【问题讨论】:

  • “程序中没有错误” - 那你为什么在这里发帖?
  • 不要使用gets,永远不要使用它。这是一个危险的功能,如果您继续使用它,迟早会导致重大问题。此外,它在 C99 标准中已被弃用,并在最新的 C11 标准中完全删除。
  • 另外,调试器是一个非常有用的工具,如果你真的想成为一名程序员,即使是业余爱好者,你也确实需要学习如何使用它。
  • 我投票决定将此问题作为题外话结束,因为程序中没有错误并且运行良好。

标签: c string stdout stdin


【解决方案1】:

问题是调用system("cls");引起的

cls 命令清除控制台屏幕。这是通过将换页符(ASCII 值 12)打印到 stdout 来完成的。命令提示符将此字符解释为清除屏幕。但是当您将输出重定向到文件时,此字符将成为输出的一部分。因此,您看到的向上箭头字符是 ASCII 代码 12 在记事本中的显示方式。

删除对system("cls");的调用,您将不会在文件中获得额外的输出。

【讨论】:

    【解决方案2】:

    您声明:程序中没有错误。它工作正常

    但是,您发布的代码副本显示了错误:

    一些建议:

    1. 每行只有一个语句,每个语句(最多)一个变量声明。
    2. 单独的代码块(for、if、else、while、do...while、switch、case、默认通过一个空行
    3. 使用适当的水平间距以提高可读性
    4. 变量和参数名称应指示“内容”或“用法”(或更好,两者兼而有之)
    5. 始终缩进代码。在每个左大括号 '{' 后缩进。在每个右大括号 '}' 之前取消缩进。
    6. 如果没有,您的编译器应该告诉您“gets()”,然后获取现代编译器和/或打开警告。

    现在你的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>           // <== this is not portable, strongly suggest using the C standard library functionality
    
    int main()                   // <-- suggest: 'int main( void )'
    {
        system("cls");           // <-- this is not portable, 
                                 //     suggest using the ansi terminal escape sequences
        int i1,n;                // <-- better written as
                                 //     'int il;'
                                 //     'int n;'
    
        scanf("%d\n",&n);        // <--This will (usually) fail due to the '\n' in the format string
                                 // <-- when calling any of the 'scanf()' family of functions
                                 //     always check the returned value (not the parameter value)
                                 //     to assure the operation was successful
                                 //     I.E.
                                 //     'if( 1 != scanf( "%d", &n ) )'
                                 //     '{'
                                 //          'perror( "scanf failed" );'
                                 //          'exit( EXIT_FAILURE );'
                                 //     '}'
        for(i1=0;i1<n;i1++)      // <-- for readability suggest:
                                 //     'for( il=0; il<n; il++ )'
        {
            char *s;
            s=(char *)malloc(sizeof(char)*20);
                                 // <-- when calling any of the heap allocation functions (malloc, calloc, realloc)
                                 //     1) do not cast the returned value.  The returned value has type 'void*'
                                 //        which can be assigned to any other pointer
                                 //        casting just clutters the code
                                 //     2) always check (!=NULL) the returned value to assure the operation was successful.
                                 //     3) the expression 'sizeof(char)' is defined in the standard as 1
                                 //        in the parameter to any of the heap allocation functions,
                                 //        multiplying by 1 has no effect and just clutters the code                           
                                 //     suggest:
                                 //     'if( NULL == (s = malloc(20) ) )'
                                 //     '{'
                                 //         'perror( "malloc failed" )'
                                 //         'exit( EXIT_FAILURE );'
                                 //     '}'
            gets(s);             // <-- the function 'gets()' has been depreciated for years and
                                 //     completely eliminated in the latest C standard
                                 //     suggest:
                                 //     'if( ! fgets( s, 20, stdin ) )'
                                 //     '{'
                                 //         'perror( "fgets failed" )'
                                 //         'free( s );   // cleanup'
                                 //         'exit( EXIT_FAILURE );'
                                 //     '}'
            int l=strlen(s);     // <-- 'strlen()' returns a 'size_t' not an 'int'
            int l1=l;            // <-- assigning an 'size_t' to an 'int' is problematic
    
            int i,j;             // <-- note: earlier comments about variable declarations
    
            for(i=0;i<l;i++)     // <-- note: earlier comments about readability and horizontal spacing
            {
                if(s[i]=='a'     // <-- code lines should honor the width of the printed page (80 or less characters)
                 ||s[i]=='e'     //     what about 'y'/'Y' is sometimes a vowel
                 ||s[i]=='i'     // <-- you should learn about 'toupper()' and 'tolower()'
                 ||s[i]=='o'
                 ||s[i]=='u'
                 ||s[i]=='A'
                 ||s[i]=='E'
                 ||s[i]=='O'
                 ||s[i]=='I'
                 ||s[i]=='U')
                {
    
                    for(j=l1-1;j>=0;j--)  // <-- note earlier comments about readability and horizontal spacing
                    {
                        if(s[j]=='a'
                         ||s[j]=='e'
                         ||s[j]=='i'
                         ||s[j]=='o'
                         ||s[j]=='u'
                         ||s[j]=='A'
                         ||s[j]=='E'
                         ||s[j]=='O'
                         ||s[j]=='I'
                         ||s[j]=='U')
                        {
                            printf("%c",s[j]);
                            l1=j;
                            break;
                        }
                    }
                }
                                          // <-- note earlier comment about readability
                else
                {
                printf("%c",s[i]);        // consistently indent the code
                }
            }
    
            printf("\n");
            free(s);
        }
                                          // <-- note: earlier comment about readability
        getch();                          // <-- this line is not portable
                                          //     suggest:
                                          //     'int ch;'
                                          //     'while( (ch = getchar()) != EOF && '\n' != ch );'
                                          //     'getchar()'
        return 0;                         // from 'main()' if returned value is 0 then this line not needed
    } // end function: main
    

    注意:'strlen()' 给出了尾随 NUL 字符的偏移量 以便打印 NUL 字符。

    注意:输出(根据您的问题)被重定向到文件。所以不允许光标操作。对 'system("cls" )' 的调用是一个游标操作,因为它没有被输出到终端处理程序,所以它被保存在文件中。这就是您的文件包含意外的“向上箭头”字符的原因。

    【讨论】:

      猜你喜欢
      • 2012-12-04
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      相关资源
      最近更新 更多