1

I have three string file that will be stored into dynamic array, but I just try one of three file to test if this succed, so I'll do the same way to handle the three file i have. the goal i'll shown the string that I get from the file to a ListView this my code.

void __fastcall TFrmNewPeta::showDefaultRute() {
    std::string lineDataAwal;
    std::ifstream ifs_Awal;
    int tempIndexAwal = 0;
    ifs_Awal.open("DefaultDataAwal");

        /*counting the line*/
        while(std::getline(ifs_Awal,lineDataAwal)){++tempIndexAwal;}

        /*use dynamic array to stored string*/    
        std::string *s = new std::string[tempIndexAwal];

        for(int dx=0;dx<tempIndexAwal;dx++)
        {
            while(std::getline(ifs_Awal,lineDataAwal))
          s[dx] = lineDataAwal[dx++];
        }

        for(int dex =0;dex<tempIndexAwal;++dex)
        {
           ItemDefult = ListView1->Items->Add();
           ItemDefult->Caption = String(IntToStr(dex + 1));
           ItemDefult->SubItems->Add(s[dex].c_str());
        }
        ifs_Awal.close();
    delete []s;
    s = NULL;
}

there's no errors during compile, but the result ListView just showing the number with this code ItemDefult->Caption = String(IntToStr(dex + 1)); can anyone show me how the best way for i do.

3
  • 4
    Use std::vector. It's much easier and safer. Commented Oct 5, 2013 at 23:18
  • Hello @chris thanks for you advice. I have try using 'std::vector' but I got the error, I'm using 'cpp builder 2010' this the code std::vector(std::string) myline; while(std::getline(ifs_Awal,myline)) { myline.push_back(myline); } Commented Oct 6, 2013 at 7:11
  • the error exactly where the line 'while' exist? compiller says "could not find a match for std::string <_Elem,_Traits,_Alloc>..." Commented Oct 6, 2013 at 7:20

2 Answers 2

3

You are reading the file, leaving it open, and expecting to read it again. That won't work because the cursor in the file is at the end of the file (so your second while loop does nothing).

A much better approach would be:

std::vector<std::string> lines;
std::string line;
std::ifstream fin("Youfilename");

while (std::getline(fin, line))
{
    lines.push_back(line);
}
fin.close();
// add data to your list view
Sign up to request clarification or add additional context in comments.

3 Comments

hi @Zac Howland thanks for your suggestion, and I'm sorry I forgot to put delocator for showing on the post. so I was edit my code, please see the above. I have been try your code but I get the error the where "while" exist. the compiler says "could not find a match for std::string <_Elem,_Traits,_Alloc>..."
Probably because @ZacHowland gave it for granted that you knew that you must #include <vector> and #include <string> in your source file before you can use his snippet. Did you include those header files?
Hi @lorenzo Donati, yes i do first into header file. and I've to make a thread for the problems std::vector and i get correct answer. thanks for your advice
1

its easier if you use std::vector for dynamic arrays and don't forget to first include the file header with #include<vector>

void __fastcall TFrmNewPeta::showDefaultRute() {
    std::string lineDataAwal;
    std::ifstream ifs_Awal;
    std::vector<std::string> vec;
    ifs_Awal.open("DefaultDataAwal");

   /*get the string of lineDataAwal */
    while(std::getline(ifs_Awal,lineDataAwal))
    { vec.push_back(lineDataAwal);}

    for(int dex =0;dex<vec.size();++dex) 
    {
      ItemDefult = ListView1->Items->Add();
      ItemDefult->Caption = String(IntToStr(dex + 1));
      ItemDefult->SubItems->Add(vec.at(dex).c_str()); 
    }
   ifs_Awal.close();
}

Hope this helps

1 Comment

Hi @Adhy. Wow you back on my post and help me again. the code your as shown, that's exactly what I want. thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.