Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Reply
 
Thread Tools
Old 09-06-07, 11:22 AM   #1
six_storm
Registered User
 
six_storm's Avatar
 
Join Date: Jun 2004
Location: Nashvegas
Posts: 11,575
Send a message via Skype™ to six_storm
Default C++ Gurus, School Me :D

Hello all fellow programmers. I'm having a hard time understanding the "time.h" file and it's uses. Currently, I've got a project that is supposed to show the time difference between reading/writing from RAM and reading/writing from disk. My program already performs all 4 functions, I just have to use the "time.h" file to show how long it takes and display it in an external file. So how to you start and stop this "measure of time"? I understand how to convert the number of ticks over to seconds (duh) but I just don't understand how to start and stop timing each function. Can anyone please explain this to me in ENGLISH please? Thanks!

BTW, here were the notes I got on "time.h":

Code:
 	
time.h
	
	
									
• 	To determine the amount of time a section of code takes to run, use the
	following code
	
• 	#include<time.h>
	
	float timer;
	
	timer = clock();              //clock() returns the time in terms of ticks
	
	timer = clock() - timer;  //Gives the elapsed time in ticks
	
	timer /= CLOCKS_PER_SEC;
	
	                                     //Converts ticks to seconds
	
	timer /= 60;                   //Converts seconds to minutes
six_storm is offline   Reply With Quote
Old 09-06-07, 02:47 PM   #2
AndyS
Registered User
 
Join Date: Feb 2007
Posts: 23
Default Re: C++ Gurus, School Me :D

I'm not sure whether I understand your question, but if you want to measure time consumed by calling some function you can simply use the clock() function for it (as the example stated)

clock() returns number of ticks since the program started. So:
1. you store the no. of ticks before the function call
beginTime = clock();

2. call the function
funcBlaBla();

3. measure no. of ticks after the function returned ..
functionTime = clock() - beginTime;
..and of course transfer the ticks into seconds

