473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pointer to string array or a work around C#

464 Expert 256MB
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

Expand|Select|Wrap|Line Numbers
  1. char* FileToken;
  2. char LineBuffer[1000];
  3. while (Test != NULL) //i haven't reach eof.
  4. if(isKeyword == false)
  5. {
  6. 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.
  7.             if(FileToken == NULL) // end of line or blank line
  8.             {
  9.                 Test= fgets(LineBuffer, sizeof(LineBuffer),f_ptr); //grab next line in file
  10.                 continue;
  11.             }
  12.  
  13.             isKeyword = CheckForKeyword(FileToken);
  14. }
  15. else    
  16. if(isKeyword == true)
  17. {
  18.     ItemInfo TempNewInfo; //creates Item Info Object
  19.     isKeyword = false;
  20.  
  21.     ParseItem(&TempNewInfo); //grab the items stats.
  22. . . .
  23. }
  24.  
  25.  
  26. ////void ParseItem( ItemInfo PassedItem)
  27. {
  28. char * FileToken = strtok (NULL, ","); //HERE is a problem in C#.  I now grab the next item from the strtok that was started up abvoe
  29. }
  30.  
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.

Expand|Select|Wrap|Line Numbers
  1. //open file with streamreader
  2.  
  3. while(FileLine != null)
  4. {
  5. if(IsKeyword = false)
  6. {
  7.      string[] SplitLine = FileLine.Split(','); //puts ALL items in the string array;
  8.      //problem is the string scope ends before i need to use the items that were in that line
  9.  
  10.      CheckKeyWord(SplitLine[0])
  11. }
  12. else
  13. if( IsKeyword == true)
  14. {
  15.     ItemInfo TempNewInfo; //creates Item Info Object
  16.     isKeyword = false;
  17.  
  18.     ParseItem(ref TempNewInfo, <NEED ITEMS IN STRING[] ABOVE>); //grab the items stats.
  19. }
  20.  
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?
Nov 13 '07 #1
6 5164
r035198x
13,262 8TB
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

Expand|Select|Wrap|Line Numbers
  1. char* FileToken;
  2. char LineBuffer[1000];
  3. while (Test != NULL) //i haven't reach eof.
  4. if(isKeyword == false)
  5. {
  6. 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.
  7.             if(FileToken == NULL) // end of line or blank line
  8.             {
  9.                 Test= fgets(LineBuffer, sizeof(LineBuffer),f_ptr); //grab next line in file
  10.                 continue;
  11.             }
  12.  
  13.             isKeyword = CheckForKeyword(FileToken);
  14. }
  15. else    
  16. if(isKeyword == true)
  17. {
  18.     ItemInfo TempNewInfo; //creates Item Info Object
  19.     isKeyword = false;
  20.  
  21.     ParseItem(&TempNewInfo); //grab the items stats.
  22. . . .
  23. }
  24.  
  25.  
  26. ////void ParseItem( ItemInfo PassedItem)
  27. {
  28. char * FileToken = strtok (NULL, ","); //HERE is a problem in C#.  I now grab the next item from the strtok that was started up abvoe
  29. }
  30.  
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.

Expand|Select|Wrap|Line Numbers
  1. //open file with streamreader
  2.  
  3. while(FileLine != null)
  4. {
  5. if(IsKeyword = false)
  6. {
  7.      string[] SplitLine = FileLine.Split(','); //puts ALL items in the string array;
  8.      //problem is the string scope ends before i need to use the items that were in that line
  9.  
  10.      CheckKeyWord(SplitLine[0])
  11. }
  12. else
  13. if( IsKeyword == true)
  14. {
  15.     ItemInfo TempNewInfo; //creates Item Info Object
  16.     isKeyword = false;
  17.  
  18.     ParseItem(ref TempNewInfo, <NEED ITEMS IN STRING[] ABOVE>); //grab the items stats.
  19. }
  20.  
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.
Nov 13 '07 #2
Studlyami
464 Expert 256MB
when looking around i found code that used
Expand|Select|Wrap|Line Numbers
  1. string SplitLine[];  
  2.  
  3. CString FileLine = MyFile.ReadLine();
  4. SplitLine = FileLine.Split(","); //lets say this row holds 10 items SplitLine has 10 elements
  5.  
  6. //then i grab another line
  7. FileLine = MyFile.ReadLine();
  8. //now SplitLine will change size;
  9. SplitLine = FileLine.Split(",");  //now this row holds 5 items.  SplitLine has 5 items
  10.  
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.
Nov 14 '07 #3
r035198x
13,262 8TB
when looking around i found code that used
Expand|Select|Wrap|Line Numbers
  1. string SplitLine[];  
  2.  
  3. CString FileLine = MyFile.ReadLine();
  4. SplitLine = FileLine.Split(","); //lets say this row holds 10 items SplitLine has 10 elements
  5.  
  6. //then i grab another line
  7. FileLine = MyFile.ReadLine();
  8. //now SplitLine will change size;
  9. SplitLine = FileLine.Split(",");  //now this row holds 5 items.  SplitLine has 5 items
  10.  
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.
Nov 14 '07 #4
Plater
7,872 Expert 4TB
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)
Nov 14 '07 #5
Studlyami
464 Expert 256MB
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.
Nov 14 '07 #6
r035198x
13,262 8TB
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.
Nov 14 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bo Sun | last post by:
hi: please take a look at the following code fragment: char* hello = "Hello World\n"; hello = 's'; why I cannot modify hello?
12
by: ur8x | last post by:
Why does this declaration give undefined result: file1: extern char * p; file2: char p; Let's assume p has been initialized, now accessing p...
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
10
by: Michael | last post by:
Hi, I'm trying to get my head around the relationship between pointers and arrays. If I have the following: int even = {2,4,6,8,10}; int *evenPtr = {even+4, even+3, even+2, even+1, even};...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
6
by: KWienhold | last post by:
I'm currently working on a project in C# (VS 2003 SP1, .Net 1.1) that utilizes IStream/IStorage COM-Elements. Up to now I have gotten everything to work to my satisfaction, but now I have come...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
11
by: copx | last post by:
Unforuntately, I know next to nothing about ASM and compiler construction, and while I was aware of the syntactic differences between pointers and arrays, I was not aware of this: ...
20
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.