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 -
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. -
//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?
6 5185
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 -
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. -
//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.
when looking around i found code that used -
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.
when looking around i found code that used -
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.
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)
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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...
|
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...
|
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...
|
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;
| |
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
|
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...
|
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);
|
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...
|
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
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |