Pointer to string array or a work around C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    #1

    Pointer to string array or a work around C#

    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

    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
    }
    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.

    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.
    }
    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?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Studlyami
    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

    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
    }
    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.

    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.
    }
    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?
    Just read the file into an ArrayList using a TextReader.
    Open the specs for the ArrayList and TextReader classes and you should be able to find everything you need.

    Comment

    • Studlyami
      Recognized Expert Contributor
      • Sep 2007
      • 464

      #3
      when looking around i found code that used
      Code:
      string SplitLine[];  
      
      CString FileLine = MyFile.ReadLine();
      SplitLine = FileLine.Split(","); //lets say this row holds 10 items SplitLine has 10 elements
      
      //then i grab another line
      FileLine = MyFile.ReadLine();
      //now SplitLine will change size;
      SplitLine = FileLine.Split(",");  //now this row holds 5 items.  SplitLine has 5 items
      Why does this work? how is the behavior of String[] Variable defined? How does it change it size depending on the return? Using my Splitline in this manner i was able to take the scope of the variable up a level so my parser works, but i'm puzzled about it.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Studlyami
        when looking around i found code that used
        Code:
        string SplitLine[];  
        
        CString FileLine = MyFile.ReadLine();
        SplitLine = FileLine.Split(","); //lets say this row holds 10 items SplitLine has 10 elements
        
        //then i grab another line
        FileLine = MyFile.ReadLine();
        //now SplitLine will change size;
        SplitLine = FileLine.Split(",");  //now this row holds 5 items.  SplitLine has 5 items
        Why does this work? how is the behavior of String[] Variable defined? How does it change it size depending on the return? Using my Splitline in this manner i was able to take the scope of the variable up a level so my parser works, but i'm puzzled about it.
        SplitLine's size changes because it is changed to hold a reference to a different array with a different number of elements.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          I noticed a little problem, that I'm sure is just a typo, because the compiler would warn you about it, but I wanted to make sure:
          if(IsKeyword = false)

          That is an assignment, not a conditional statement.
          Be sure it's like this:
          if(IsKeyword == false)
          OF
          if (!isKeyword)

          Comment

          • Studlyami
            Recognized Expert Contributor
            • Sep 2007
            • 464

            #6
            Originally posted by r035198x
            SplitLine's size changes because it is changed to hold a reference to a different array with a different number of elements.
            When would those array get deleted? would it be at the end of the object, or do they last until the end of the program? is there a way to clear SplitLine (delete the array) before i change it to hold different array?

            Thanks Plater, it was just a typo in here though.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by Studlyami
              When would those array get deleted? would it be at the end of the object, or do they last until the end of the program? is there a way to clear SplitLine (delete the array) before i change it to hold different array?

              Thanks Plater, it was just a typo in here though.
              No you don't need to delete that array manually.
              Once no variable is pointing to it, it becomes legible for garbage collection an C# does that for you automagically.

              Comment

              Working...