在C中读取字符串 [英] Reading strings in C

查看:69
本文介绍了在C中读取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正在使用C gets(),并且正在从用户读取一个字符串,但是我不知道我需要多大的缓冲区,并且输入可能非常大。
有什么方法可以确定用户输入的字符串有多大,然后分配内存,然后将其放入变量中?或者至少是一种在不知道输入有多大的情况下接受输入的方法,有可能它不适合我已经分配的缓冲区。

If I was using C gets(), and I was reading a string from the user, but I have no idea how big of a buffer I need, and the input could be very large. Is there a way I can determine how large the string the user inputted was, then allocate memory and then put it in the variable? Or at least a way to accept input without knowing how large it is, with a possibility it won't fit in the buffer i've already allocated.

推荐答案

我认为使用适当的中间缓冲区,并通过将字符串长度限制为最大,使用 fgets 或其他函数将字符串输入到其中缓冲区大小。稍后在输入字符串时。计算字符串长度并分配一个与字符串大小相同的缓冲区,然后将其复制到新分配的缓冲区中。可以将旧的大缓冲区重新用于此类输入。

I think use an intermediate buffer which is suitably large, and input the string into it with fgets or other function by limiting the string length to the max buffer size. Later when the string is input,. calculate the string length and allocate a buffer of the size of the string and copy it into the newly allocated buffer. The old large buffer can be reused to such inputs.

您可以执行以下操作:

fgets(缓冲区,BUFSIZ,stdin);

scanf(%128 [^ \n]%* c,缓冲区);

此处您可以指定缓冲区长度为128个字节,为%128 .. ,还包括字符串中的所有空格。

Here you can specify the buffer length 128 bytes as %128.. and also include all the blankspace within the string.

然后计算长度并分配新的缓冲区,其中:

And then calculate the length and allocate new buffer with:

len = strlen (buffer);
string = malloc (sizeof (char) * len + 1);
strcpy (string, buffer);
.
.
.
free (string);

编辑

这是我解决问题的一种方法:

Here is one way i worked out:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void)
{
  char *buffer[10];  /* temporary buffers 10 nos, or make this dynamically allocated */
  char *main_str;    /* The main string to work with after input */
  int k, i=0, n, retval;

  while (1)
  {
    buffer[i] = malloc (sizeof (char) * 16); /* allocate buffer size 16 */
    scanf ("%15[^\n]%n", buffer[i], &n);     /* input length 15 string + 1 byte for null */
    if (n<16)                                /* Buffer is not filled and end of string reached */
      break;
    n=0;                                     /* reinitialize n=0 for next iteration. to make the process work if the length of the string is exactly the sizeof the buffer */
    i++;
  }
  /* need to fix the while loop so that the buffer array does not overflow and protect it from doing so */

  /* allocate buffer of exact size of the string */
  main_str = malloc (sizeof (char) * 16 * i + strlen (buffer[i]));

  /* copy the segmented string into the main string to be worked with 
   * and free the buffers
   */
  strcpy (main_str, "");
  for (k=0; k<=i; k++)
  {
    strcat (main_str, buffer[k]);
    free (buffer[k]);
  }

  /* work with main string */
  printf ("\n%s", main_str);

  /* free main string */
  free (main_str);

  return 0;
}

在某些情况下,您需要修复代码以防止崩溃,但这应该回答您的问题。

You need to fix the code to stop crashing in some cases, but this should answer your question.

这篇关于在C中读取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