Beeping sound when file changes

Pages: 12
I want to make my computer beep, when a specific file I have already created changes. The idea is this:
I have a personalized e-mail for my website.
When people send me a message, I don't know what happens at all, but at least I can create a file which leaves the comment there. How can I make a program to beep when a file changes?
If it's a console program (it shouldn't be), then you can output char(7) and on most systems (not all) it will beep.

Generally though you should either play a sound file, which involves using a sound library or multimedia library. Since your application shouldn't be a console application I'm assuming you're already using a multimedia library like SFML to display your application interface. Therefore it should be simple to play e.g. Beep.ogg
It is a combination of php and C++
I get the info from php and I want to create a little program which will run on screen all the time, wherever the file changes. I want it to beep, I don't care what kind of sound it is, so there is no big deal with the library.
@LB: of course, you do need big buttons and bright colours for a daemon.


> wherever the file changes.
┬┐have you already solved that part?
No
Detecting a file change is an OS function.
You can read more starting here:
http://stackoverflow.com/questions/931093/how-do-i-make-my-program-watch-for-file-modification-in-c

There are also OS functions to make the PC speaker beep.

Good luck!
When people send me a message, I don't know what happens at all...

This part worries me a bit. Did you not write this yourself?

...but at least I can create a file which leaves the comment there.

It's easy then, don't worry about a program that constantly runs and watches for file changes. Take the part of your program you already have, that gets called when this file you mention is made and have it play a sound after changing the file.
Is this homework or class assignment ?
No, this is an experiment for my website
I have already finished school
Donanza wrote:
When people send me a message, I don't know what happens at all, but at least I can create a file which leaves the comment there.
You could make the program beep at the same time you make the file and leave the comment there, rather than trying to track when your own program changes the file.
I have thought about that, but if I get 100 messages that would just mean 100 messages and doesn't seem very practical
I have seen other programs show a notification on the screen, even though they should be running all the time
You're not making sense. Why would you get 100 messages at once?
No I mean, the same folder will get 100 messages.
I just want a beeping sound to alert me.
So I know I have to read and reply, otherwise I wouldn't hear anything
There seems to be a disconnect in understanding.

Which part of your code saves those messages in that folder in the first place?
Look, if there is no sound, how am I supposed to know that I got a new message?
That is exactly the disconnection
I will accept $1 paypal if you want to buy me a cup of coffee, Enjoy!

PS, I didn't test it to make sure that it runs every 60 seconds, but it should update every 59-62 seconds. Hope that is good enough.


Output looks like this on screen, feel free to change it.


1
2
3
4
5
6
7
8
9
10
11
12
C:\Temp>checkforfilechange1min in.txt
Monitoring file "in.txt" for changes in file size.
Looks at file every 60 seconds and beeps if size changes.
When program starts, you should hear a beep to verify sound works.
Runs for 527040 minutes unless user stops. Use (ctrl)-C to stop.
//------------------------------------------------------------------
File Size - 9 Bytes
Uptime: (1) Minutes
Uptime: (2) Minutes
Uptime: (3) Minutes
File Updated - LastSize:9 CurrentSize:13 Bytes
/^C



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// checks filename given at runtime to see if the size changes, once per min
// sounds beep when program starts, and any time a size change occurs.

#include <fstream>		// for ifstream
#include <iostream>		// for cin, cout and cerr
#include <string>		// for the string datatype
#include <cstdlib>		// needed for the exit function
#include <sstream>		// 
#include <math.h>		// 
#include <stdio.h>		// 
#include <time.h>		// 
#include <conio.h>		// 
#include <dos.h>		// 
#include <stdlib.h>		// 
#include <time.h>		// 
#include <windows.h>	// 
#include <sys/stat.h>	// 
#include <cstring>		// 
#include <winsock.h>    // 
using namespace std;	// 

int lastfilesize=0;
int currentfilesize=0;
int loopcount=0;

void alarm()
{
     printf("\a");
}

void getfilesize(char* filename)
{
    struct stat results;
    if (stat(filename, &results) == 0)
	{
	currentfilesize=results.st_size;
	}
}

void spinner()
{
string backspace="\b";
for (int i=0;i<59;i++)
	{
	cout << "|";
	Sleep (125);
	cout << backspace;
	cout << "/";
	Sleep (125);
	cout << backspace;
	cout << "-";
	Sleep (125);
	cout << backspace;
	cout << "\\";
	Sleep (125);
	cout << backspace;
	cout << "|";
	Sleep (125);
	cout << backspace;
	cout << "/";
	Sleep (125);
	cout << backspace;
	cout << "-";
	Sleep (125);
	cout << backspace;
	cout << "\\";
	cout << backspace;
	}
}

void comparefilesize()
{
	if (currentfilesize!=lastfilesize)
	{
	alarm();
	if (loopcount==0)
	{
		cout << "File Size - " << currentfilesize << " Bytes" << endl; // Test			
	}
	else
	{
		cout << "File Updated - LastSize:" << lastfilesize << " CurrentSize:" << currentfilesize << " Bytes" << endl; // Test		
	}
	
	lastfilesize=currentfilesize;
	}
}


void programinfo()
{
cout << "Looks at file every 60 seconds and beeps if size changes." << endl;
cout << "When program starts, you should hear a beep to verify sound works." << endl;
cout << "Runs for 527040 minutes unless user stops. Use (ctrl)-C to stop." << endl;
cout << "------------------------------------------------------------------" << endl;
}

int main(int argc, char* argv[])
{
ifstream infile(argv[1]);
   if (argc != 2)
   {
      cout << "Error: Filename not given" << endl;
      cout << "Useage: " << argv[0] << " " << '"' << "Filename" << '"' << endl;
      cout << "Use quotes to prevent errors with spaces in filename." << endl;
   }
   else
   {
	cout << "Monitoring file " << '"' << argv[1] << '"' << " for changes in file size." << endl;
	programinfo();

	if (infile.good())
	{
	while(loopcount<527040) // Runs for 366 days, unless user stops
	{
	getfilesize(argv[1]);
	comparefilesize();
	spinner();
	loopcount++;
	cout << "Uptime: (" << loopcount << ") Minutes" << endl;
	}
	}
	else
	{
	cout << "Filename does not exist" << endl;
	}
}
return 0;
}
Last edited on
Donanza wrote:
Look, if there is no sound, how am I supposed to know that I got a new message?
That is exactly the disconnection
No, the disconnect is that you don't know where to play the sound. We haven't even gotten to actually playing the sound yet - that comes after you figure out where to even put the code.

@SamuelAdams: Please do not post entire code solutions. You have been warned about this in the past.
Last edited on
Are you an admin because I don't see any msgs in my in box, and that is where I would expect to see a msg from an admin.

This doesn't sound like a homework assignment to me, yes, that's my assessment which may be wrong but it's mine.

^ Please indent your code.

> Take the part of your program you already have, that gets called when this file you mention is made
Suppose that the log program is close source
Do you want to play a custom file? I can help you with the code if you'd like.
Pages: 12