C ++ read into c-style one char string at a time?

in C ++ id likes to read the input to the c-style style string at a time. how do you do this without creating a char array with a given size (you don't know how many characters the user enters). And since you cannot resize the array, how is this done? I thought about it, but it does not work.

char words[1]; int count = 0; char c; while(cin.get(c)) { words[count] = c; char temp[count+1]; count++; words = temp; delete[] temp; } 
+5
source share
6 answers

Since you cannot use std::vector , I assume that you cannot use std::string . If you can use std::string , you can provide a solution @ilia's answer .

Without this, your only option is:

  • Use a pointer pointing to dynamically allocated memory.
  • Watch the size of the selected array. If the number of characters to be stored exceeds the current size, increase the size of the array, allocate new memory, copy the contents from the old memory to the new memory, delete the old memory, use the new memory.
  • Delete the allocated memory at the end of the function.

Here is what I suggest:

 #include <iostream> int main() { size_t currentSize = 10; // Always make space for the terminating null character. char* words = new char[currentSize+1]; size_t count = 0; char c; while(std::cin.get(c)) { if ( count == currentSize ) { // Allocate memory to hold more data. size_t newSize = currentSize*2; char* temp = new char[newSize+1]; // Copy the contents from the old location to the new location. for ( size_t i = 0; i < currentSize; ++i ) { temp[i] = words[i]; } // Delete the old memory. delete [] words; // Use the new memory words = temp; currentSize = newSize; } words[count] = c; count++; } // Terminate the string with a null character. words[count] = '\0'; std::cout << words << std::endl; // Deallocate the memory. delete [] words; } 
+6
source

You requested a C-style array. A stack or dynamic allocation will not serve you in this case. You need to change the counter of the array number each time you add a new element, which is impossible automatically. You have to work and delete and reserve the array every time a new chae is read. Therefore, you have options:

  • Use std::vector (which was created for this purpose)
  • Duplicate what's inside std :: vector and write it during your code (which seems horrible)

std::vector solution:

 std::vector<char> words; words.reserve(ESTIMATED_COUNT); // if you you do not the estimated count just delete this line char c; while(cin.get(c)){ words.push_back(c); } 
+2
source
 #include <iostream> #include <string> using namespace std; int main() { string s1; char c; while (cin.get(c)) { if (c == '\n') continue; s1 += c; cout << "s1 is: " << s1.c_str() << endl; //s1.c_str() returns c style string } } 
+2
source

You have two ways: first use a zero-size array, after each input you delete the array and select a new one that is greater than +1, and then save the input. It uses less memory but is inefficient. (In C, you can use realloc to increase efficiency)

The second is to use a buffer, for example, you save data entry in an array of a fixed size, and when it is full, you add a buffer to the end of the main array (by deleting and redistributing).

By the way, you can use std::vector , which automatically and effectively increases the size of itself.

+1
source

If you use c-style strings, then:

 char* words; int count = 0; char c; while(cin.get(c)) { // Create a new array and store the value at the end. char* temp = new char[++count]; temp[count - 1] = c; // Copy over the previous values. Notice the -1. // You could also replace this FOR loop with a memcpy(). for(int i = 0; i < count - 1; i++) temp[i] = words[i]; // Delete the data in the old memory location. delete[] words; // Point to the newly updated memory location. words = temp; } 

I would do what Humam Helfawi suggested and use std::vector<char> , since you are using C ++, this will make your life easier. The implementation above is basically a less elegant version of the vector. If you do not know the size before hand, you will have to resize the memory.

+1
source

You need to allocate a string buffer of arbitrary size. Then, if the maximum number of characters is reached after adding, you need to increase the buffer using realloc.

To avoid calling realloc for each character, which is not optimal, a growth strategy is recommended, for example, doubling the size of each distribution. There are even more subtle growth strategies that are platform dependent.

Then, at the end, you can use realloc to clip the buffer to the exact number of bytes added if necessary.

0
source

Source: https://habr.com/ru/post/1244716/


All Articles