为什么 atoi 返回随机数?
我正在尝试从文本文件(时间)中读取数据。并将其转换为可以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要以 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.
您忘记以零终止您的
char
缓冲区。应该是
You forgot to terminate your
char
buffers with zero.Should be
atoi 假定其参数是 C 样式字符串,这意味着它必须包含尾随 nul 字符。
您手工制作的伪字符串不能确保这一点。
解决方案是放大它们并将最后一个元素设置为
'\0
: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
:您忘记了 null 终止:
You forgot to null terminate: