-1

Cのread()関数を使用して、ファイルから一度に1文字ずつ入力を取得する必要がある単純なシェルプログラムを作成しています。ユーザーから入力を取得する唯一の方法は、read()関数です。この部分は、次のような非常に簡単な方法で正しく動作させることができます。

int main( int argc, char *argv[] )
{
int error = 0;
int input_result = 1; /*Integer to track the result of inputs.*/
int input_exit = 0;
char input_buffer[100];
char *buffer_pointer = &input_buffer[0];
char *input;

write( 1, "# ", 2 ); /*Display # to signal the user to input data.*/

int input_counter = 0; /*Int that tracks the number of input characters.*/

/*While loop to continually take input from the user.*/
/*Step one of the programming assignment.*/
while( input_exit == 0 && input_result == 1 && input_counter < 15 && error == 0)
{
    input_result = read( 0, input, (size_t) 1 );

    input_buffer[input_counter] = *input;

    printf( "%c - %d\n", input_buffer[input_counter], input_result );

    input_counter++;
} /*End while for user input*/

write( 1, input_buffer, (size_t) input_counter );
}

入力ファイルを使用すると、これも非常に簡単で、エラーのない適切な出力が得られます。コードをより複雑にし、ネストされたwhileループを追加し始めると、「不正なアドレス」エラーが発生し始めます。エラーをスローしているコードを以下に示します。ちなみに、私のCプログラミングは全体的に最高ではなく、変更できることがありますが、読み取りの問題に焦点を当てたいと思います。よろしくお願いします。

int main( int argc, char *argv[] )
{
/*Infinite loop to keep the shell running until it is implicitly ended by the user.*/
while( 1 )
{
    int error = 0; /*Int to keep track if an error occurred.*/

    char input_buffer[101]; /*Array of chars to hold the input.*/
    char *input_bufferp = &input_buffer[0]; /*Pointer to the first element of the char array.*/
    char *input; /*Char pointer to hold the read input*/
    char *newline = "\n";
    int buffer_counter = 0; /*Int that tracks the number of input characters.*/
    int input_result = 1; /*Int to hold the result of the read.*/

    char input_string[17][64]; /*Array to the parsed input data.*/
    char **input_stringp; /*Pointer to the first element of the string array.*/
    char *input_strings; /*Holds the parsed information before it is organized.*/
    int string_counter = 0; /*Int to track the number of strings.*/


    write( 1, "# ", 2 ); /*Display # to signal the user to input data.*/

    /*While loop to parse the information into separate strings.*/
    /*This while loop contains steps 1 and 2.*/
    while( string_counter == 0 && error == 0)
    {
        /*While to take in information via read.*/
        while( buffer_counter < 100 && input_result == 1 )
        {
            input_result = read( 0, input, (size_t) 1 ); /*Read one char from the user.*/

            /*If statement to signal an input error.*/
            if( input_result == -1 )
            {
                error = 1; /*Signal the error*/
                printf( "\nInput errno: %s\n", (char *)strerror(errno) ); /*Inform the user of the error.*/

                exit(0);
            }/*End if to signal an error.*/

            /*If to handle the end of the file.*/
            else if( input_result == 0 )
            {
                input_buffer[buffer_counter] = '\0'; /*Place the escape char.*/
            }/*End if for end of file.*/

            /*If statement handles a proper read from the user.*/
            else if( input_result == 1 )
            {
                /*If statement to check for a new line.*/
                if( strcmp( input_bufferp[buffer_counter], newline ) == 0 )
                {
                    input_result = 0; /*Set variable to exit the while loop.*/
                    input_buffer[buffer_counter] = '\0'; /*Place the escape char.*/
                }/*End new line if.*/

                /*Else statement to put input into the buffer.*/
                else
                {
                    input_buffer[buffer_counter++] = *input; /*Place the input into the buffer.*/
                }/*End buffer else.*/
            } /*End good input read.*/
        } /*End input gather while loop.*/

        write( 1, input_bufferp, (size_t) buffer_counter ); /*Echo the input to the user*/

        input_strings = strtok( input_bufferp, " \t" ); /*Split the input into tokens.*/

        /*While loop to tokenize the rest of the data.*/
        while( input_strings != NULL )
        {
            input_stringp[string_counter++] = input_strings; /*Store the tokenized string.*/
            input_strings = strtok( NULL, " \t" ); /*Spilt the remain data into tokens.*/
        } /*End tokenizer while loop.*/

        input_stringp[string_counter--][0] = '\0'; /*Please the escape char.*/
    } /*End parsing while loop.*/

    /*Check if the user wants to exit.*/
    if( strncmp( *input_stringp, "exit", 4 ) == 0 )
    {
        exit( 0 ); /*Exit the shell.*/
    } /*End if to exit program.*/


} /*End of infinite while loop.*/
}
4

1 に答える 1

3

変数の値は、何らかの値に設定するまで使用できません。input何かを指すように初期化してから、その値をreadに渡し、特にデータを読み取ることはありません。

データを保存する場所をread指示する2番目のパラメーター。readこれは、1つ以上の文字を格納するために割り当てたスペースを指している必要がありますが、コードではこれを行いません。

に変更inputします。charではなく、に変更しますchar *。これにより、小野文字を格納するためのスペースが割り当てられます。次に、のアドレスinputに渡しますread

于 2012-07-19T03:30:57.763 に答える