gpt4 book ai didi

c - 额外的换行符来自哪里?

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:06 24 4
gpt4 key购买 nike

<分区>

我想知道当我的程序接受一些用户输入并将它们打印回标准输出时,额外的空间来自哪里。我有一个程序如下:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "dbg.h"

#define MAX_DATA 100

int read_string(char **out_string, int max_buffer){
*out_string = calloc(1, max_buffer + 1);
check_mem(*out_string);

char *result = fgets(*out_string, max_buffer, stdin);
check(result != NULL, "Input error.");

return 0;

error:
if(*out_string) free(*out_string);
*out_string = NULL;
return -1;
};

int read_int(int *out_int){
char *input = NULL;
int rc = read_string(&input, MAX_DATA);
check(rc == 0, "Failed to read number.");

*out_int = atoi(input);
free(input);
return 0;

error:
if(input) free(input);
return -1;
}

int read_scan(const char *fmt, ...){
int i = 0;
int rc = 0;
int *out_int = NULL;
char *out_char = NULL;
char **out_string = NULL;
int max_buffer = 0;

va_list argp;
va_start(argp, fmt);

for(i = 0; fmt[i] != '\0'; i++) {
if(fmt[i] == '%'){
i++;
switch(fmt[i]){
case '\0':
sentinel("Invalid, format, you ended with %%.");
break;
case 'd':
out_int = va_arg(argp, int *);
rc = read_int(out_int);
check(rc == 0, "Failed to read int.");
break;
case 'c':
out_char = va_arg(argp, char *);
*out_char = fgetc(stdin);
break;
case 's':
max_buffer = va_arg(argp, int);
out_string = va_arg(argp, char **);
rc = read_string(out_string, max_buffer);
check(rc == 0, "Failed to read string.");
break;
default:
sentinel("Invaid format.");
} //end switch
}
else{
fgetc(stdin);
} // end if
check(!feof(stdin) && !ferror(stdin), "Input error.");
} // end for
va_end(argp);
return 0;

error:
va_end(argp);
return -1;
} // end read_scan

int main(int argc, char *argv[]){
char *first_name = NULL;
char initial = ' ';
char *last_name = NULL;
int age = 0;

printf("What's your first name? ");
int rc = read_scan("%s", MAX_DATA, &first_name);
check(rc == 0, "Failed first name.");

printf("What's your initial? ");
rc = read_scan("%c\n", &initial);
check(rc == 0, "Failed initial.");

printf("What's your last name? ");
rc = read_scan("%s", MAX_DATA, &last_name);
check(rc == 0, "Failed last name.");

printf("How old are you? ");
rc = read_scan("%d", &age);

printf("---- RESULTS ----\n");
printf("First Name: '%s'", first_name);
printf("Initial: '%c'\n", initial);
printf("Last Name: '%s'", last_name);
printf("Age: %d\n", age);

free(first_name);
free(last_name);
return 0;

error:
return -1;
}

现在我在 shell 中执行程序:

%./ex25
What's your first name? a
What's your initial? b
What's your last name? g
How old are you? 11
---- RESULTS ----
First Name: 'a
'Initial: 'b'
Last Name: 'g
'Age: 11

注意'g'后面有一个换行符,它是从哪里来的?

编辑 1:添加了调试宏的定义。

#define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)

#define check(A, M, ...) if(!(A)) {log_err(M, ##__VA_ARGS__); errno=0; goto error;}

编辑 2:glglgl 提到据我所知,read-scan() 中重复的 va_end() 不符合标准。那么符合标准的解决方案是什么?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com