简体   繁体   English

如何使用gets()扫描具有未知大小和值的字符串以在C中的function中使用?

[英]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.这不是标准库的一部分,因此有其局限性。


  • Another approach is to use snprintf() to preddict the size of the output buffer then and use malloc()另一种方法是使用snprintf()来预测 output 缓冲区的大小,然后使用 malloc()
#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.

相关问题 当我在c中使用套接字编程使用大型文件时,性能如何提高 - how does the performance gets increased when I use large size files using socket programming in c 如何使用 for 循环在 C 中打印可变大小的“字符串”? - How can I print a variable size “string” in C using a for loop? 我在字符串中有问题,在使用 get 函数时,在 c 语言中 - I have problem in string, in using the gets function, in c language 如何在我的 C 程序中多次使用“获取”function? - How can I use the “gets” function many times in my C program? 我可以在C字符串中使用什么值? - What values can I use in a C - string? 我如何在主 function 中使用来自 function 原型的值? - 组合单独的值 - How can I use the values from function prototypes inside the main function? - Combining separate values 如何使用读取函数,大小未知 - How to use the read function, with an unknown size 如何编译使用不推荐使用的功能(例如gets())的C程序 - How can I compile C programs that use deprecated functions, such as gets() 如何解决 C 中的 gets() function 的问题? - How can I fix the problem with gets() function in C? 如何在C中创建一个带有未知类型参数的函数? - How can I create a function that takes a parameter of unknown type in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM