Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-13-2003, 07:57 PM   #1
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 ?


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

delete[] dirName;

Last edited by cxel91a; 08-14-2003 at 02:58 PM.
Old 08-13-2003, 09:38 PM   #2
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, 04:11 PM   #3
Registered: Aug 2003
Distribution: many
Posts: 37

Rep: Reputation: 15
Re: control chars past array

Originally posted by cxel91a

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

delete[] dirName;
That second mkdir() might be messing with you. (you already made it by then)
Old 08-14-2003, 06:23 PM   #4
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 06:27 PM.


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

All times are GMT -5. The time now is 09:58 PM.

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