How do I read a stringstream into a char *[40] / char ** array?

Montana Burr

I am working on creating a UNIX shell for a lab assignment. Part of this involves storing a history of the past 10 commands, including the arguments passed. I'm storing each command as a C++ string, but the parts of the program that actually matter, and that I had no input in designing (such as execve) use char * and char ** arrays exclusively.

I can get the whole command from history, and then read the program to be invoked quite easily, but I'm having a hard time reading into an arguments array, which is a char *[40] array.

Below is the code for a program I wrote to simulate this behavior on a test string:

#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
   char *chars[40];
   string test = "Hi how are you";
   stringstream testStream;
   testStream << test;
   int i = 0;
   while (true)
   {
      string test_2;
      testStream >> test_2;
      if (testStream.fail())
      {
         break;
      };
      chars[i] = (char *)test_2.c_str();
      i++;
   }
   for (int i=0; i < 4; i++)
   {
      cout << chars[i];
   }
   cout << "\n";
}

I get the feeling it has something to do with the array being declared as an array of pointers, rather than a multi-dimensional array. Am I correct?

Paul Sanders

This line:

chars[i] = (char *)test_2.c_str();

leaves chars[i] 'dangling' when you go back round the loop or fall off the end. This is because test_2.c_str() is only valid while test_2 is in scope.

You'd do better to do something like this:

#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include <memory>

int main()
{
   std::vector <std::string> args;
   std::string test = "Hi how are you";
   std::stringstream testStream;
   testStream << test;
   int i = 0;

   while (true)
   {
      std::string test_2;
      testStream >> test_2;
      if (testStream.fail())
         break;
      args.push_back (test_2);
      i++;
   }

    auto char_args = std::make_unique <const char * []> (i);
    for (int j = 0; j < i; ++j)
        char_args [j] = args [j].c_str ();

    for (int j = 0; j < i; ++j)
        std::cout << char_args [j] << "\n";
}

Now your vector of strings remains in scope while you are building and using char_args.

Live demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I return a char array of an unknown value?

How do I find a character followed by a character in a char array Java?

How do I create a type alias for an array of char with a fixed size?

How to convert vector of char to stringstream

How do I receive a char array in a C function?

How do I create a file descriptor backed by a simple char array?

How do I read a C char array into a python bytearray with cython?

How do I tokenize a char array input into a char and a string?

How do I format the values of an array of `unsigned char` in hexadecimal?

How do I change 2+ values in a char array?

How do I add String to a char array?

How do I get the number of elements a char array points to?

How do I read a string char by char in C++?

How do I properly use strcat for char array?

How do I compare a a string array with a char array

How do I return a pointer to something in the middle of a char array?

How do I convert an unsigned char array into a string in C?

How do I manually enter characters in a char[ ] array?

How do I initialize a char array with a memory address in C?

how do I free this char** array?

How do I correctly work with char pointer to array in C++?

Binary char array into stringstream and pop from the buffer

How to read from a char array (array of names)?

How do I cast /copy from a char array so that I may store it in a char pointer array?

How do I tokenize a char array twice to turn it into a 3D char array?

How do I convert a long into a char[] array in java?

How do I insert char* to array of structures?

How do I create an array of pointers of type char C++?

How do I use tolower() with a char array?