Results 1 to 3 of 3

Thread: eVC++ String manipulation **RESOLVED**

  1. #1

    Thread Starter
    Lively Member rabhen's Avatar
    Join Date
    Dec 2001
    Location
    Derby, Derbyshire, UK
    Posts
    79

    Angry 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.

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    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.

  3. #3

    Thread Starter
    Lively Member rabhen's Avatar
    Join Date
    Dec 2001
    Location
    Derby, Derbyshire, UK
    Posts
    79
    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
  •  



Click Here to Expand Forum to Full Width