[英]How can I scan a string with unknown size and values to use inside a function in C using gets()?
I am trying to convert a number from base b to base 10 using the function base_b_to_10 as below:我正在尝试使用 function base_b_to_10 将数字从基数 b 转换为基数 10,如下所示:
long int base_b_to_10(char* B, long int base) //take in the number as string returns it converted to base 10 as long int
{
long int N; //base 10 number:
N = strtol(B, NULL, base); //
return(N);
}
In the main program, the user will input the base of the input number and the input number itself.在主程序中,用户将输入输入数字的基数和输入数字本身。 The length and the size of the string is unknown and is up to the user to define This is a simplified version of a big program I am trying to build in which the size of the string B is dynamically allocated in another function so I cannot do something like:
字符串的长度和大小是未知的,由用户定义这是我正在尝试构建的一个大程序的简化版本,其中字符串 B 的大小在另一个 function 中动态分配,所以我不能这样做就像是:
char B[100];
How to solve this?如何解决这个问题? the compiler is returning nothing.
编译器什么也不返回。
Ok so I solved this problem by first using a buffer string with size 50 (pretty sufficient), than I used the strlen() function to determine the size of the string given by the user, and lastly I allocate this number to the string I will be using for the conversion:好的,所以我首先使用大小为 50(足够)的缓冲区字符串解决了这个问题,然后我使用 strlen() function 来确定用户给出的字符串的大小,最后我将此数字分配给字符串 I将用于转换:
char *str, buffer[50];
printf("enter the input value: ");
gets(buffer);
int i;
i = strlen(buffer);
str = (char*)malloc(i*sizeof(char));
strcpy(str, buffer);
fflush(stdin);
N = base_b_to_10(str, base_in);
Input string (here: test
) can be of any reasonable size.输入字符串(这里:
test
)可以是任何合理的大小。 Anyway, there might be some limitations, see: Strtol() and atol() do not convert strings larger than 9 digits无论如何,可能会有一些限制,请参阅: Strtol() 和 atol() 不会转换大于 9 位的字符串
Output string is a pointer (here: *str
) to position within input string (here: test
). Output 字符串是指向输入字符串中 position 的指针(此处为
*str
)(此处为: test
)。 According to this, the size of the string to which str
points does not matter.据此,
str
指向的字符串的大小无关紧要。 (Note: In this example, str
is only valid as long as test
is valid). (注意:在本例中,
str
仅在test
有效时才有效)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long int base_b_to_10(char* B, long int base, char **ret_str)
{
long int N;
N = strtol(B, ret_str, base);
return N;
}
int main()
{
long int val;
char *str;
char test[50] = "43724325HelloWorld";
val = base_b_to_10(test, 10, &str);
printf("val = %ld\n", val);
printf("str = %s\n", str);
return 1;
}
Notes: I adapted your example just to be working (not improving or simplifying anything).笔记:我调整了你的例子只是为了工作(没有改进或简化任何东西)。 It is probably clear that you could also directly use
strtol
function in the main routine.很明显,您也可以在主程序中直接使用
strtol
function。 Moreover, it does not matter, how the input string was allocated (fixed size or dynamic size) - just needs to be allocated and needs to end by \0
(as usual).此外,输入字符串的分配方式(固定大小或动态大小)无关紧要 - 只需要分配并且需要以
\0
结尾(像往常一样)。
If cross platforming is not an issue, and assuming you are running on a linux system, you can make use of asprintf() .如果跨平台不是问题,并且假设您在 linux 系统上运行,则可以使用asprintf() 。
It basically formats your string and allocates the memory for you (remember to free it afterwards).它基本上格式化您的字符串并为您分配 memory (记得之后释放它)。
This is not part of the standard library so it has its limitations.这不是标准库的一部分,因此有其局限性。
#include <stdio.h>
int main ()
{
int cx = snprintf ( NULL, 0, "18 characters long" );
printf("%d", cx);
return 0;
}
// OUTPUT:
// 18
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.