..
also note that if you want to measure writing/reading performance of some device you should do it be writing and reading some larger amount of data ..not just few bytes (and there can be also differences in sequential access and random access but I that don't bother you right now )
AndyS is offline   Reply With Quote
Old 09-06-07, 05:49 PM   #3
six_storm
Registered User
 
six_storm's Avatar
 
Join Date: Jun 2004
Location: Nashvegas
Posts: 11,575
Send a message via Skype™ to six_storm
Default Re: C++ Gurus, School Me :D

Thanks a lot man! It really helped. I've never used the "time.h" library before . . . Just as long as it can measure the time it takes to read/write from RAM and HDD, and display them out into an external file, I'm all good. Thanks again.
six_storm is offline   Reply With Quote
Old 09-20-07, 08:20 PM   #4
six_storm
Registered User
 
six_storm's Avatar
 
Join Date: Jun 2004
Location: Nashvegas
Posts: 11,575
Send a message via Skype™ to six_storm
Default Re: C++ Gurus, School Me :D

I'd hate to make another thread but I have another question. CHAR ARRAYS.

So I need to make a char array in a struct and have it read in from a file. HEre is my code:

Code:
struct myStruct
{
char *name[60];
};

main()
{
int x=0;
myStruct mainStruct;
ifstream inFile;
inFile.open("input.txt");

while(!inFile.eof())
{
inFile >> mainStruct.name[x];
x++
}

return 0;
}
I keep getting errors in the syntax but I can't figure out what it is. Any hints would be appreciated.
six_storm is offline   Reply With Quote
Old 09-20-07, 09:09 PM   #5
evilghost
Registered User
 
Join Date: Jul 2005
Posts: 3,606
Default Re: C++ Gurus, School Me :D

Note, I no longer own the domain stickit.nu, the site is now NSFW. This code may help you, I wrote it a while back for use with qmail in .qmail files for |condredirect I believe.

Code:
// Spamfilter by evilghost
//
// Version 4.1 Beta
// Release date March 19, 2003
//
// Spam Filter Beta 4.1 - with wildcard domain support.
// Modified by evilghost@stickit.nu Nov 28, 2001
//      + Added default exit code of 1.
// Modified by evilghost@stickit.nu Feb 09, 2002
//      + Added Single-line denied log file syntax
//      + Modified invalid argument output to fit on screen
//      + Stripped newline character from datet() function
//      + Added tab delimitted log output
//      + Added comment support with '#' character in spamlist.txt
//      + Added valid spam address filter testing
// Modified by evilghost@stickit.nu July 03, 2002
//      + Added subdomain support, syntax is '@*.domain.com'
// Modified by evilghost@stickit.nu March 19, 2003
//      + Enabled Debug Mode for $SENDER and $RECIPIENT arguments
//      + Removed strlwr() function and included in main functions due to pointer issues.

#include <fstream.h>
#include <string>
#include <time.h>
#include <ctype.h>

   void print_log(int, char*, char*, char*, char*);
   void check(char*, char*, char*, char*);
   char* datet();
   string ddate;        //Added in 4.01

   void main(int argc, char* argv[]){
      if((argv[1] == NULL) || (argv[2] == NULL) || (argv[3] == NULL) || (argv[4] == NULL)){
         cout << "Spamfilter Beta 4.1\n";
         cout << "Usage: " << argv[0] << " [sender] [recipient] [/path/to/spamlist] [/path/to/logfile] <debugfile>\n";
         cout << "Report bugs to evilghost@stickit.nu\n";
         exit(1);
      }else {
         //Debugging output.
         if(argv[5] != NULL){
           ofstream debugfile;
           debugfile.open(argv[5], ios::app);
           if(debugfile.fail()){
             debugfile.open(argv[5], ios::out);
               if(debugfile.fail())
                  cerr << "Cannot find nor create " << argv[5] << "\n\a";
           }
           ddate = datet();
           debugfile << ddate + " " + "Sender: " + argv[1] + " Recipient: " + argv[2];
           debugfile << endl;
           debugfile.close();
         }

         char* sender = argv[1];
         char* recipient = argv[2];
         char* global = argv[3];
         char* logfile = argv[4];
         check(sender, recipient, global, logfile);
      }
   }

   void print_log(int a, char* sender, char* recipient, char* logfile){
      ofstream log;
      log.open(logfile, ios::app);
      if(log.fail()){
         log.open(logfile, ios::out);
         if(log.fail())
            cerr << "Cannot find nor create " << logfile << "\n\a";
      }
      if(a == 1){
        ddate = datet();
        log << ddate + "\tDenied " << sender << " to " << recipient;
      }
      if(a == 2)
         log << "Cannot access global list!";

      log << endl;
      log.close();
   }


   void check(char* sender, char* recipient, char* global, char* logfile){
      string spamaddr;
      int maxlen = 0;
      int x=0;

      //Lowercase sender
      x=0;
      while(x < strlen(sender)){
         sender[x] = tolower(sender[x]);
         x++;
      }

      ifstream glist(global);
      if(glist.fail()){
         print_log(2, sender, recipient, logfile);
         exit(1);
      }

      while(glist >> spamaddr){
        //Lowercase SpamAddr
        x=0;
        while(x < strlen(spamaddr.c_str())){
          spamaddr[x] = tolower(spamaddr[x]);
           x++;
        }

        if(spamaddr[0] != '#' && strlen(spamaddr.c_str()) > 2){
          if(spamaddr[0] == '@'){
            if(spamaddr[0] == '@' && spamaddr[1] == '*' && spamaddr[2] == '.') //Adjust the wildcard for subdomain matching, added in 4.02
               spamaddr.erase(0,2);
            if(strstr(sender,spamaddr.c_str())){
               print_log(1, sender, recipient, logfile); // log spam attempt
               exit(0);
            }
          }else{
             maxlen = strlen(sender);
             if((strlen(spamaddr.c_str())) > (strlen(sender)))
               maxlen = strlen(spamaddr.c_str());

             if(!strncasecmp(sender,spamaddr.c_str(),maxlen)){
               print_log(1, sender, recipient, logfile); // log spam attempt
               exit(0);
             }
            }
          }
      }
      glist.close();
      exit(1);
   }

   char* datet(){
      char* dt;
      time_t timer;
      struct tm *tblock;
      timer = time(NULL);
      tblock = localtime(&timer);
      dt = asctime(tblock);
      return strtok(dt,"\n");   //Added in 4.01, return the string minus the newline character.
   }
Code:
#Sample Deny Spamlist.txt
#Comments are prefixed with the '#' character.

#This is an example of a domain filter
#Deny all messages from this domain
@foo.fr
@foo.br

#This is an example of a account filter
john@foo.com
sexyjen1287@foo.com

#This is an example of a subdomain filter
#Deny domains like mail.foo.com, groups.foo.com, etc.
@*.foo.com
@*.foo2.com
evilghost is offline   Reply With Quote
Old 09-21-07, 05:09 AM   #6
picofarad
Registered User
 
Join Date: Nov 2003
Posts: 14
Default Re: C++ Gurus, School Me :D

Hi six_storm

The problems with that code is that it isn't indented properly. That always ruins everything.

Always check all your statements are terminated (;'s) and that all your functions have a return type specified.

You also need to watch out for namespaces. Everything in the standard c++ libraries is in the std namespace. To use something from a namespace you just scope it by going std::ifstream for example.

The below code is syntactically ok (well, it compiles at least) but all you have is an uninitialized array big enough to hold 60 pointers...where does the data streamed from the inFile go?

Code:
#include <fstream>

struct myStruct
{
   char *name[60];
};

int main()
{
   int x=0;
   myStruct mainStruct;
   std::ifstream inFile;
   inFile.open("input.txt");

   while(!inFile.eof())
   {
      inFile >> mainStruct.name[x];
      x++;
   }

   return 0;
}
picofarad is offline   Reply With Quote
Old 09-21-07, 09:09 AM   #7
six_storm
Registered User
 
six_storm's Avatar
 
Join Date: Jun 2004
Location: Nashvegas
Posts: 11,575
Send a message via Skype™ to six_storm
Default Re: C++ Gurus, School Me :D

Quote:
Originally Posted by picofarad
Hi six_storm

The problems with that code is that it isn't indented properly. That always ruins everything.
My code is on a remote server and I couldn't copy and paste at the time, therefore I posted the necessary code in CODE quotations. I couldn't indent.

I'm just going to talk to my teacher today after class and maybe I'll get some help there. My assignment is already one day late lol. Thanks for the help though!
six_storm is offline   Reply With Quote
Old 09-21-07, 10:57 AM   #8
six_storm
Registered User
 
six_storm's Avatar
 
Join Date: Jun 2004
Location: Nashvegas
Posts: 11,575
Send a message via Skype™ to six_storm
Default Re: C++ Gurus, School Me :D

Ok, I got everything worked out. Now my problem is indentation. I'm supposed to take an input file and meet the following conditions when outputting to the screen and output.txt:

1) When a "{" passes thru, go to the next line and indent 6 spaces.
2) When a "}" passes thru, go to the next line and dedent 6 spaces.
3) If you have a nested or two "{", go to next line and indent 12 spaces.

