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