为什么 atoi 返回随机数?

发布于 2024-08-19 11:06:56 字数 1826 浏览 10 评论 0原文

我正在尝试从文本文件(时间)中读取数据。并将其转换为可以 DiffTime 到当前系统时间的东西。

我现在已经很接近让它正常工作了,我可以尝到它的味道,但我遇到了一个无法解决的问题。 (我对C语言有非常基本的掌握)。

该程序从文本文件中读取数据,将其分成两个字符数组,然后尝试使用 atoi 将其转换为整数。但是,我在第二个 atoi 调用时遇到问题。

从底部两个 printf 语句,我应该得到:

12  
34

但由于某种原因我得到了一些类似的东西。

12  
3412

我认为 atoi 超出了其中一个 char 数组的边界,或者我的 char 数组太长或太短。不管怎样,我无法理解发生了什么。

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define COPYMODE 0644

int main (int argc, char *argv[]){
    int  i, nRead, fd;

    int  source;
    int  ihour;
    int  imin;

    int  STATE_OK = 0;
    int  STATE_WARNING  = 1;
    int  STATE_CRITICAL = 2;
    int  STATE_UNKNOWN  = 3;
    /* indicates if system is paused 1 = System is paused, 0 = System running */
    int  system_paused  = 0; 

    char filebuf[5]; 
    char hourbuf[2];
    char minbufer[2];

    if((fd = open(argv[1], O_RDONLY)) == -1)
    {
        printf("failed open : %s", argv[1]);
    }
    else
    {
        nRead = read(fd, filebuf, 5);
    }
    close(source);

    printf("filebuffer %s\n", filebuf);

    hourbuf[0] = filebuf[0];
    hourbuf[1] = filebuf[1];

    printf("Hour Buffer %c%c\n", hourbuf[0],hourbuf[1]);

    minbufer[0] = filebuf[2];
    minbufer[1] = filebuf[3];

    printf("Min Buffer %c%c\n", minbufer[0],minbufer[1]);

    imin = atoi(minbufer);
    ihour = atoi(hourbuf);


    printf("hour as int %d\n", ihour);
    printf("min as int %d\n", imin);

    return 0;
}

I am trying to read in data from a text file (the time). and convert that into something that can be DiffTime'ed to the current system time.

I am now so close to getting this working correctly, I can taste it but I am stuck with an issue I cannot work out. (I have a very basic grasp of the C language).

this program reads in the data from the text file, splits it into two char arrays, and then tries to use atoi to convert this into an integer. However, I am getting problems with the second atoi call.

From the bottom two printf statements, I should be getting:

12  
34

but for some reason i am getting something along these lines.

12  
3412

I presume that atoi is overunning the boundries of one of the char arrays or my char arrays are too long or too short. Either way, I cannot fathom what is going on.

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define COPYMODE 0644

int main (int argc, char *argv[]){
    int  i, nRead, fd;

    int  source;
    int  ihour;
    int  imin;

    int  STATE_OK = 0;
    int  STATE_WARNING  = 1;
    int  STATE_CRITICAL = 2;
    int  STATE_UNKNOWN  = 3;
    /* indicates if system is paused 1 = System is paused, 0 = System running */
    int  system_paused  = 0; 

    char filebuf[5]; 
    char hourbuf[2];
    char minbufer[2];

    if((fd = open(argv[1], O_RDONLY)) == -1)
    {
        printf("failed open : %s", argv[1]);
    }
    else
    {
        nRead = read(fd, filebuf, 5);
    }
    close(source);

    printf("filebuffer %s\n", filebuf);

    hourbuf[0] = filebuf[0];
    hourbuf[1] = filebuf[1];

    printf("Hour Buffer %c%c\n", hourbuf[0],hourbuf[1]);

    minbufer[0] = filebuf[2];
    minbufer[1] = filebuf[3];

    printf("Min Buffer %c%c\n", minbufer[0],minbufer[1]);

    imin = atoi(minbufer);
    ihour = atoi(hourbuf);


    printf("hour as int %d\n", ihour);
    printf("min as int %d\n", imin);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤寂小茶 2024-08-26 11:06:56

您需要以 null 终止字符串。将 hourbuf 和 minbufer 延长 1 并在调用 atoi 之前在末尾添加 0。这两个缓冲区在堆栈上相邻,因此读取 minbuf 也会扫描到 hourbuf。

You need to null terminate the strings. Make hourbuf and minbufer 1 longer and put a 0 at the end before calling atoi. The two buffers are adjacent on the stack, so reading minbufer scans into hourbuf as well.

千寻… 2024-08-26 11:06:56

您忘记以零终止您的 char 缓冲区。

char hourbuf[2];
char minbufer[2];

应该是

char hourbuf[3];
char minbufer[3];

hourbuf[2] = '\0';
minbufer[2] = '\0';

You forgot to terminate your char buffers with zero.

char hourbuf[2];
char minbufer[2];

Should be

char hourbuf[3];
char minbufer[3];

hourbuf[2] = '\0';
minbufer[2] = '\0';
动次打次papapa 2024-08-26 11:06:56

atoi 假定其参数是 C 样式字符串,这意味着它必须包含尾随 nul 字符。

您手工制作的伪字符串不能确保这一点。

解决方案是放大它们并将最后一个元素设置为 '\0

char hourbuf[3];
int hour;
...
hourbuf[0] = filebuf[0];
hourbuf[1] = filebuf[1];
hourbuf[2] = '\0';
hour = atoi(hourbuf);

atoi assumes its argument is a C-style string, which means it must contain a trailing nul character.

Your handcrafted pseudostrings don't ensure this.

A solution would be to enlarge them and set the last element to '\0:

char hourbuf[3];
int hour;
...
hourbuf[0] = filebuf[0];
hourbuf[1] = filebuf[1];
hourbuf[2] = '\0';
hour = atoi(hourbuf);
微凉 2024-08-26 11:06:56

您忘记了 null 终止:

minbufer[2] = '\0'

You forgot to null terminate:

minbufer[2] = '\0'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文