Hi,
i'm making a simple texteditor from VB6, and i use Visual C++ as my back end.
i call a function where in I will read another text file and put it in a structure.

this is the function:

void loadR()
{
    ifstream file;
    file.open("C:/CaseStudy/CaseStudy/Rsrvd.txt",ios::in);
    for (i=0; i<44; i++) {
        file.getline(sign[i].RsrvdWrd,13,'\n');
    }
    file.close();
}

sample text file:
alnum
burn
decimal
default
embed
goodbye
ifnot

sample output:
alnum (then there is an extra character displayed in the richtextbox(from vb6), a square)

but when i copy and paste it the square gone.

can some one help me? or rather suggest what to do? thanks

how is sign and RsvrdWrd declared?

As for the extra character -- why don't you try reading the file into std::string object then copying the info to the RsvrdWrd.

void loadR()
{
ifstream file;
std::string line;
file.open("C:/CaseStudy/CaseStudy/Rsrvd.txt",ios::in);
for (i=0; i<44; i++) {
   getline(file, line);
   strcpy_s(sign[i].RsvrdWrd, 12, line.c_str());
//file.getline(sign[i].RsrvdWrd,13,'\n');
}

it is declraded as
struct Reserved{
char RsrvdWrd[12];
}sign[44];

also, when i use std, it gives an error

it gives what error? Did you include <string> header file?

If RsvrdWord is an array of 12 characters why are you tring to read in 13 on your getline()?

because, the 12 characters are for the array RsrvdWrd and the 1 character is for the delimiter.

getline() doesn't work that way. The second parameter is the maximum number of characters to be read, including the null terminating character. The third parameter, if it exists, if the character that you want getline() to stop reading when it is found. That character is NOT added to the character array (RsvdWrd).

Are there any words in that file that are longer than 11 characters? If there is, then you will have to increase the size of RsvrdWrd array so that it can accommodate the largest word in the file.

the largest is 8 characters, so in array the max is 7, so do i need to put 9 as my maximum number?

That is entirely dependent upon you. What if you add a word later that exceeds your limit? I would suggest you allocate the memory dynamically so that you don't have to worry about what array size will work best.
Anyway, If the largest has 8 characters then you need an array to fit all 8 because as Ancient Dragon already stated
"The third parameter, if it exists, if the character that you want getline() to stop reading when it is found. That character is NOT added to the character array (RsvdWrd)."

That is entirely dependent upon you. What if you add a word later that exceeds your limit? I would suggest you allocate the memory dynamically so that you don't have to worry about what array size will work best.
Anyway, If the largest has 8 characters then you need an array to fit all 8 because as Ancient Dragon already stated
"The third parameter, if it exists, if the character that you want getline() to stop reading when it is found. That character is NOT added to the character array (RsvdWrd)."

No, you need an array that will hold 9. 8 chars plus a NULL. To prevent errors/faults, the NULL must be within the boundaries of the array.

Of course! Once the delim character is extracted and discarded (presuming third parameter is used) the NULL character is still appended to the end of the array. Apologies for causing confusion

If the longest word is only 8 characters then you can put any nunber you want that is equal to or larger than 9. You don't have to specify a value that exactly matches the length of the largest character, a larger number is perfectly ok. The number needs to match the number of characters that RsvrdWrd can hold. Since you defined it as 12 then it should be ok.

I suspect this is to do with the way in which C interprets newline statements. Look into memory at debug time and find out how the newline character is being represented (CrLf (CarriageReturn LineFeed) for VB). In memory, (CarriageReturn) '/r' is 0D and (LineFeed) '/n' is 0A.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.