If I understand it correctly, strtok scans from the first character after the separator. The code below: - #include <iostream>
-
#include <fstream>
-
using namespace std;
-
-
void main()
-
{
-
char *rules[50];
-
int priority[50];
-
int i=0;
-
char line[100]=" (arule, 12), (brule, 21), (zrule, 70), (drule, 25), (erule, 10)(frule, 3)(grule, 20), (srule, 100)";
-
-
char *tok = strtok(line,"(");
-
rules[i]=strtok(NULL,",");
-
-
tok=strtok(NULL,")");
-
priority[i]=atoi(tok);
-
i++;
-
-
while((tok=strtok(NULL,"("))!=NULL)
-
{
-
rules[i]=strtok(NULL,",");
-
tok=strtok(NULL,")");
-
priority[i]=atoi(tok);
-
i++;
-
}
-
-
for(int j=0;j<i;j++)
-
{
-
cout<<rules[j]<<endl;
-
cout<<priority[j]<<endl;
-
}
-
}
gives an output like this:
arule
12
brule
21
zrule
70
drule
25
erule
10
However, if I remove the space between " and (arule, 12) so the line would look like: - char line[100]="(arule, 12), (brule, 21), (zrule, 70), (drule, 25), (erule, 10)(frule, 3)(grule, 20), (srule, 100)"; //there's no space between " and (arule, 12)
then the output would become:
brule
21
zrule
70
drule
25
erule
10
Why is a space needed between " and the first separator in order for the program to work correctly? Am I doing something incorrectly?
8 2128 horace1 1,510
Recognized Expert Top Contributor
the first token is framed with (, so try -
char line[100]="(arule, 12), (brule, 21), (zrule, 70), (drule, 25), (erule, 10)(frule, 3)(grule, 20), (srule, 100)";
-
char *tok;
-
rules[i]=strtok(line,"(,");
-
the first token is framed with (, so try -
char line[100]="(arule, 12), (brule, 21), (zrule, 70), (drule, 25), (erule, 10)(frule, 3)(grule, 20), (srule, 100)";
-
char *tok;
-
rules[i]=strtok(line,"(,");
-
Thank you. That fixes the problem.
I then modified the input into: - char line[100]="(arule, 12), (brule, 21), (zrule, 70), (drule, 25), (erule, 10)(frule, 3)(grule, 20), (srule, 100)";
Notice that there's no "," between "(erule, 10)", "(frule, 3)", and "(grule, 20)". What I want to do is, let the program read from "(arule, 12)" until "(erule,10)", pause right before "(frule,3)", do something, then continue reading "(frule, 3)", pause before "(grule, 20)", do something, then continue reading "(grule,20), (srule, 100)".
So far I've tried adding:
and then changing the while loop to: - while((num=strtok(NULL,"("))!= NULL && (num=strtok(NULL,"("))!=next)
-
{
-
rules[i]=strtok(NULL,",");
-
num=strtok(NULL,")");
-
priority[i]=atoi(num);
-
i++;
-
}
but the output becomes:
arule
12
zrule
70
erule
10
srule
100
I figured everytime a strtok is called it changes the beginning token so I then changed the while loop into: - while((num=strtok(NULL,"("))!= NULL)
-
{
-
rules[i]=strtok(NULL,",");
-
num=strtok(NULL,")");
-
priority[i]=atoi(num);
-
i++;
-
-
if(num==next)
-
cout<<"pause"; //to test if the condition works
-
}
but the output becomes:
arule
12
brule
21
zrule
70
drule
25
erule
10
grule
20
srule
100
What am I doing wrong?
Thank you.
Will someone be so kind to point out why my program hangs? It doesn't give out any error message other than "<program_name has stopped working>". - #include <iostream>
-
#include <fstream>
-
using namespace std;
-
-
void main()
-
{
-
ifstream infile;
-
char c[256],str[64];
-
char *rules[50];
-
int priority[50];
-
int i=0;
-
char *num;
-
-
cout<<"Enter the name of input file: ";
-
cin.get(str, 64);
-
-
infile.open(str);
-
-
for(i=0;i<256;i++)
-
{
-
c[i]=infile.get();
-
cout<<c[i];
-
}
-
-
cout<<endl;
-
i=0;
-
-
rules[i]=strtok(c,"(,");
-
num=strtok(NULL,")");
-
priority[i]=atoi(num);
-
-
cout<<endl;
-
cout<<rules[i];
-
cout<<endl;
-
cout<<priority[i];
-
cout<<endl;
-
-
i++;
-
-
while((num=strtok(NULL,"("))!=NULL)
-
{
-
rules[i]=strtok(NULL,",");
-
cout<<rules[i]<<endl;
-
num=strtok(NULL,")");
-
priority[i]=atoi(num);
-
cout<<priority[i]<<endl;
-
i++;
-
}
-
infile.close();
-
}
The input file has only one line:
(R10, 10), (R20, 20), (R90, 90), (R75, 75), (R35, 35), (R60, 60).
The program works just fine if I include the input line in the program (in other words: char c[100]="(R10, 10), (R20, 20), (R90, 90), (R75, 75), (R35, 35), (R60, 60)"; works just fine). But it would hang at the while loop when the input is reading from a file. It would output
R10
10
R20
20
R90
90
R75
75
R35
35
R60
60
but hangs right after 60. Looks to me it is unable to "satisfy" the !=NULL condition but I can't why.
Any advice appreciated.
Thank you.
horace1 1,510
Recognized Expert Top Contributor
your program works fine with GNU C++.
The problem may be in your the loop where you read a line from the file -
for(i=0;i<256;i++)
-
{
-
c[i]=infile.get();
-
cout<<c[i];
-
}
-
in that the string is not being properly null terminated with your compiler.
try
so you read up to 256 characters or until a newline is found
so you read up to 256 characters or until a newline is found
Thank you! I wasn't aware of the difference between c[i]=infile.get() and infile.get(c,256). Now the program can read the line just fine. However, it hangs again if the input is like this:
(arule, 10), (brule,20)(crule, 30)(drule, 40), (erule, 50)(frule, 60)
I use Visual Studio and it simply says the program (the one I wrote) has stopped working. The output would look like this:
arule
10
brule
20
drule
40
erule
50
(crule, 30) and (frule, 60) are both missing. I think it's got something to do with how the delimiter is set up. What I am trying to do is, let the program read block "(arule, 10), (brule, 20)", do something, read (crule, 30), do something, read block "(drule, 40), (erule, 50)", do something, then read (frule, 60) and do something. So how do I make a condition that says if current token ends with ")" and there's a "(" immediately follows, then do something before continue reading next block?
Thank you.
horace1 1,510
Recognized Expert Top Contributor
it gives a segmentation error with Visual; C++ on my machine
the problem is if strtok() returns NULL within your loop - you need to check and if so exit the loop, e.g. -
while((num=strtok(NULL,"("))!=NULL)
-
{
-
if((rules[i]=strtok(NULL,","))==NULL)break;
-
cout<<rules[i]<<endl;
-
if((num=strtok(NULL,")"))==NULL)break;
-
cout << "num " << num <<endl;
-
priority[i]=atoi(num);
-
cout<<priority[i]<<endl;
-
i++;
-
}
-
it gives a segmentation error with Visual; C++ on my machine
the problem is if strtok() returns NULL within your loop - you need to check and if so exit the loop, e.g. -
while((num=strtok(NULL,"("))!=NULL)
-
{
-
if((rules[i]=strtok(NULL,","))==NULL)break;
-
cout<<rules[i]<<endl;
-
if((num=strtok(NULL,")"))==NULL)break;
-
cout << "num " << num <<endl;
-
priority[i]=atoi(num);
-
cout<<priority[i]<<endl;
-
i++;
-
}
-
Shouldn't num=strtok(NULL,"(")) return NULL if there's no more element after the last ")"?
Alright I have modified my code into this: - #include <iostream>
-
#include <fstream>
-
#include <string>
-
using namespace std;
-
-
#define index 5
-
-
struct am{
-
string rule;
-
int priority;
-
}set[index];
-
-
void printset(am amg)
-
{
-
cout<<amg.rule<<endl;
-
cout<<amg.priority<<endl;
-
}
-
-
void main()
-
{
-
ifstream infile;
-
char c[256],str[32];
-
char *rules[50];
-
int priority[50];
-
int i=0;
-
char *num;
-
-
cout<<"Enter the name of input file: ";
-
cin.get(str, 32);
-
-
infile.open(str);
-
infile.get(c,256);
-
-
set[i].rule=strtok(c,"(,");
-
-
for(i=0;i<index;i++)
-
{
-
num=strtok(NULL,")");
-
set[i].priority=atoi(num);
-
num=strtok(NULL,"(");
-
set[i+1].rule=strtok(NULL,",");
-
}
-
for(int n=0;n<index;n++)
-
printset(set[n]);
-
-
infile.close();
-
}
That solves the while loop problem. But another problem still stands: the program reads just fine with input of (arule, 10), (brule,20), (crule, 30), (drule, 40), (erule, 50), (frule, 60). But if the input is (arule, 10), (brule,20)(crule, 30)(drule, 40), (erule, 50)(frule, 60), the program will hang.
Please advise on how to tell the strtok to separate ") from "(", or pause at ")" if the immediate element is "(". Thank you.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: tvn007 |
last post by:
// I am using strtok to break the string
// For example: to extract 5 from the string below:
// TEST 1,P,5,PASS
// Below is my code in C:
ptr =strtok(testbuff," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");
|
by: Fernando Barsoba |
last post by:
Hi,
I'm using strtok() in the following way:
void obtain_param(char *pmsg, CONF_PARAMS *cnf ) {
char *s1, *s2;
size_t msg_len;
s1 = strtok (pmsg,":");
if (s1) {
|
by: ern |
last post by:
I'm using strtok( ) to capture lines of input. After I call
"splitCommand", I call strtok( ) again to get the next line. Strtok( )
returns NULL (but there is more in the file...). That didn't happen
before 'splitCommands' entered the picture. The problem is in
splitCommands( ) somehow modifying the pointer, but I HAVE to call that...
|
by: bofh1234 |
last post by:
I need to delimit a string. The delimiters are a semicolon and comma.
When I run the program I get a segmentation fault on the first strtok.
I followed the examples of others and from my old C books, but I can't
seem to find the problem. The accesslist has a format of
20,45;22,44;46,28;99,43,etc. What am I doing wrong?
Thanks,
#include...
|
by: hu |
last post by:
hi, everybody!
I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0.
Program is simple, but mistake is confusing. First, the below code can get
right outcome:"ello world, hello dreams."
#include <stdafx.h>
#include <string.h>
#include <stdio.h>
int main()
| |
by: Kelly B |
last post by:
I need a function which returns me a "word" from a given string and
then sets the pointer to the next one which is then retrieved during
further calls to the function.
I think strtok( ) is the solution but i could not understand the use of
the function as given in the C99 standard
EXAMPLE
#include <string.h>
static char str =...
|
by: Lothar Behrens |
last post by:
Hi,
I have selected strtok to be used in my string replacement function.
But I lost the last token, if there is one.
This string would be replaced
select "name", "vorname", "userid", "passwort" from "users" order by
"users"
|
by: krista |
last post by:
Hi,
I am beginner of C++. When I try to read a file and put it into X and
Y variables. I got that segmentation fault.
the data file is
data.txt:
4.4 6.8
3.2 -5.5
3.3 0.9
|
by: Stu Cazzo |
last post by:
Hi all,
I have a question on why strtok is doing what it's doing for my
splitString( string2 ); call.
Below is the output for the entire program:
token was: word1
token was: word2
token was: word3
token was: word1
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |