Can you break and store a string up into an array of characters?
Can you break and store a string up into an array of characters?
What if the string is dynamic?
Like that maybe?Code:char *buf; string mystring = "Hello World, I have no clue how long this is!"; buf = new char[mystring.length() + 1]; strcpy(buf, mystring.c_str());
I've tried to do this but I don't know where I am going wrong. Basically I am trying to read two lines of characters from an input file. The input file is called in like this: a.out < inputfile
For those of you who are not familar with this, this just mean you are feeding it through a file rather than through the keyboard. So
will just make the program read the first integer and so forth.... My problem is this: I cannot read in the two character arrays successfully. The contents of the sample input file is as follows:Code:int some_value; cin << some_value
I need to read the first line into the first character array and the second line into the second character array. Please please help.Code:ABSDEF SDEFEA
will read one line at a time and store it in buffer.Code:cin.getline(buffer,'\n'); //or you can also put a limit on the size cin.getline(buffer,sizeof(buffer)-1,'\n');
PHP and XML
Let's talk about SAX
Great! That worked. Now I just need to get the length of one of the arrays. It could be either one since both have the same length.
Thanks to everyone on this post I have this much to read in from the input file to the corresponding arrays. I do have one problem though and that is how to get the length of each of the arrays. The way that I have tried below is to use Waldo2K2's code(thank you). I made two arrays and now want to travel through them until I reach a delimiter, in this case, '\n'. Seems logical enough but its just not working.
Code:int main(){ char preorder[26]; char inorder[26]; // store values from input file into arrays cin.getline(preorder, '\n'); cin.getline(inorder, '\n'); // WHY DOESN'T THIS BLOCK OF CODE WORK TO TRAVERSE AND // FIND THE LENGTH OF the array(s)?? int i = 0; int length = 0; while (preorder[i] != '\n'){ length++; i++; } cout << "length: " << length << "\n"; cout << "preorder = "; for (int i = 0; i < 6; i++){ cout << preorder[i]; } cout << "\n"; cout << "inorder = "; for (int i = 0; i < 6; i++){ cout << inorder[i]; } cout << "\n"; return 0; }
When you read in a line
you read in until but not including the newline char. What you're looking for is the terminating null '\0'Code:cin.getline(preorder, '\n');
This can also be simplified to:Code:while (preorder[i] != '\0'){ length++; i++; }
Of course you could always use strlen()Code:while (!preorder[i])++i;
-Futura
elaborating even further...
the '\n' not only is seperate from the string, it still resides in the input buffer. Take the following situation.
so, after each cin call, useCode:cin.getline(buffer,'\n'); [input] user types "hello" [/input] cout<<buffer; [output] prints "hello" [/output] cin.getline(buffer2,'\n'); [error] the getline function (or any other cin) looks at the input buffer, and immediatly sees the '\n' and stops reading input, therefore the user has no time to enter a string. [/error] cout<<buffer2; [output] prints nothing [/output]
it will wipe out anything leftover in the console input buffer.Code:cin.flush();
PHP and XML
Let's talk about SAX