LinuxQuestions.org
Help answer threads with 0 replies.
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 08-13-2003, 06:57 PM   #1
cxel91a
Member
 
Registered: May 2001
Posts: 61

Rep: Reputation: 15
Unhappy control chars past array


void createDir()
{
char* dirName = new char[8];
cout<<"Enter the name of the directory : ";
// doesn't work dirName[7] ='\0';
// doesn't work *(dirname + 7)='\0';
//
// I'm trying to get only 8 chars max. When anything over 7
//the program goes in to a continues loop. How can I discard
//all characters after 7 ?

cin.getline(dirName,8);


if(mkdir(dirName,777) < 0)
{
perror("Cannot create directory");
}
mkdir(dirName,777);

delete[] dirName;
}

Last edited by cxel91a; 08-14-2003 at 01:58 PM.
 
Old 08-13-2003, 08:38 PM   #2
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
you can use cin.ignore(x); which will ignore the next x value of characters i believe.
 
Old 08-14-2003, 03:11 PM   #3
devoyage
Member
 
Registered: Aug 2003
Distribution: many
Posts: 37

Rep: Reputation: 15
Re: control chars past array

Quote:
Originally posted by cxel91a

if(mkdir(dirName,777) < 0)
{
perror("Cannot create directory");
}
mkdir(dirName,777);

delete[] dirName;
}
That second mkdir() might be messing with you. (you already made it by then)
 
Old 08-14-2003, 05:23 PM   #4
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
The failbit is being set on cin if you give too many characters to getline.

You need to call cin.clear() before you will be able to do anything with cin again.

To discard everything with ignore( ... ) call it with std::numeric_limits<std::streamsize>::max() as the size.

Last edited by dakensta; 08-14-2003 at 05:27 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Buffer overflow and ip spoofing Ephracis Linux - Security 10 12-07-2004 05:39 PM
What is a buffer overflow Joey.Dale Linux - Security 4 07-12-2004 05:12 PM
Buffer Overflow pymehta Linux - Security 7 02-24-2004 01:19 PM
Preventing buffer overflow with gets() JStew Programming 1 11-19-2002 11:03 AM
Q. What is a buffer overflow? auslew Linux - Security 2 11-08-2002 05:36 AM


All times are GMT -5. The time now is 04:28 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration