PDA

View Full Version : C++ quesiton



Jeweladdict
01-18-2004, 04:56 PM
is there anyway to access the system clock to get system time an date?

i did some lookign and i havent seen any good time packages...

Tass
01-18-2004, 06:42 PM
#include <time.h>

int main()
{
time_t initial_time; // time_t is a time variable in time.h
char* timedisplay; // string to hold the time.
time ( &initial_time ); // gets the no of seconds since jan 1st, 1970 and applies it to initial_time
timedisplay = ctime ( &initial_time ); converts initial time to a real date and saves it to timedisplay.
}

Jeweladdict
01-20-2004, 03:42 PM
thx :)
i'll try it out when i get home

Jeweladdict
01-24-2004, 01:06 AM
worked like a charm :)
thx tass\


next question:

theres a directory, full of .dats, but we don't know how many, and all of various names, is there any way to find out how many .dat files are in the directory, or how to access them (path name)?


this all needs to be done within the source code

basically my prgram creates a bunch of temp .dat files, and then i need to go back and "finalize" the .dat's into .txts and so i need to be able to manipulate them.

I was thinking of dragging and dropping so that i get them int he argv, but can this take multiple files?

Not very familiar with using argv, only used it once :)

Tass
01-24-2004, 04:52 AM
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <stdio.h>

extern int alphasort(); // external function that will sort the items of an array alphabetically

int filesort(struct direct *fileitem)
{
return strcmp(fileitem->d_name, ".dat"); // will return non-zero value for any file that has a .dat in it
}

int main()
{
int fileno;
struct direct **files;
char pathname[255];

getwd(pathname); // get working directory and store it into pathname
fileno = scandir(pathname, &files, filesort, alphasort);

return 0;
}

read up on scandir() and getwd() to see how they work, as well as alphasort(), which is a built-in sorting method.

Tass
01-24-2004, 04:56 AM
come to thin kof it, that filesort may or may not work, but I found this one online:

int file_select(struct direct *entry)

{char *ptr;
char *rindex(char *s, char c);

if ((strcmp(entry->d_name, ``.'')== 0) ||
(strcmp(entry->d_name, ``..'') == 0))
return (FALSE);

/* Check for filename extensions */
ptr = rindex(entry->d_name, '.')
if ((ptr != NULL) &&
((strcmp(ptr, ".dat") == 0) ))
return 1;
else
return 0;
}

Jeweladdict
01-24-2004, 05:20 PM
haha, ok, lemme sort thru that and i'll get back to you :)

where do you look for these tass?
like how did you find that? i wouldnt even know what or where to search =/

Tass
01-24-2004, 06:16 PM
as far as finding stuff online, I just search random university beginner programming sites and I find stuff like that. But I'm also pretty good with C and I'm able to sort through huge clumps of code to find answers to what I'm looking for.

Jeweladdict
01-25-2004, 02:28 AM
wow, ok, figure this one out, its a type checking problem
===========================================
int truck2=NULL;
cout<<"\n\nEnter Truck Number to Finalize:";
cin>>truck2;
string type=typeid( truck2 ).name();
===============================
truck2=x type=int value=1... whaaa? its a char...
truck2=int = everythings fine

the problem arises when i "accidentally" use the barcode scanner to scan in a upc. input is as follows: 041167010402

a return is included. This is too big to be an int... so i tried adding this code... but still doesnt work:
==============================
if(type!="int")
{ cout<<"Your truck number is not an Integer.\n";
//insert code to reprompt for a different truck
}
=============================

i was thinking we could check if the types match, and if they do then we're ok, if not, then we just prompt for a new input.

the problem is, im not type checking right...
like with the barcode input, it won't even let me debug and see what type it returns as...



basically, i want to be able to type check to make my program idiot proof. It's expecting an int, and if we throw it a string, or a double, i want it to reprompt...
am i going about this the wrong way?


thanks sooo much for your help tass :)

Tass
01-25-2004, 10:26 AM
if (typeid( variable ) != typeid( int )) { }

Jeweladdict
01-25-2004, 02:49 PM
still didn't work, i believe my problem is that i decalre the input as an int.

int input=NULL;
cout<<"Input: ";
cin>>input;
if (typeid( input) != typeid( int )) //will this always be true?

how do you declare input that isn't of a defined type?

Tass
01-25-2004, 06:37 PM
hum... I'm not too sure... in Java, there's "object" type, which is the uber-parent type, if you will. try that in C.