Putting Characters into an Array with I/O
I am trying to put characters into an array.
I placed the following in a file called "ultranew.DAT", character by character (with a white space at the end):
C078C75D.DAT
I open the file "ultranew.DAT" and a destination file called "ultranew2.DAT" with the following code:
instream2.open("ultranew.DAT");
outstream2.open("ultranewtwo.DAT", ios::app);
My plan is to get the characters from the file "ultranew.DAT", put them into an array, and print the array to the file "ultranewtwo.DAT"
I try to place "C078C75D.DAT" into an array called "mystring", character by character with the following code:
char next;
char mystring[12];
while (index<12)
{
if (mystring[index] != ' ')
{
instream2.get(next);
outstream2<<next;
mystring[index]=next;
index++;
}
}
However, when I print out the array, it has "no" at the end, and regardless of how large I make the array, "no" always appears at the end. I know that the "no" comes from this part of my code earlier in the program:
char yes[4], no[3];
strcpy(yes, "yes");
strcpy(no, "no");
This is totally unrelated to the array "mystring", so I have no idea how "no" appears at the end of the array. Does anyone know what's going on and/or how I can fix this? Thank you very much for any help!
[1448 byte] By [
oops94] at [2007-11-19 10:08:07]

# 1 Re: Putting Characters into an Array with I/O
On quick inspection, I noticed that your array (char mystring[12];) is not large enough to hold the string ("C078C75D.DAT"). This string is actually 13 characters because you need to have space for the null terminator '\0' Chances are, your "no" array is next to "mystring" in memory. When you do a "printf" or "cout", it just prints until it hits a \0, which would be at the end of the "no" string.
hope that helps.
sulu
# 2 Re: Putting Characters into an Array with I/O
Its difficult to tell what's going on with the code fragments you provide, but initialization of variables is very important in C and C++ programs, and I see a couple of places where this may cause you some problems:
1) You begin your 'while' loop with, while (index<12), but I don't see that you've initialized 'index' to zero before starting this loop. Perhaps you did earlier in the code, but its something to check.
2) You check the value of the 'mystring[index]' before assigning any values to it in the line: if (mystring[index] != ' '). On the first pass through your loop, there's no telling what the value of mystring[index] is, since all you've done is declare the variable 'mystring' but assigned nothing to the array elements. Either some initialization or re-arrangement of the code would help here.
Hope this helps, and good luck ... ghovis
ghovis at 2007-11-11 0:14:16 >

# 3 Re: Putting Characters into an Array with I/O
Thank you for the suggestion! Actually, when I first wrote the code, I sized the array at the appropriate 13 characters, but strangely enough, it prints out as "C078C75D.DAT(white space)(three odd characters)no"
When I size it at 12 characters, it prints out as "C078C75D.DATno"
I certainly understand where you're coming from, but my program is giving me results that don't seem to make sense.
The reason why you get the extra characters when you set it to [13] is because memory blocks are allocated in 4 byte chunks. Going from 12 to 13 actually allocates an extra chunk which brings it to 16.
You need to allocate 13 (or 16 if you prefer ;)) bytes and MAKE SURE you set mystring[12] = '\0'; That way your string will actually be null-terminated.
If you still have problems, post it here. Hope this helps.
sulu