LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2009, 10:09 PM   #1
ohmygoth
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Rep: Reputation: 0
Exclamation Checking for Illegal characters in C++


I'm trying to make a single program that ask for a string (to create a file later) and check if there is some illegal character, this is what I have, but I get "Segmentation fail", I'm using Gcc under linux

Quote:
int main()
{
string archivou;
cout << "Enter filename" << endl;
getline(cin,archivou);

char simbolos[]={};
strcpy(simbolos,archivou.c_str());
int i;
int erroree=0;
for (i=0;i<strlen(simbolos);i++){
if ((simbolos[i] < 47 && simbolos[i] > 57) || (simbolos[i] < 64 && simbolos [i] > 95) || (simbolos[i] < 97 && simbolos[i] > 122)){
erroree++;}
}
printf("%d errors",erroree);*/
}
What am I doing wrong?!
 
Old 12-06-2009, 10:28 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
I would think this is the problem
Code:
char simbolos[]={};
strcpy(simbolos,archivou.c_str());
Your allocating 'simbolos' 0 bytes of memory, then your putting stuff into it with strcpy (length of 'archivou.c_str()' bytes of memory). Two alternatives:

- change 'char simbolos[]={};' to 'char * simbolos', and dynamically allocate enough memory to it, based on the length of the 'archivou' string entered.

- or preferably: use the 'archivou' variable itself, theres no need for another variable (or array of variables such as char[]). You can use array syntax on strings, so just do the checks on the actual string, like
Code:
if ( archivou[i] < 47 //...
etc.

Last edited by nadroj; 12-06-2009 at 10:29 PM.
 
Old 12-07-2009, 12:30 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Nadroj is correct:
Code:
  // Wrong 
  char simbolos[]={};
  strcpy(simbolos,archivou.c_str());

  // Better
  int ilen = strlen (archivou.c_str());
  char *simbolos = new char[ilen + 1];
  strcpy(simbolos,archivou.c_str());

  // Best
  string simbolos  = new string(archivou);
However, as to your original question:

I'd strongly encourage you NOT to use hard-coded constants like "if ((simbolos[i] < 47 && simbolos[i] > 57)" and instead:

a) #include <ctype.h>
b) use the standard macros "isprint()", "isalpha ()", etc
(or the Unicode equivalents "iswprint()", etc)

IMHO .. PSM
 
Old 12-07-2009, 12:36 AM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
char simbolos[]={};
HELLO! by default the compiler allocates 1 byte

Code:
char *simbolos = new char [2+strlen( archivou.c_str() )];
is ok.
 
Old 01-13-2010, 01:29 PM   #5
ohmygoth
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thumbs up Thanx

Tnx everybody, finally I used this code:

int RevisarExtension() {
int validos=0;
char key[] = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890 -_.";
char * pch;
pch = strpbrk (archivo.c_str(), key);
while (pch != NULL) {
pch = strpbrk (pch+1,key); validos++;
}
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Mastering Characters Sets in Linux (Weird Characters, part 2) LXer Syndicated Linux News 0 11-25-2009 11:30 PM
Nagios: Illegal characters in config file TalkingMarble Linux - Software 7 10-29-2009 07:35 AM
splitting numbers and characters from an array of characters. trscookie Programming 6 11-14-2008 09:34 AM
Accented Characters and other "foreign language" Characters Mark_in_Hollywood LQ Suggestions & Feedback 2 04-30-2007 06:10 PM
How to modify the names of files and replace characters with other characters or symb peter88 Linux - General 2 12-10-2006 03:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration