need help for simple c++ programming

N

news.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.
 
S

Sean Kenwrick

news.hku.hk said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top