|
-
Oct 7th, 2002, 03:05 AM
#1
Thread Starter
Lively Member
eVC++ String manipulation **RESOLVED**
Hi there,
i am hoping someone can help me, i am writing a proggy in eVC++ the problem i have is i am trying to manipulate an array of character. i am setting the value of the array to NULL and then reading in the values from a file (17 characters at a time, though this causes problems and i find that 18 characters which includes the CR/LF characters on the end of the line) and placing the data from the file into the array.
then i want to append a few more characters on the end of this string. the problem i have, is that even though i initialise the array (as far as i know) the last few characters that appear in the array before it is re-initialised remain there and the characters i appent to the string get lost when i write the string to another file.
Below is a copy of the code i have:
void CNewForm::OnbtnComplete()
{int numread, numwritten;
char pList[21];
char NewVal[]="ZX";
(stream2 = fopen("\\temp\\sequence.txt","r+"));
//switch data between files. prove it can be done.
while (!feof(stream2))
{
pList[0]=NULL;
numread = fread( pList, sizeof( char ), 18 , stream2 );
strncat(pList,NewVal,2);
numwritten=fwrite(pList, sizeof(char), 21, stream);
}
fclose(stream);
fclose(stream2);
CDialog::OnOK();
}
note: Stream is opened earlier in the program as a w+ or a+ format depending on whether the file exists or not.
Please help me!
R
Last edited by rabhen; Oct 7th, 2002 at 09:21 AM.
-
Oct 7th, 2002, 09:08 AM
#2
Frenzied Member
Try:
Code:
instead of this which is bad idea
pList[0]=NULL;
use this
memset(pList,0x00,sizeof(pList));
TO clobber the \n char:
Code:
for(char *buf=pList;*buf;buf++) {
if(*buf=='\n') {
*buf=0x00;
break;
}
}
Now you can stick more chars att the end of the array with strcat() and no have an embedded newline.
-
Oct 7th, 2002, 09:20 AM
#3
Thread Starter
Lively Member
thanks Jim,
i did manage to solve this issue a couple of hours back by using the
fprintf(stream, "%s%s\n",pList,NewVal);
seemed to give me the results i needed which is a good thing.
but i will try it out with your suggestions too..
R
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|