Click to See Complete Forum and Search --> : Reading Strings from a text file


they
January 14th, 2005, 10:46 PM
Hi,

I'm currently building my first managed c++ application using visual c++ 2003 .net, and I am having a problem reading strings from a text file. Here is an example of the contents of the file:

path_to_resource_file c:/app/files/res.txt
path_to_desc_file c:/app/files/desc.txt

My current code is as follows:

FileStream* pr = new FileStream(mPath, FileMode::Open);
BinaryReader * rp = new BinaryReader(pr);

String* fileDescription = rp->ReadString();
String* amp = rp->ReadString();

this->ampDataBox->Text = amp;

rp->Close();

This unfortunately throws the complete file into the text box of the application.

What I want to be able to do is read the first string (path_to_resource_file) into fileDescription and then read the second string (c:/app/files/res.txt) into amp.

using standard c++ this was easy:

ifstream readFile(mPath, ios::in | ios::binary);

string fileDescription = " ";
string amp = " ";

readFile >> fileDescription >> amp;



In the above example fileDescription now equals "path_to_resource_file " and amp equals "c:/app/files/res.txt"

How can I read the strings as above in managed c++?

Marc G
January 17th, 2005, 09:16 AM
Instead of a BinaryReader, use a StreamReader which has a method called ReadLine to read a single line. Once you have your line, find the first space in your text and split your line on that space.

NOTE: readFile >> fileDescription >> amp; won't work when your path contains spaces!

they
January 19th, 2005, 12:30 AM
Thanks Marc G,

You definately sent me in the right direction. I'm using the Split function in the code below to split two words seperated by white space. I am also trying to compare them, but have been unable to do so.

Here is the code:



#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;


int main()
{


String* delimStr = S" ";
Char delimiter[] = delimStr->ToCharArray();

String* words = S"the the";
String* split[] = 0;



for (int x = 1; x <= 2; x++)
{
split = words->Split(delimiter, x);
}

String* strOne = split[0];
String* strTwo = split[1];

Console::WriteLine(strOne);
Console::WriteLine(strTwo);



if(strOne == strTwo)
{
Console::WriteLine(S"These Strings are equal");
}



system("PAUSE");
}


Any idea why the "if" statement can not equate the two strings?

Thanks for your help.

Jinto
January 19th, 2005, 12:33 AM
Take a look at the string class.

String::Equal
String::Compare

they
January 19th, 2005, 01:01 AM
Yup, just found the compare function.



#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;


int main()
{


String* delimStr = S" ";
Char delimiter[] = delimStr->ToCharArray();

String* words = S"the the";
String* split[] = 0;



for (int x = 1; x <= 2; x++)
{
split = words->Split(delimiter, x);
}

String* strOne = split[0];
String* strTwo = split[1];

Console::WriteLine(strOne);
Console::WriteLine(strTwo);

int compare = strOne->CompareTo(strTwo);



if(compare == 0)
{
Console::WriteLine(S"These Strings are equal");
}
else if(compare != 0)
{
Console::WriteLine(S"These Strings are not equal");
}



system("PAUSE");
}



Thanks for your help everyone.