|
KarstenK wrote: You are misusing the string class. Read the docs
Ah, but string deserves it. It said bad things about his mother.
But I'm not a huge fan of string for this reason. I quite like CString's implicit LPCTSTR conversion.
Iain.
In the process of moving to Sweden for love (awwww).
If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!
|
|
|
|
|
Hi I have an assignment to write a simple phone book program. I've done that & everything is working perfectly except my edit function. Can someone plz help me. Here is what I've done so far (sorry it's so long):
/*
Name: Subrina's Phone Book
Author: Subrina Bisnauth
Description: Electronic phone book
*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
void menu();
void program_start();
void add();
void edit();
void search();
void del();
void view();
void program_exit();
void DelayProg(float sec);
char name[100];
char email[50];
char phone[15];
char namebuff[50];
char lnamebuff[50];
char buf[80];
int count;
FILE *phonebook;
FILE *pbtemp;
time_t now;
struct tm *ts;
/*Main Function*/
int main()
{
system("CLS");
program_start();
menu ();
view();
return 0;
}
/*Menu Function*/
void menu()
{
int ch = 0;
char ch2;
program_start();
printf("\t\t Please choose one . . .\n\n");
printf("\t\t\t1: < Add to phonebook >\n");
printf("\t\t\t2: < Edit phonebook >\n");
printf("\t\t\t3: < Search phonebook >\n");
printf("\t\t\t4: < Remove from phonebook >\n");
printf("\t\t\t5: < View phonebook >\n");
printf("\t\t\t6: < Exit phonebook >\n\n");
printf("\t\t\t\tPlease enter the number of your choice: ");
fflush(stdin);
scanf("%d", &ch);
switch(ch)
{
case 1:
add();
case 2:
edit();
case 3:
search();
case 4:
del();
case 5:
view();
case 6:
program_exit();
default:
program_start();
printf("\t\tYou must choose 1 - 5. Retry? Y/N ");
fflush(stdin);
scanf("%c", &ch2);
if(ch2=='y'||ch2=='Y')
{
menu();
}
else
{
program_exit();
}
}
}
/*Function executed at program start-up*/
void program_start()
{
system("CLS");
FILE *file;
}
/* Function that allows new entries to be made in the phonebook*/
void add()
{
int numbers[100];
char choice;
do {
int Exists = 0;
program_start();
printf("\t\t- Add to Phonebook -\n\n");
printf("\n\t\t\tEnter Full Name: ");
fflush(stdin);
gets(name);
strcat(name, "|");
strcat(name, "\n");
phonebook = fopen("phonebook.txt","a+");
while(!feof(phonebook) && Exists==0)
{
fgets(namebuff, 50, phonebook);
if(strcmp(name, namebuff)==0)
{
printf("\n\t\tThat entry is already stored in the phonebook . . .");
printf("\n\n\t\t\t");
system("PAUSE");
Exists=1;
}
}
if(!Exists)
{
ts = localtime(&now);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", ts);
printf("\n\t\t\tEmail : ");
gets(email);
fflush(stdin);
printf("\n\n\t\t\tPhone Number: ");
gets(phone);
fflush(stdin);
fprintf(phonebook, "%s%s\n%s\n\n%s\n%s\n", name, email, phone, buf);
}
fclose(phonebook);
program_start();
printf("If you would like to continue press Y or if you want to return to the menu press N\n ");
fflush(stdin);
scanf("%c", &choice);
if(choice!='y'&&choice!='Y')
{
menu();
}
}while(choice=='y'||choice=='Y');
}
/*Function to search for a particular entry in the phonebook*/
void search()
{
char choice;
int length;
do {
int Exists = 0;
int i = 1;
program_start();
printf("\t\t- Search phonebook -\n\n");
printf("\n\t\t\tEnter Full Name: ");
fflush(stdin);
gets(name);
strcat(name, "|");
strcat(name, "\n");
phonebook = fopen("phonebook.txt","r");
while(!feof(phonebook) && Exists==0)
{
fgets(namebuff, 50, phonebook);
if((strcmp(namebuff, name)==0))
{
Exists = 1;
while(i<6)
{
if(i==1)
{
fgets(email, 50, phonebook);
fflush(stdin);
}
if(i==5)
{
fgets(phone, 15, phonebook);
fflush(stdin);
}
i++;
}
}
}
if(Exists)
{
name[strlen(name)-2]=' ';
printf("\n\t\tSuccess . . .\n\n");
printf("\t\tName : %s\n", name);
printf("\t\tEmail : %s\n", email);
printf("\t\tPhone Number: %s\n", phone);
printf("\n\n\t\t\t");
system("PAUSE");
fflush(stdin);
}
else
{
length = strlen(name);
name[length-2] = '\0';
printf("\n\t\tSorry . . .\n\n");
printf("\t\t\"%s\" was not found in phonebook.", name);
printf("\n\n\t\t\t");
system("PAUSE");
fflush(stdin);
}
fclose(phonebook);
program_start();
printf("If you would like to continue press Y or if you want to return to the menu press N\n ");
fflush(stdin);
scanf("%c", &choice);
if(choice!='y'&&choice!='Y')
{
menu();
}
}while(choice=='y'||choice=='Y');
}
/*Function to edit entries in the phonebook*/
void edit()
{
char choice;
int length;
do {
int Exists = 0;
int i = 1;
int g;
char ph[6];
char nam[25];
char e_mail[15];
char tel[15];
program_start();
printf("\t\t- Edit an entry -\n\n");
printf("\n\t\t\tEnter Full Name: ");
fflush(stdin);
gets(name);
strcat(name, "|");
strcat(name, "\n");
phonebook = fopen("phonebook.txt","r+");
while(!feof(phonebook) && Exists==0)
{
fgets(namebuff, 50, phonebook);
if((strcmp(namebuff, name)==0))
{
Exists = 1;
while(i<6)
{
if(i==1)
{
fgets(email, 50, phonebook);
fflush(stdin);
}
if(i==5)
{
fgets(phone, 15, phonebook);
fflush(stdin);
}
i++;
}
}
}
if(Exists)
{
name[strlen(name)-2]=' ';
for (g=0;g<count;g++)
{
printf("\n\tPlease enter what you would like to edit - name, e-mail or tel");
scanf("%s", &ph);
if(ph=='name')
{
printf("\n\t\tPlease enter the correct name");
scanf("%s",&nam);
name[g]=nam;
}
if(ph=='e-mail')
{
printf("\n\t\tPlease enter the correct e-mail address");
scanf("%s",&e_mail);
email[g]=e_mail;
}
if(ph=='tel')
{
printf("\n\t\tPlease enter the correct telephone #");
scanf("%s",&tel);
phone[g]=tel;
}
}
}
else
{
length = strlen(name);
name[length-2] = '\0';
printf("\n\t\tSorry . . .\n\n");
printf("\t\t\"%s\" was not found in phonebook.", name);
printf("\n\n\t\t\t");
system("PAUSE");
fflush(stdin);
}
fclose(phonebook);
program_start();
printf("If you would like to continue press Y or if you want to return to the menu press N\n ");
fflush(stdin);
scanf("%c", &choice);
if(choice!='y'&&choice!='Y')
{
menu();
}
}while(choice=='y'||choice=='Y');
}
/*Fuction to delete entries from phonebook*/
void del()
{
char conf;
int Exist = 0;
int len;
int i = 1;
program_start();
printf("\t\t- Delete From phonebook -\n\n");
printf("\n\t\t\tEnter Full Name: ");
fflush(stdin);
gets(name);
strcat(name, "\n");
if(phonebook = fopen("phonebook.txt", "r"))
{
printf("\n\n\t\tDelete the selection? Y/N ");
scanf("%c", &conf);
if(conf=='y'||conf=='Y')
{
if(pbtemp = fopen("phonebook", "w"))
{
while(!feof(phonebook))
{
fgets(namebuff, 50, phonebook);
len = strlen(namebuff);
if(namebuff[len-2]=='|')
{
namebuff[len-2]='\n';
namebuff[len-1]='\0';
if(strcmp(name, namebuff)!=0)
{
while(i<6)
{
if(i==1)
{
fgets(email, 50, phonebook);
fflush(stdin);
}
if(i==5)
{
fgets(phone, 15, phonebook);
fflush(stdin);
}
i++;
}
namebuff[len-2]='|';
namebuff[len-1]='\n';
fprintf(pbtemp, "%s%s%s", namebuff, email, phone);
}
else if((strcmp(name, namebuff)==0))
{
Exist = 1;
}
}
}
if(!Exist)
{
fclose(pbtemp);
fclose(phonebook);
len = strlen(name);
name[len-1]='\0';
program_start();
printf("\t\t- Delete From phonebook -\n\n");
printf("\t\tSorry . . .\n\n");
printf("\t\t\"%s\" was not found in phonebook.\n\n", name);
printf("\t\t\t");
system("PAUSE");
fflush(stdin);
remove("phonebook.txt");
menu();
}
}
else
{
fclose(phonebook);
fclose(pbtemp);
program_start();
printf("\t\t- Delete From phonebook -\n\n");
printf("\t\t\tError . . .\n\n");
printf("\t\t\tThe file could not be opened.\n\n");
printf("\t\t");
system("PAUSE");
fflush(stdin);
menu();
}
fclose(pbtemp);
fclose(phonebook);
len = strlen(name);
name[len-1]='\0';
remove("phonebook.txt");
rename("pbtemp.txt", "phonebook");
program_start();
printf("\t\t- Delete From phonebook -\n\n");
printf("\t\t\t\"%s\" was deleted from phonebook.", name);
printf("\n\n\t\t");
system("PAUSE");
fflush(stdin);
view();
}
else
{
fclose(phonebook);
len = strlen(name);
name[len-1]='\0';
program_start();
printf("\t\t- Delete From phonebook -\n\n");
printf("\t\t\t\"%s\" was not deleted.", name);
printf("\n\n\t\t");
system("PAUSE");
menu();
}
}
else
{
program_start();
printf("\t\t- Delete From phonebook -\n\n");
printf("\t\t\tError . . .\n\n");
printf("\t\t\tThe file could not be opened.\n\n");
printf("\t\t");
system("PAUSE");
menu();
}
}
/*Function that allows user to view entries in phonebook*/
void view()
{
namebuff[0] = '\0';
int len;
phonebook = fopen("phonebook.txt", "r");
program_start();
printf("\t\t- View phonebook -\n\n");
while(!feof(phonebook))
{
int i = 1;
fflush(stdin);
fgets(namebuff, 50, phonebook);
len = strlen(namebuff);
if(namebuff[len-2]=='|')
{
while(i<6)
{
if(i==1)
{
fgets(email, 50, phonebook);
fflush(stdin);
}
if(i==5)
{
fgets(phone, 15, phonebook);
fflush(stdin);
}
i++;
}
namebuff[len-2]=' ';
printf("\n\t\t\tName : %s", namebuff);
printf("\n\t\t\tEmail : %s", phone);
printf("\n\t\t\tPhone Number: %s\n", phone);
}
}
printf("\n\n\t\t\t");
system("PAUSE");
fclose(phonebook);
menu();
}
/*Function executed when program is about to close*/
void program_exit()
{
fflush(stdin);
program_start();
printf("\t\t\tThank you for using phonebook . . .");
DelayProg(2);
}
/*Function that controls the time delay when program is about to exit*/
void DelayProg(float sec)
{
float t = GetTickCount() + (1000L*sec);
while(GetTickCount() < t)
{
}
exit(1);
}
/*End of Program*/
|
|
|
|
|
Subrina Bisnauth wrote: (sorry it's so long):
So then fix it. If you already know the problem is with edit() , then post just that code. All the other is just useless noise.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hi All,
I am making a change to some ancient WinSock legacy code. What I am trying to do is modify the server code to have the server pass messages received from all the clients to a special “reporting client”. It appears that when I receive a message from a client and try to pass the message along to the “reporting client”, that the message is never sent (or never received by the “reporting client”). I am using the standard send() command with the socket number of the “reporting client”. Could the problem be that I’m currently in the thread to communicate with one client when I’m trying to do the send() to another? I'd appreciate any insights anyone might have. Thanks in advance.
|
|
|
|
|
Is it something like this?
Thread1: Server Listening on port A (socket 1)
Thread2-10: Client Doing stuff, sending to the server on port A from ports B-J (socket 2-10)
Thread11: Server Reporting Client listening on port I (socket 11)
I think what I read was that you're in Thread1 trying to use send() with socket 11. That won't work, you should use send() with socket 1 in Thread1.
If the threads are _sharing_ sockets, then you should have some sort of synch object to keep them from randomly failing in race conditions.
And if you have two listening threads, they should probably be on two different ports. Hope that helps.
|
|
|
|
|
Bulg,
Thanks for your reply. I think my previous explanation of the issue was lacking (I’m a complete newbie to sockets/threads [i.e., I don’t think I’m fully aware of my ignorance]). I’ll use a diagram to attempt to clarify:
Client 1
|
| Client 2
| |
V V ...
----------
| Server | ---> Reporting Client
----------
^
|
|
Client X
The standard Clients 1 through X are running tests and reporting their statuses to the Server. The Server is then passing a subset of these status messages on to the Reporting Client.
The Server sees a status message coming from one of the Clients and immediately does a send() using the socket number of the Reporting Client, but these messages are not being seen at the Reporting Client and I’m wondering if the problem is that I’m currently in the thread that is communicating with Client X when I’m trying to do a send() to the Reporting Client.
Thanks again for your help.
|
|
|
|
|
I like your diagram, I'm gonna copy it and add stuff to it
Client 1
Socket 1
|
| Client 2
| |Socket 2
| |
V V
Socket S
^ |
| V
| Server -> Socket R -----> Socket 3 -> Reporting Client
|
|
|
|
Socket X
Client X
Each listening socket must be bound (bind() ) to a port number. A process that wants to send information to a listening socket, as far as I know, must use a different socket to send information to the listening socket.
So when you say socket number, I know that sockets are returned from socket() as integers, and I know that listening sockets are bound to port numbers, also integers. Which of those two is a "socket number"?
some code:
...
if(bind(serv_sock, (struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0){
}
if(listen(serv_sock,MAXPENDING)<0){
}
for(;;) {
if((client_sock = accept(serv_sock,(struct sockaddr*)client_addr, sizeof(client_addr)) < 0){
HandleTCPClient(client_sock)
}
}
...
if(bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
}
for(;;) {
if(recv_size = recvfrom(serv_sock, buffer, MAX_BUFFER, 0, (struct sockaddr*)&client_addr, sizeof(client_addr)) < 0) {
sendto(serv_sock, buffer, recv_size, 0, (struct sockaddr*)&other_addr, sizeof(other_addr));
} I admit that the for(smileyface) may be misleading at first.
|
|
|
|
|
Bulg,
First, thanks for taking time to look into this.
The socket number I'm referring to is the socket number returned from the accept() call:
SOCKET tempSock = accept(aArgs->socket, (LPSOCKADDR)&SockAddr, &mLen);
and then passed into the send() call:
int iRet = send(ReportingClientData.CurrentSocket, dlgMessage, COMMANDSIZE, 0);
Keep in mind that this is ancient WinSock code.
|
|
|
|
|
This is just a general question looking for suggestions. I want to use Visual C++ 6.0 to create an application that will allow me to pass traffic from the internet to a special USB device I have designed. Presently I can send to and get responses from the USB device using the code development tools.
My thinking is to create an internet application on the PC side using a port above 1024. By doing that I don't have to get the remote end user to make any changes on their router. I was thinking of maybe creating something similar to a telnet server or maybe a chat client/server.
I will just be passing small commands over the internet which will then be sent out the USB port on the PC to my embedded application. The USB device will just return a "Success" or "Failure" response. Does anyone have any suggestions as what would be the best approach to this?
Thank you.
Ray
|
|
|
|
|
"internet" programming is done using "sockets". If you look them up that should give you quite a few good starting points.
|
|
|
|
|
bulg wrote: "internet" programming is done using "sockets".
Yes, I know that. I was more interested in suggestions about which type of "server" would be best for me to create to handle the incoming traffic. I know I can open a socket and retrieve the data and write it to a buffer. Once I have the data, I guess I could then pass it to the USB device. Or write it to a file and have the USB device read it in.
|
|
|
|
|
Hrmm, then I'm going to try to guess that your question could be narrowed down a little bit more:
How does a server make and keep track of multiple clients and sessions ( aka, client logs in, interacts with the system, logs out)?
|
|
|
|
|
No, not that complex. Only 1 client (me) and one session (mine). I will call the customer, have them start the application that I write, and then connect to the application. Once connected I will send a couple of commands to my embedded device, wait for an answer, and disconnect.
I'm thinking something similar an automated telnet session except I will write it. I don't won't to depend on, or mess up, anything on the remote computer. Or maybe some type of RPC call. Just kicking ideas around in my head for now. The code won't be too difficult. I just need to decide how to do it.
|
|
|
|
|
Your two things (baby client / server) and (talk to my usb device) are really quite separate problems.
I embedded a simple 1 to 1 server in my main software for a PDA to communicate with over TCP/IP. I blatantly used CCESocket: a general purpose TCP/UDP socket class for WinCE[^] to great effect.
Where you will have to be careful is that is handles packets in a separate thread to the main UI. That's not an issue, you just have to keep your eyes open. And it's better than using the abominations that are the built in classes.
On to the USB part... You may already be doing it, but my article Detecting when drives are added or removed[^] could be of use. I initially wrote a version that detected USB chipsets from FTDI being added / removed.
Good luck,
Iain.
In the process of moving to Sweden for love (awwww).
If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job! http://cv.imcsoft.co.uk/[ ^]
|
|
|
|
|
Lain, thanks for pointing out the article. I will pursue that. I was going to use the built in classes and just put up with the overhead. This looks like a much better approach.
I am already detecting my USB device being plugged in. I am using the Microchip USB stack and developed my device using one of their tools. Once I can talk to it over the local LAN I think the rest of it will be easy.
Ray
|
|
|
|
|
I am working on a set class and I have successfully written an insert function, but I need the function to insert in an ordered fashion.
bool Set::insert( const EType & A )
{
bool Flag = false;
unsigned Position = 0;
Node * Prev = Head;
Node * Curr = Head->Succ;
Node * Temp;
Temp = new Node;
if (Temp != NULL )
{
Temp->Item = A;
Temp->Succ = Curr;
Prev->Succ = Temp;
Num++;
Flag = true;
}
return Flag;
}
Set::Set()
{
Num = 0;
Head = new (nothrow) Node;
Head->Succ = NULL;
}
class Set
{
private:
struct Node
{
EType Item;
Node * Succ;
};
unsigned Num;
Node * Head;
Tell me if anything else is needed
|
|
|
|
|
This smells of a linked list. See here for pseudo code.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Does anyone have an idea how I can detect if a file has been copied?
I'm trying to develop a simple (hopefully) protection scheme that'll allow me to distribute a license file to a customer, and our application will read the file to update their license.
What I don't want is for the client to be able to copy the license file and 'give' it to another of our customers and avoiding our licensing scheme.
Right now, our application reads the license file and then modifies it so that it's no good any more, so copying it after the fact wouldn't work...I'd like to prevent someone from copying the original un-altered file before it's used.
I've tried to use the GetFileTime() function to get the Creation, LastAccessed, and LastWrite times of the file, but these don't appear to change for a copy of the file.
Any suggestions/ideas?
Steve
|
|
|
|
|
What criteria is used to generate the license in the file (e.g., HDD serial number, MAC address of NIC, license/registration key of Windows)? When the file is copied from one machine to another, check that criteria, compare it to what is in the file. If they do not match, don't allow usage of your software.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
The license file is a simple text file (scrambled so it's not obvious what information is what).
What I want to do is to create the file on my computer and distribute it to customers (on a floppy/CD/Flash Drive). When they run our app (which is not copy protected), it will look for and read the new license file and extract the information from it. It then re-writes the file so it can't be used again.
We do not record any hardware specifics of the installation, so I can't 'key' the license file to a specific machine.
What I'd hoped to do was to record (in the file) the date of it's creation...then, when our app reads the file, it checks the date stamp on the file and if it didn't match, it wouldn't apply the license. Alas, when I make a copy of the license file, all the dates I can find (Creation, Last Accessed, and Last Written) appear to copy too.
Are there any attributes of a copied file that are different from the original that I can use to detect an unauthorized copy?
|
|
|
|
|
I have seen lots of protection schemes, all free ones are (almost) worthless.
The best solutions are:
1. no protection at all (no hassle for the user, no waste of time for the developer; yes you loose some customers who would not have paid for your product anyway);
2. hardware-dependent protection
2a. encode some characteristic (baseboard number, MAC address, disk number) in a license file; disadvantage: lots of hassle; customers don't like their software to stop functioning when they change some hardware, possibly for good reasons.
2b. idem, with server access instead of license file; disadvantage: same plus needs internet every time.
2c. deliver special hardware device (dongle). Costs ca 30$
2d. link to the serial number of a USB stick (best of all these). Still the hassle.
steve76063 wrote: Are there any attributes of a copied file that are different from the original that I can use to detect an unauthorized copy?
There is one: the physical sector number on disk. That is how some programs used to get protected on a floppy or CD. But then you could make binary copies of those disks (before first use).
And good luck if you want your app, run by a non-admin, find out its sector number on modern Windows versions (Vista, 7).
|
|
|
|
|
Well, I may have found what I've been looking for...
Keep in mind, I'm not looking for a foolproof copy protection scheme--there's no such thing. We've used dongles before, but it gets cumbersome--especially when the client's computer already has 4 or 5 other dongles already attached!
I was just looking for a quick simple way to prevent unauthorized copying of my file.
I've found out that when I CreateFile() I can use the FILE_ATTRIBUTE_OFFLINE for the file and it's file icon is a light blue X, and it's Attribute is listed as O. If I copy the file to another location, it loses it's 'O' attribute! I'm can test for the attribute using the GetFileInformationByHandle() and it works just fine.
I'm just trying to deter casual copiers, not dedicated 'crackers', so this should be 'good enough'.
One caveat that I've just discovered is that the Offline attribute is only for NTFS drives, so that leaves out distributing my file via Floppy or CD...it'll have to be an NTFS formatted Flash Drive.
modified on Monday, April 13, 2009 6:21 PM
|
|
|
|
|
|
Hi All
When file is deleted from drive then some where present in hard disk.i want to know where is present in hard disk.Can any one tell me where is present file in hard disk.There is any API or document.
plz help me
|
|
|
|
|
At the file system level, a file does not get moved when it is deleted. The file system simply marks its space as available.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|