-1

我正在编写一个简单的 shell 程序,它必须使用 C 中的 read() 函数一次从文件中获取一个字符。 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告诉read存储数据的位置。它必须指向您已分配用于存储一个或多个字符的空间,而您的代码永远不会这样做。

改为inputa char,而不是a char *。这将分配空间来存储一个字符。然后传递to的地址inputread

于 2012-07-19T03:30:57.763 回答