Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting Characters into an Array using I/O

Status
Not open for further replies.

oop94

Programmer
Jun 14, 2005
31
US
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!
 
It's a rather strange loop: where is index initialization?
What for yes/strcpy etc, why not the simplest:
Code:
const char yes[] = "yes"; // and so on...
 
Your string is not terminated.
Code:
#define MYSTRINGMAX 12
...
char mystring[MYSTRINGMAX + 1];  // one more than what you expect
...
while (index < MYSTRINGMAX)
...

mystring[index] = 0;  // this is the missing bit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top