What I can't figure out is how can you tell your program where the end of the line is to "endl;". Any takers? TIA.
six_storm is offline   Reply With Quote

Old 09-22-07, 06:49 PM   #9
bugmeplz
*BANNED*
 
bugmeplz's Avatar
 
Join Date: Sep 2006
Posts: 427
Default Re: C++ Gurus, School Me :D

Quote:
Originally Posted by six_storm
Ok, I got everything worked out. Now my problem is indentation. I'm supposed to take an input file and meet the following conditions when outputting to the screen and output.txt:

1) When a "{" passes thru, go to the next line and indent 6 spaces.
2) When a "}" passes thru, go to the next line and dedent 6 spaces.
3) If you have a nested or two "{", go to next line and indent 12 spaces.

What I can't figure out is how can you tell your program where the end of the line is to "endl;". Any takers? TIA.
Please pastebin a sample input and what the resulting output should be like so I can better understand what you mean.

http://pastebin.com/ (select c++ syntax highlighting for easier reading)
bugmeplz is offline   Reply With Quote
Old 09-22-07, 10:50 PM   #10
Imbroglio
xeroyphyte
 
Imbroglio's Avatar
 
Join Date: Mar 2004
Posts: 1,271
Default Re: C++ Gurus, School Me :D

Quote:
Originally Posted by six_storm
Ok, I got everything worked out. Now my problem is indentation. I'm supposed to take an input file and meet the following conditions when outputting to the screen and output.txt:

1) When a "{" passes thru, go to the next line and indent 6 spaces.
2) When a "}" passes thru, go to the next line and dedent 6 spaces.
3) If you have a nested or two "{", go to next line and indent 12 spaces.

What I can't figure out is how can you tell your program where the end of the line is to "endl;". Any takers? TIA.
why not just use getline? and then parse it per the tokens you want?

syntax is getline(stream, string var);

the function reads the entire line until '\n' is hit and puts it into the string variable (if i remember correctly '\n' is trunced from the end of the string, so you may or may not need to add it depending on formatting specs)
__________________

HT: 360 -- Sony60A3000 -- Onkyo SR605 --> Polk cs2, monitor 60s and monitor 40s w/ Bic F12 sub -- PS3
XBL:Mr X3r0 -- PSN:Mr_X3r0

Rig- Asus P6X58D Premium - intel i7 920 @3.6 - EVGA GTX570 - Corsair HX1000 - Prolimatech Mega Shadow - WD640 - COOLER MASTER ATCS 840 - OCZ Gold 6GB DDR3 1600*

Imbroglio is offline   Reply With Quote
Old 09-23-07, 07:58 AM   #11
six_storm
Registered User
 
six_storm's Avatar
 
Join Date: Jun 2004
Location: Nashvegas
Posts: 11,575
Send a message via Skype™ to six_storm
Default Re: C++ Gurus, School Me :D

Quote:
Originally Posted by Imbroglio
why not just use getline? and then parse it per the tokens you want?

syntax is getline(stream, string var);

the function reads the entire line until '\n' is hit and puts it into the string variable (if i remember correctly '\n' is trunced from the end of the string, so you may or may not need to add it depending on formatting specs)
I changed it to a getline and then did a strcpy. I finally got 95% of the program to work correctly, but my indentation didn't turn out so well. Most of the time, my C++ problems are just a stupid bracket or colon lol. Thanks anyways guys!
six_storm is offline   Reply With Quote
Reply


Thread Tools

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 On
HTML code is Off
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ground Branch - Old school hardcore military shooter kickstarter FastRedPonyCar Gaming Central 3 06-23-12 12:21 PM
Bring Your Valve To School Day: Teach With Portals News Gaming Headlines 0 06-21-12 08:30 AM
nvidia: We're helping build a new sports field 4 Shishiori Junior High School kids in News Archived News Items 0 05-25-12 08:00 AM

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


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.