Tuesday, July 19, 2011

C string


C stringTo make this simple, the only reason it's displaying 0 is the fact that you are only accessing the last memory section of the string array that you're making and you keep writing over it in each iteration of your loop so you end up with only the last value of your file.As a short lesson on arrays, arrays are safe temporary pointers, they allocate the size of whatever you're making the array of in memory space (in the case of strings you're making it the size of the string class in 6 slots side-by-side in memory) int array[6]; // is the same as declaring six separate integersTo access the first value of an array you start with 0

first value would be accessed using :

array[0];

and the last is accessed using :

array[5]; //one less than the number you originally used to allocate its size because the memory space begins with 0

Strings have the capability to resize themselves when you give them a new value (a string is a class that holds a resizeable const char array) so what you might want to do if you are only taking bool-type information in the file is just resize the string and add the new value in. (Or alternatively you can catenate like I did below.)
Share/Bookmark

No comments:

Post a Comment