Reading lines of text

Aug 19, 2014 at 11:27am
Hi,

I've asked a similar question to this before but I'm still having trouble. Suspose I have a text file that has:

001 This is line 1
002 This is line 2
003 This is line 3

And a Form with a richTextBox (called richTextBox1) and a button. When the button is clicked, a line from this text is then displayed into the richTextBox.

So far, I have the code for the button when it's clicked:
1
2
3
4
5
StreamReader^ sr = gcnew StreamReader("C:\\Test.txt");
String^ line;
line = sr->ReadLine();
richTextBox1->Text = line->Substring(4);
sr->Close();


This only displays line one but doesn't display the first 4 characters (because of Substring(4))...

How do I display a specific line into a richTextBox? E.g. line 'n'.I've done this using getline etc in C++, but not for a windows form.

Any ideas / suggestions?
Last edited on Aug 19, 2014 at 11:29am
Sep 1, 2014 at 11:17am
Hi,

I've solve my problem and here's the solution if anyone is interested:

Contents of Test.txt:
001 This is line 1
002 This is line 2
003 This is line 3

All in Form1:
Includes:
1
2
#include <string>
#include <fstream> 


After namespaces (or place after int main(){...} in main.cpp):
1
2
3
4
bool isWanted(const std::string & str)
{
return (str.find("001") != string::npos); // change "001" to whatever is needed
}


Within button1_Click event:
1
2
3
4
5
6
7
8
9
10
11
bool isWanted(const std::string & str);

String^ str2;
ifstream fin("C:\\Test.txt");
string str;
while (getline(fin, str)){
if (isWanted(str))
str2 = gcnew String(str.c_str()); // convert std:string to System::String^
richTextBox1->Text = str2->Substring(4); // starts 4 characters in
}
fin.close();


This code finds the line with '001' in it and omits the first 4 characters.

My issue was converting std::string to System::String^....

Cheers,
Last edited on Sep 1, 2014 at 11:23am
Sep 2, 2014 at 12:35am
Hi!

I sound stupid now. The above code only works when:
1
2
3
4
bool isWanted(const std::string & str)
{
return (str.find("001") != string::npos); // change "001" to whatever is needed
}


When "001" is changed to any other number, the program fails....

I'm getting closer to solving this myself but I'm assuming it has something to do with this line of code:
return (str.find("001") != string::npos);

Interestingly, if ->Substring(4); is removed, the code works for any number replacing "001".....

Any thoughts / advice?
Last edited on Sep 2, 2014 at 12:36am
Sep 2, 2014 at 1:37am
Ok, I believe this works now...

And I hope this helps someone out there!

The codes for the includes, and introduction to bool 'isWanted' remains the same. However the button1_Click event needs the following:
1
2
3
4
5
6
7
8
9
10
11
bool isWanted(const std::string & str);

String^ str2;
ifstream fin("C:\\Test.txt");
string str;
while (getline(fin, str)){
if (isWanted(str))
str2 = gcnew String(str.substr(4).c_str()); // convert std:string to System::String^ and 
//substr(4) is used here instead of str2->Substring(4) in the next line
richTextBox1->Text = str2; //displays truncated line of text}
fin.close();


I'm not sure why but the program works if substr(4) is applied to the std::string rather than using ->Substring(4) after std::string has been converted to System::String^.... At least, this is my interpretation of what is going on..

Cheers,
Last edited on Sep 2, 2014 at 1:38am
Topic archived. No new replies allowed.