By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Problems whith an header file

P: n/a
Hi everyone,
I've just created a function in a header file that accept a string and
parse but the prototipe of the function return me an exception:

#include <stdio.h>
#include <string.h>

int Scan(char Search[]) // this generate the exception
{
// char Search[] = "Sample text"; //only in this way
that perfectly works!
char *pch;
pch = strtok (Search," -,.");
while (pch != NULL)
{
printf ("%s\n",pch);
//
// some other code here...
//
pch = strtok (NULL, " -,.");
}
}
the function must accept the string.
I use this header whith this sample.cpp
#include "myHeader.h"

int main()
{
Scan("Hi everyone");
return 0;
}
the function return an error when i try to parse a string whith " -,."
Can you help me please?
Thx for all!
Federico

Jul 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 1 Jul 2006 03:11:34 -0700, "J4CK4L" <fe**************@tiscali.it>
wrote in comp.lang.c++:
Hi everyone,
I've just created a function in a header file that accept a string and
parse but the prototipe of the function return me an exception:
What exception? Always include the compiler, linker, or run time
error message in your post. Don't expect people to read your mind or
guess what the problem is.
#include <stdio.h>
#include <string.h>
Is this C code or C++? In C++, it is much better to use <cstdio> and
<cstring>.
int Scan(char Search[]) // this generate the exception
{
// char Search[] = "Sample text"; //only in this way
that perfectly works!
char *pch;
pch = strtok (Search," -,.");
The strtok() function modified the string, that is it writes a '\0'
over each token as it finds it.
while (pch != NULL)
{
printf ("%s\n",pch);
//
// some other code here...
//
pch = strtok (NULL, " -,.");
}
}
the function must accept the string.
I use this header whith this sample.cpp
#include "myHeader.h"

int main()
{
Scan("Hi everyone");
You are passing a string literal to your function. It is undefined
behavior, in both C and C++, to try to modify a string literal. Try
changing your code like this:

char test [] = "Hi everyone";
Scan(test);
return 0;
}
the function return an error when i try to parse a string whith " -,."
Can you help me please?


The array test is an array of writeable characters that strtok() can
modify.

See also this specific question in the comp.lang.c FAQ:

http://c-faq.com/decl/strlitinit.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 1 '06 #2

P: n/a
Thx for all!!
I just made it!
Oh... another question;
how can I see if the word that I scan is match whit another in a file
called wordlist.dat?
thx again! :)

Jul 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.