我的c程序正在生成不需要的输出

时间:2017-03-25 16:13:24

标签: c string stdout stdin

#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 -> odicatuen)顺序的程序。在左下方的图像中输入文件&amp;右边一个是输出文件。你可以看到开头有一个上箭头

left one is input file & right one is output file

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

2 个答案:

答案 0 :(得分:3)

问题是由对system("cls");

的调用引起的

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

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

答案 1 :(得分:0)

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

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

一些建议:

  1. 每行只有一个语句,并且(最多)每个语句一个变量声明。
  2. 单独的代码块(for,if,else,while,do ... while,switch,case,default by single blank line
  3. 使用适当的水平间距以提高可读性
  4. 变量和参数名称应指明&#39;内容&#39;或者&#39;用法&#39; (或更好,两者)
  5. 始终缩进代码。在每个开口支架后缩进&#39; {&#39;。在每个闭幕式之前取消注意&#39;}&#39;。
  6. 你的编译器应该告诉你关于&#39; gets()&#39;如果没有,那就得到一个现代编译器和/或打开警告。
  7. 现在你的代码:

    #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
    

    注意:&#39; strlen()&#39;给尾随NUL字符赋予偏移量       所以正在打印NUL char。

    注意:输出(根据您的问题)被重定向到文件。所以不允许游标操作。呼叫系统(&#34; cls&#34;)&#39;是一个游标操作,因为它没有输出到终端处理程序,它被保存在文件中。这就是为什么您的文件包含意外的向上箭头&#39;字符。