need help for simple c++ programming



 DEVELOP > c-Plus-Plus > need help for simple c++ programming

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "news.hku.hk"
Date: 20 Dec 2003 04:41:28 AM
Object: need help for simple c++ programming
As myfile.txt is the required filename, i try to extract the string
"myfile.txt" to variable "filename" of type char from buffer
i1: position of "m" in "myfile.txt"
i2: position of the last "t" in "myfile.txt"
buffer: a character array containing many words in
********************************************** while(i1 < i2) {
strcat( filename, buffer[i1] );
i1++;
}; *********************************************** The above
lines generate error. I think this is because buffer[i1], buffer[i1+1],
...... are some characters without the ending '\0' and therefore cannot be
used in the strcat function. But is there any ways to achieve my purpose? or
i simply go to a completely wrong direction? Thanks for your kind attention.
.

User: "Sean Kenwrick"

Title: Re: need help for simple c++ programming 20 Dec 2003 07:01:45 AM
"news.hku.hk" <billychu@hkusua.hku.hk> wrote in message
news:3fe434c3$1@newsgate.hku.hk...



As myfile.txt is the required filename, i try to extract the string
"myfile.txt" to variable "filename" of type char from buffer
i1: position of "m" in "myfile.txt"
i2: position of the last "t" in "myfile.txt"
buffer: a character array containing many words in
********************************************** while(i1 < i2) {
strcat( filename, buffer[i1] );
i1++;
}; *********************************************** The above
lines generate error. I think this is because buffer[i1], buffer[i1+1],
..... are some characters without the ending '\0' and therefore cannot be
used in the strcat function. But is there any ways to achieve my purpose?

or

i simply go to a completely wrong direction? Thanks for your kind

attention.



buffer[i1] is a char NOT a null terminated string of chars. Remember that
there is no difference between a char and an int (except that a char might
be one or two bytes long instead of two or four bytes long (but you cannot
rely on this)). To do what you are trying to do you could do the
following:
while(i1 < i2) {
filename[i1]= buffer[i1] ; // Assign the array elements char
by char
i1++;
};
Or you could force buffer to be a NULL terminated string and use the string
functions...
buffer[i2]='\0'; // Null terminate the array of chars makeing a 'string'
strcpy(filename,buffer); // Now just use strcpy to copy into the filename
array (null terminated)
Sean
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER