473,804 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer to string array or a work around C#

464 Recognized Expert Contributor
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 5185
r035198x
13,262 MVP
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 Recognized Expert Contributor
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 MVP
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 Recognized Expert Expert
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 Recognized Expert Contributor
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 MVP
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
7420
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
2400
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
2538
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 going to hell just by using dlopen()/LoadLibrary() respectively, I'm still trying to get it as clean as possible. I have a number of different quantums of data and a number of plugins. Since any plugin can (and possibly will) touch any quantum...
26
3067
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 read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
10
407
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}; int **ip = evenPtr+3;
2
3279
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 and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs thru. But once i add more, it dies. In this program i have 4 files setup to read. The
6
3023
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 across a problem I can't really explain: When deleting an object from an IStorage, the space it used up will not be freed, but rather marked as unused and overwritten the next time you add an object to the storage. This is obviously working as...
29
3658
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 string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
11
1980
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: http://udrepper.livejournal.com/13851.html (Yes, it is livejournal, but it is the journal of the glibc maintainer) I am not sure I really understand this low-level difference between pointers and arrays, and I do not have the time to learn ASM and compiler...
20
12452
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
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10564
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7609
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5513
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.