read()一旦嵌套就返回坏地址

时间:2012-07-19 03:19:38

标签: c

我正在编写一个简单的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.*/
}

1 个答案:

答案 0 :(得分:3)

在将变量设置为某个值之前,不能使用该变量的值。您永远不会将input初始化为指向任何内容,然后将其值传递给read,特别是将数据读取到任何位置。

read的第二个参数告诉read存储数据的位置。它必须指向您已分配用于存储一个或多个字符的空间,而您的代码永远不会这样做。

input更改为char,而不是char *。这将分配空间来存储ono角色。然后将input地址传递给read