strtol

Do I need to cast the result of strtol to int?

南笙酒味 提交于 2019-12-07 09:59:35
问题 The following code does not give a warning with g++ 4.1.1 and -Wall . int octalStrToInt(const std::string& s) { return strtol(s.c_str(), 0, 8); } I was expecting a warning because strtol returns a long int but my function is only returning a plain int . Might other compilers emit a warning here? Should I cast the return value to int in this case as a good practice? 回答1: You may need the -Wconversion flag to turn these warnings on. However, it won't warn about long -> int , since they are the

C - Comparing numeric strings

独自空忆成欢 提交于 2019-12-07 01:01:20
问题 Out of professional curiosity, what is the safest / fastest / most efficient way to compare two fully numeric strings in C? #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char str1[5] = "123"; char str2[5] = "123"; char *ptr; if(atoi(str1) == atoi(str2)) printf("Equal strings"); if(strtol(str1,&ptr,10) == strtol(str2,&ptr,10)) printf("Equal strings"); if(strcmp(str1,str2)==0) printf("Equal strings"); return 0; } 回答1: strcmp () in my opinion, as it does not need any

std::atoll with VC++

▼魔方 西西 提交于 2019-12-06 21:44:33
问题 I have been using std::atoll from cstdlib to convert a string to an int64_t with gcc. That function does not seem to be available on the Windows toolchain (using Visual Studio Express 2010). What is the best alternative? I am also interested in converting strings to uint64_t . Integer definitions taken from cstdint . 回答1: MSVC have _atoi64 and similar functions, see here For unsigned 64 bit types, see _strtoui64 回答2: use stringstreams ( <sstream> ) std::string numStr = "12344444423223"; std:

Complex algorithm to extract numbers/number range from a string

我只是一个虾纸丫 提交于 2019-12-06 16:29:42
问题 I am working on a algorithm where I am trying the following output: Given values/Inputs: char *Var = "1-5,10,12,15-16,25-35,67,69,99-105"; int size = 29; Here "1-5" depicts a range value, i.e. it will be understood as "1,2,3,4,5" while the values with just "," are individual values. I was writing an algorithm where end output should be such that it will give complete range of output as: int list[]=1,2,3,4,5,10,12,15,16,25,26,27,28,29,30,31,32,33,34,35,67,69,99,100,101,102,103,104,105; If

Do I need to cast the result of strtol to int?

半腔热情 提交于 2019-12-05 18:03:55
The following code does not give a warning with g++ 4.1.1 and -Wall . int octalStrToInt(const std::string& s) { return strtol(s.c_str(), 0, 8); } I was expecting a warning because strtol returns a long int but my function is only returning a plain int . Might other compilers emit a warning here? Should I cast the return value to int in this case as a good practice? You may need the -Wconversion flag to turn these warnings on. However, it won't warn about long -> int , since they are the same size with GCC (the value won't change because of the conversion). But it would if you convert for

std::atoll with VC++

别说谁变了你拦得住时间么 提交于 2019-12-05 01:36:22
I have been using std::atoll from cstdlib to convert a string to an int64_t with gcc. That function does not seem to be available on the Windows toolchain (using Visual Studio Express 2010). What is the best alternative? I am also interested in converting strings to uint64_t . Integer definitions taken from cstdint . MSVC have _atoi64 and similar functions, see here For unsigned 64 bit types, see _strtoui64 use stringstreams ( <sstream> ) std::string numStr = "12344444423223"; std::istringstream iss(numStr); long long num; iss>>num; use boost lexical_cast ( boost/lexical_cast.hpp ) std::string

Complex algorithm to extract numbers/number range from a string

我的梦境 提交于 2019-12-04 22:10:53
I am working on a algorithm where I am trying the following output: Given values/Inputs: char *Var = "1-5,10,12,15-16,25-35,67,69,99-105"; int size = 29; Here "1-5" depicts a range value, i.e. it will be understood as "1,2,3,4,5" while the values with just "," are individual values. I was writing an algorithm where end output should be such that it will give complete range of output as: int list[]=1,2,3,4,5,10,12,15,16,25,26,27,28,29,30,31,32,33,34,35,67,69,99,100,101,102,103,104,105; If anyone is familiar with this issue then the help would be really appreciated. Thanks in advance! My initial

Is there a C function to get permissions of a file?

五迷三道 提交于 2019-12-01 20:26:32
问题 I am writing a c program to be run on UNIX, and attempting to utilize the chmod command. After consulting the man pages, i know that chmod needs two parameters. first is the permission bits, second is the file to be changed. I want to take the bitwise OR of the file's current permission bits and those entered by the user, and feed that to chmod() to change the file's permissions. I found the access() function, but am having trouble figuring out how to use it to get the permission bits of the

Is there a C function to get permissions of a file?

心不动则不痛 提交于 2019-12-01 19:45:30
I am writing a c program to be run on UNIX, and attempting to utilize the chmod command. After consulting the man pages, i know that chmod needs two parameters. first is the permission bits, second is the file to be changed. I want to take the bitwise OR of the file's current permission bits and those entered by the user, and feed that to chmod() to change the file's permissions. I found the access() function, but am having trouble figuring out how to use it to get the permission bits of the specified file. What i have right now is: octalPermissionString = strtol(argv[1], (char**)NULL, 8); if

Using strtol to validate integer input in ANSI C

て烟熏妆下的殇ゞ 提交于 2019-12-01 10:39:09
I am new to programming and to C in general and am currently studying it at university. This is for an assignment so I would like to avoid direct answers but are more after tips or hints/pushes in the right direction. I am trying to use strtol to validate my keyboard input, more specifically, test whether the input is numeric. I have looked over other questions on here and other sites and I have followed instructions given to other users but it hasn't helped me. From what I have read/ understand of strtol (long int strtol (const char* str, char** endptr, int base);) if the endptr is not a null