Okay, i have developed a file parser in C++ that i am trying to being into a c# program which is proving to be a lot more difficult than i thought.
first i scan a file (which i opened using a streamreader) until i find a keyword that i specified. Then i want to grab the items after that keyword and do a switch on them. In C++ it looked something like this
Now in C# in order to parse i cant use strtok and then continue it in a different function so here is what i have.
i tried to do something like a string *
string* ParsedStringPtr
string[] SplitLine = FileLine.Split( ',');
ParsedStringPtr = SplitLine; // or ParsedStringPtr = &SplitLine;
both of which makes the compiler complain. So how can i retain the items in the string::split function when the string array goes out of scope?
Do i have to read in the entire file into a char array and pass in the line that we are currently at? that seems like a poor way of doing this. Any ideas? anyone need more clarification?
first i scan a file (which i opened using a streamreader) until i find a keyword that i specified. Then i want to grab the items after that keyword and do a switch on them. In C++ it looked something like this
Code:
char* FileToken;
char LineBuffer[1000];
while (Test != NULL) //i haven't reach eof.
if(isKeyword == false)
{
FileToken = strtok(LineBuffer, ","); //grab first part of string until you hit a ',' HERE is the problem. I used strtok in a function down below to continue on THIS line in the file.
if(FileToken == NULL) // end of line or blank line
{
Test= fgets(LineBuffer, sizeof(LineBuffer),f_ptr); //grab next line in file
continue;
}
isKeyword = CheckForKeyword(FileToken);
}
else
if(isKeyword == true)
{
ItemInfo TempNewInfo; //creates Item Info Object
isKeyword = false;
ParseItem(&TempNewInfo); //grab the items stats.
. . .
}
////void ParseItem( ItemInfo PassedItem)
{
char * FileToken = strtok (NULL, ","); //HERE is a problem in C#. I now grab the next item from the strtok that was started up abvoe
}
Code:
//open file with streamreader
while(FileLine != null)
{
if(IsKeyword = false)
{
string[] SplitLine = FileLine.Split(','); //puts ALL items in the string array;
//problem is the string scope ends before i need to use the items that were in that line
CheckKeyWord(SplitLine[0])
}
else
if( IsKeyword == true)
{
ItemInfo TempNewInfo; //creates Item Info Object
isKeyword = false;
ParseItem(ref TempNewInfo, <NEED ITEMS IN STRING[] ABOVE>); //grab the items stats.
}
string* ParsedStringPtr
string[] SplitLine = FileLine.Split( ',');
ParsedStringPtr = SplitLine; // or ParsedStringPtr = &SplitLine;
both of which makes the compiler complain. So how can i retain the items in the string::split function when the string array goes out of scope?
Do i have to read in the entire file into a char array and pass in the line that we are currently at? that seems like a poor way of doing this. Any ideas? anyone need more clarification?
Comment