How can I read a paragraph in via the terminal as

2019-02-26 03:49发布

问题:

I'm writing a C program that should read in an essay from a user. The essay is divided into multiple paragraphs. I don't know how many lines or characters the essay will be, but I do know that it ends with a hash symbol (#). I want to use only as much memory as is necessary to hold the essay.

Here is what I have tried so far:

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


main(){

    int size;
    char *essay;

    printf("\n how many characters?\n");
    scanf("%d", &size);
    essay =(char *) malloc(size+1);
    printf("Type the string\n");
    scanf("%s",essay);

    printf("%s",essay );

}

As I said before, I don't know (and don't want to ask) about the number of characters beforehand. How do I dynamically allocate memory to save space? (What is dynamic memory allocation?) Is there another way to save memory that doesn't rely on dynamic allocation?

Additionally, my code only reads one line at a time right now. How can I read multiple lines and store them as a single string?

this is another code

#include <stdio.h>    
#include <stdlib.h>    
int main ()
{
  char input;
  int count = 0;
int n;
  char* characters= NULL;
  char* more_characters = NULL;
  do {
     printf ("type the essay:\n");
     scanf ("%d", &input);
     count++;

       more_characters = (char*) realloc (characters, count * sizeof(char));
      if (more_characters!=NULL) {
       characters=more_characters;
       characters[count-1]=input;  }
     else {
       free (characters);
       printf ("Error (re)allocating memory");
       exit (1);
     }
  } while (input!='#');

printf ("the essay: ");
    for (n=0;n<count;n++) printf ("%c",characters[n]);
    free (characters);
   }

it is not working

回答1:

You can read character at a time and copy it into your essay buffer. When your essay buffer runs out of space, you can do a realloc to get another chunk of memory. When your character that you read is a "#" you're done.



回答2:

Hmmm to "not waste space in memory",

then how about excessive calls of realloc()?

char *Read_Paragraph_i(void) {
  size_t size = 0;
  size_t i = 0;
  char *dest = NULL; 
  int ch;
  while ((ch = fgetc(stdin)) != EOF) {
    if (ch == '#') break;
    size++;
    char *new_ptr = realloc(dest, size);
    assert(new_ptr);
    dest = new_ptr;
    dest[i++] = ch;
  }
  size++;
  char *new_ptr = realloc(dest, size+);
  assert(new_ptr);
  dest = new_ptr;
  dest[i++] = '\0';
  return dest;
}

A more sane approach would double the allocation size every time more memory is need, temporally wasting memory and then a final "right-size" allocation.



回答3:

If this can use C++, you can use string (std::string) which will grow as needed as characters are added. If you can't then you will have to use malloc to create an array to hold characters. When it is full, you will have to create a new one, and copy the current data from old to new one, then add the new character. You can do that on each character read to use the minimal amount of memory, but that is WAY too inefficient. A better way is to allocate the character array in chucks, keeping the current size, and the number of characters currently in it. When you want to add another character and the array is full, then you allocate a new one that is some number of characters larger than current one, update current size to size of new one, then add new character.



标签: c string scanf