简体   繁体   English

atoi函数从缓冲区添加数字

[英]atoi function adds number from buffer

I tried to take 2 integers (smaller than 40000) between a blank like 1232 11232 and then parse it to integer. 我尝试在1232 11232类的空格之间取2个整数(小于40000),然后将其解析为整数。 As you can understand getfirstnum returns the first number. 如您所知, getfirstnum返回第一个数字。 But the problem is that there is something unusual I couldn't understand. 但是问题是,有些异常我无法理解。 When I type 11232 22312 as an input the output must be same 11232 22312 but it is 2231211232 223121232 . 当我键入11232 22312作为输入时,输出必须相同11232 22312但必须是2231211232 223121232 it basically puts the second number in front of first number, it concatenates both numbers and in second one it concatenate the last 4 digits. 它基本上将第二个数字放在第一个数字的前面,将两个数字连接起来,在第二个数字中将最后四个数字连接起来。 Why and how to solve it? 为什么以及如何解决?

PS: I get input with regex to get input with blank (blank is problem in scanf ) it clearly works, I checked it many times. PS:我用正则表达式输入以空格输入( scanf空白是问题),它显然有效,我检查了很多次。 Problem starts in atoi because before atoi the string is parsed well. 问题始于atoi因为在atoi之前字符串已被很好地解析。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int getfirstnum(char *input);
int getsecnum(char *input);

int main() {
    char *input = malloc(40005 * sizeof(char));
    int N, target, i = 0, j = 0, x, y, shot = 0, found = 0;

    scanf(" %[^\n]s", input);

    N = getfirstnum(input);
    target = getsecnum(input);

    printf("%d %d", N, target);
}

int getfirstnum(char *input) {
    int numm, i = 0;
    char num1[40005];

    while (input[i] != ' ') {
        num1[i] = input[i];
        i++;
    }
    fflush(stdout);
    numm = atoi(num1);
    return numm;
}

int getsecnum(char *input) {
    int num, i = 0, j = 0;
    char num2[40005];

    while (input[i] != ' ')
        i++;

    i++;

    while (i < strlen(input)) {
        num2[j] = input[i];
        printf("%c", sayi2[j]);
        i++;
        j++;
    }
    num = atoi(num2);

    return num;
}

Your code shows major confusion between the number value and the number of digits. 您的代码显示数字值和位数之间的主要混淆。 Your code fails because you do not null terminate the buffers after copying digits from the source. 您的代码失败,因为从源复制数字后,您不可以将终止缓冲区的值设置为null。 But your code is way too complicated for the task: parsing numbers can be done easily with sscanf() or strtol() . 但是您的代码对于这项任务来说太复杂了:可以使用sscanf()strtol()轻松地解析数字。 All you need is a buffer to read one line: 您只需要一个缓冲区即可读取一行:

#include <stdio.h>

int main() {
    char line[256];
    int N, target;

    if (fgets(line, sizeof line, stdin)) {
        if (sscanf(line, "%d%d", &N, &target) == 2) {
            printf("%d %d\n", N, target);
        }
    }
    return 0;
}

Or using strtol : 或使用strtol

#include <stdio.h>
#include <stdlib.h>

int main() {
    char line[256];
    int N, target;
    char *p, *q;

    if (fgets(line, sizeof line, stdin)) {
        N = strtol(line, &p, 10);
        target = strtol(p, &p, 10);
        if (p > line && q > p)
            printf("%d %d\n", N, target);
    }
    return 0;
}

Silly mistake :) 愚蠢的错误:)

Your debugging statements for num2 caused the confusion. 您对num2的调试语句引起了混乱。

#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int getfirstnum(char *input);
int getsecnum(char *input);

int main(){
    char *input = malloc(40005 * sizeof(char));
    int N, target, i = 0, j = 0, x, y, shot = 0, found = 0;

    scanf(" %[^\n]s", input);

    N = getfirstnum(input);
    target = getsecnum(input);


    printf("%d %d", N, target);
}

int getfirstnum(char *input){
    int numm, i = 0;
    char num1[40005];

    while (input[i] != ' '){
        num1[i] = input[i];
        i++;
    }
    num1[i] = '\0';  /* null terminate string */

    fflush(stdout);
    numm = atoi(num1);
    return numm;
}

int getsecnum(char *input){
    int num, i = 0, j = 0;
    char num2[40005];

    while (input[i] != ' ')
        i++;

    i++;

    printf("debugging of num2\n");  /* your statements without newline... */
    while (i<strlen(input)){
        num2[j] = input[i];
        printf("%c", num2[j]);
        i++;
        j++;
    }
    num2[j] = '\0';  /* null terminate string */
    printf("end debugging of num2\n");  

    num = atoi(num2);

    return num;
}

... attention to detail... ...注重细节...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM