我想读几个数字stdin,每行一个数字.我想忽略任何行上的数字和字符串之后的任何尾随文本.为了实现这一点,我使用了以下代码:
while (getline(cin, str)) {
num = strtol(str.c_str(), NULL, 0);
if (errno != ERANGE && errno != EINVAL) {
arr[i++] = num;
req_pages_size++;
cout << arr[i-1] << "\t";
}
str.clear();
}
Run Code Online (Sandbox Code Playgroud)
ISSUE:转换失败后,errno无法更新成功转换案例的错误值.对于之前未成功的通话,它的价值保持不变.
请让我知道如何处理这个问题?
我是编程人员和C语言的新手,目前正在大学学习。这是一项作业,因此我想避免直接回答,但会更多地遵循正确方向的提示或提示/推动。
我正在尝试使用strtol验证我的键盘输入,更具体地说,测试输入是否为数字。我在这里和其他网站上查看了其他问题,并且按照其他用户的指示进行操作,但并没有帮助我。
根据我对strtol的了解/理解(long int strtol(const char * str,char ** endptr,int base);)如果endptr不是空指针,该函数会将endptr的值设置为第一个字符在号码之后。
因此,如果我要输入84948ldfk,则endptr会指向“ l”,告诉我输入中除了数字以外还有其他字符,这将使其无效。
但是,就我而言,正在发生的事情是,无论我输入什么内容,程序都将返回无效输入。这是我的代码:
void run_perf_square(int *option_stats)
{
char input[MAX_NUM_INPUT + EXTRA_SPACES]; /*MAX_NUM_INPUT + EXTRA_SPACES are defined
*in header file. MAX_NUM_INPUT = 7
*and EXTRA_SPACES
*(for '\n' and '\0') = 2. */
char *ptr;
unsigned num=0; /*num is unsigned as it was specified in the start up code for the
*assignment. I am not allow to change it*/
printf("Perfect Square\n");
printf("--------------\n");
printf("Enter …Run Code Online (Sandbox Code Playgroud) 这是我的 C 代码:
char *ptr = "0xfff1342809062Cac";
char *pEnd;
long long value = strtoll(ptr, &pEnd, 0);
printf("%lld\n", value);
printf("errno: %d\n", errno);
Run Code Online (Sandbox Code Playgroud)
我是用gcc-8.3.0编译的,输出是:
9223372036854775807
errno: 34
Run Code Online (Sandbox Code Playgroud)
我很困惑 strtoll 给出了一个意想不到的值并将 errno 设置为 34。
我在这里strtoll()遇到的主要问题是在VC 2010(error C3861: 'strtoll': identifier not found)中被标记为错误.如果我用它替换它会做同样的事情strtol()吗?
unsigned int get_uintval_from_arg(int argc, int index, char **argv,
unsigned int lower_bound, unsigned int upper_bound)
{
unsigned int return_val=0;
if (index + 1 <= argc - 1)
{
return_val=(unsigned int)strtoll(argv[index+1],NULL,10);
if (errno == EINVAL || errno== ERANGE)
{
fprintf(stderr, "Could not parse argument %s for switch %s!\n",
argv[index], argv[index+1]);
return 0;
}
}
// ....... I will post the remaining part of the code if necessary
.......
}
Run Code Online (Sandbox Code Playgroud) 第二个论点如何运作strtol?
这是我尝试过的:
strtol(str, &ptr, 10)
Run Code Online (Sandbox Code Playgroud)
其中ptr是 achar *并且str是一个字符串。现在,如果我传入stras'34EF'和 print *ptr,它会正确地给我E, 并*(ptr+1)给我F,但是如果我 print ptr,它会给我EF!打印不应该ptr导致像十六进制地址之类的垃圾值吗?
我试图在32位机器上进行十六进制到整数转换.这是我正在测试的代码,
int main(int argc,char **argv)
{
char *hexstring = "0xffff1234";
long int n;
fprintf(stdout, "Conversion results of string: %s\n", hexstring);
n = strtol(hexstring, (char**)0, 0); /* same as base = 16 */
fprintf(stdout, "strtol = %ld\n", n);
n = sscanf(hexstring, "%x", &n);
fprintf(stdout, "sscanf = %ld\n", n);
n = atol(hexstring);
fprintf(stdout, "atol = %ld\n", n);
fgetc(stdin);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
strtol = 2147483647 /* = 0x7fffffff -> overflow!! */
sscanf = 1 /* nevermind */
atol = 0 /* nevermind …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 MFC 项目中进行从十六进制字符串到整数的转换。代码是这样的:
CString sMask = "0xFFFFFFE0";
char* pMaskBuffer = sMask.GetBuffer(sMask.GetLength());
sMask.ReleaseBuffer();
char * p = NULL;
long iMask = strtol(pMaskBuffer, &p, 16);
Run Code Online (Sandbox Code Playgroud)
当 sMask 变量很小时,代码运行良好。但是 4 字节掩码生成了奇怪的值。而不是 4294967264 ,我得到 2147483647. 如何克服这个。请帮忙。
我需要总结一些不同的数字char*.
char类似于"12,38,40"结果为90的位置,但也可以是"12/5?10,-20"结果为7的位置.
我已经拥有的代码是:
extern int Add()
{
char *string = "ab230c,i d*(s370*(20k-100d";
char *totaal = string;
int Sum = 0;
while (*totaal)
{
if (isdigit(*totaal))
{
int getal = strtol(totaal, &totaal, 10);
printf("%ld\n", getal);
if(getal >= 0)
{
Sum += getal;
}
}
else
{
totaal++;
}
}
printf("the sum of all numbers is: %d\n", Sum);
return Sum;
}
Run Code Online (Sandbox Code Playgroud)
除负数之外,它完全处理所有事情,它只是忽略-并添加10.
有人知道如何解决这个问题吗?我无法理解它.
我目前正在为我的大学的实验室练习制定银行终端计划.
让我摆脱困境的是一个假定用户输入金额的函数,检查它是否符合所有要求,如果是,则返回提供给程序的值.
我们的导师对保护输入的所有方法都很生气,因此我必须在任何不一致的情况下调用错误.价值太高了?错误.负值还是零值?错误.数字比0.01精确?错误.输入中的非数字非点字符?错误.
因此,我的功能肯定过于复杂,但我很好.是什么驱使我走上墙,事实是,两者atof()和strtod()函数都以某种错误的方式读取数字.
long double take_amount()
{
char input[21];
bool success, correct;
unsigned long int control1;
long double amount, control, control2, control3;
do
{
success = true, correct = true;
printf("\t\tAmount:\t\t");
success = scanf("%20[^ \t\n]c", input);
__fpurge(stdin);
correct = check(input, 'b');
amount = strtod(input, NULL);
printf("\n\tGOT %.20Lf\n", amount); ///
control = amount * 100;
control1 = (unsigned long int)floor(control);
control2 = (long double) control1;
control3 = control2 - control;
printf("\n\tGOT2 %.20Lf\n", control3); ///
if (control3 …Run Code Online (Sandbox Code Playgroud) 我正在将一个mac地址的字符串表示转换为一个UINT8定义为的数组unsigned char.我好奇为什么当我读入常规32位s 的数组时读入s和实际值sscanf()的数组时会读取全0 .它几乎就像是砍掉了int的错误结束的8位.UINT8int
char *strMAC = "11:22:33:AA:BB:CC";
typedef unsigned char UINT8;
UINT8 uMAC[6];
int iMAC[6];
sscanf( (const char*) strMac,
"%x:%x:%x:%x:%x:%x",
&uMAC[0], &uMAC[1], &uMAC[2], &uMAC[3], &uMAC[4], &uMAC[5] );
printf( "%x:%x:%x:%x:%x:%x",
uMAC[0], uMAC[1], uMAC[2], uMAC[3], uMAC[4], uMAC[5] );
// output: 0:0:0:0:0:0
sscanf( (const char*) strMac,
"%x:%x:%x:%x:%x:%x",
&iMAC[0], &iMAC[1], &iMAC[2], &iMAC[3], &iMAC[4], &iMAC[5] );
printf( "%x:%x:%x:%x:%x:%x",
iMAC[0], iMAC[1], iMAC[2], iMAC[3], iMAC[4], iMAC[5] );
// output: 11:22:33:AA:BB:CC
Run Code Online (Sandbox Code Playgroud)
更新:%hhx将适用于C99及以上,但我有一个旧的代码库,所以我最终得到strtoul():
char *str = …Run Code Online (Sandbox Code Playgroud)