im sorta having trouble (since i am a total noob) with this piece of code:

for( ;; )
    {
        system("md %random%");
    }

simple piece of code....compiles with the rest of my program perfectly but the problem i am having is how can i make this loop last when 10 random named folder were created or 5 random folders were created basically a break from the loop after 5 times or 10 times or 20 times...i know this can be done using a switch statement or writing the same line of code 10 times but i want to know how to do it in a loop......all help is welcome.

Recommended Answers

All 6 Replies

I'm really beginning to wonder what it is you think you're learning, or what it is you're trying to achieve.

Add a program to startup by brute force
http://www.daniweb.com/forums/thread162281.html

Start a browser looking at any hard disk
http://www.daniweb.com/forums/thread140662.html

And now you want to fill the disk with random rubbish.

Because at the moment, it looks just like an annoying little pest.

please dont get me wrong here im not trying to create anytype of malware..and if the code looks a bit to suspicious then i will use this piece of code:

for(;;)
      {
                 some random code...
       }
LPSECURITY_ATTRIBUTES attributetag = NULL;
string name;

for(int i = 0; i < 10; i++)
{
    generator(name);
    CreateDirectory(name.c_str(), attributetag);
    ...add files, fill them with junk using generator()...
}

...

void generator(string &str)
{
    for(int i = 0; i < rand() % 256; i++)
    {
         str[i] = rand() % 256;
    }
}

Haven't tried it. Sort of what you're looking for?

LPSECURITY_ATTRIBUTES attributetag = NULL;
string name;

for(int i = 0; i < 10; i++)
{
    generator(name);
    CreateDirectory(name.c_str(), attributetag);
    ...add files, fill them with junk using generator()...
}

...

void generator(string &str)
{
    for(int i = 0; i < rand() % 256; i++)
    {
         str[i] = rand() % 256;
    }
}

Haven't tried it. Sort of what you're looking for?

sorta but it didn't work with my code cause my code's intention is to do more then printing and creating files which have strings embedded..thanks anyway..^_^

sorta but it didn't work with my code cause my code's intention is to do more then printing and creating files which have strings embedded..thanks anyway..^_^

I found it a bit odd that you were initially using a system call to create a directory. There are functions in C to create directories. system() should almost never be used.

You can instead use mkdir() function to create the directory, however, there are conflicting definitions for it-- mainly because of Microsoft (they claim to use the POSIX mkdir() definition, but they do not).

The solution is to use a macro that auto-detects if it's being compiled on the satanic Microsoft Visual C++ compiler:

#ifdef _MSC_VER
        #include <dir.h>_
        #define mk_dir(x) mkdir(x);
#else
        #include <unistd.h>
        #include <sys/stat.h>
        #define mk_dir(x) mkdir(x,S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP)
#endif

You can break out of a loop using the "break" statement:

for(;;){
     do_something:
     if(something>=something_else)break;
}

In very rare circumstances you can use goto. This is sacrildige and produces hard to follow code, but in rare satanic instances, does the risk of burning in hell for using "goto" provide a significant improvement in performance to justify it:

for(blarg,blargett;blarg;blarg){
   while(arg){
      for(ung;arg;gasp){
            do{
               /* ahhhh! help!!! get me out of here!!! */
                goto label666; 
            }while(!egag);
      }
   }
}
label666:

Thou shalt promptly forget goto, or risk eternal damnation in coder hell (you'll be forced to make assembler function calls to the Windows GUI for all eternity).

I have a page that covers loops in more detail, which is part of a crash course to C. It can be found here:
http://coderguide.com/Guides:C/C_Crash_Course/Loops

You may also want to take a look at the whole C cash course if you're really new to programming, or to use as a reference.

A simple program on random generation of numbers

#include <stdlib.h>
#include <stdio.h>
#include <values.h>
#include <time.h>

int main(void)
{
   int i,j;

      for(j=0;j<150;j++)
      {
     // randomize();
      for(i=0;i<200;i++)
	 printf("%d\n", rand() % MAXINT);
      }
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.