|
How to delete a file outside of main( ) & How to change mouse speed |
Seraph6
Member #7,517
July 2006
|
I'm almost done with my first original game, but I have two little problems that I'm hooked up on. 1. In trying to delete a file, the remove() function isn't working in an if() or switch() statement or outside of main(). I want to be able to do this outside of main(), in its own function, but I've searched and searched and I can't find a single example program of that, even in C programming books. The way I'm using it is: "remove("mb1.txt");" Apparently it is returning a fail on the delete, but I have no clue as to why. If I put that same line of code in main() with no if() or switch() statements, it'll delete it. But that's useless. EDIT: Found a solution to my second question. Thanks allegro.cc |
Steve Terry
Member #1,989
March 2002
|
I only see one question??? Anyway can you show me your code that fails. ___________________________________ |
count
Member #5,401
January 2005
|
He removed it, because he solved it.
|
Seraph6
Member #7,517
July 2006
|
I call the function from another function in the program similar to this: for(n=0; n<4; n++) if((cursor->x >= sframe[n]->x + 120) && (cursor->y > sframe[n]->y) && (cursor->x < sframe[n]->x + 200) && (cursor->y < sframe[n]->y + 125) && (mouse_b & 1)) trash_file(n); And the actual function trash_file(int x) is like so:
I had it set up before so that it would return an error and exit the program if the deletion didn't work. But I guess I deleted that code. |
Steve Terry
Member #1,989
March 2002
|
Your code looks ok, not sure what would cause it to fail. How exactly is it failing? Is it not compiling or is the code just not doing anything? Try "./mb1.txt". ___________________________________ |
Seraph6
Member #7,517
July 2006
|
The code just isn't doing anything. The program will compile and run without any errors or warnings. I also have another action occur when the user clicks that is grouped with the trash_file(int x), and it works. So the program is definitely reading the click. But when I tested remove() for a fail with... if(remove("mb1.txt")) { set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); allegro_message("ERROR: File deletion unsuccessful", allegro_error); } ...it confirmed that the remove was failing. Also, I just look every time to see if the file is there, and it remains there... just as stubborn as ever. The only time I got it to work is when I typed "remove("mb1.txt");" right in the main() function with no if loops or switch statements. This, however, is of course useless. I tried your suggestion of "./mb1.txt" and that didn't work either. I greatly appreciate the effort though. Oh, and if it helps. The function that calls trash_file(int x) and the function trash_file(int x) itself are in two different .c files. |
BAF
Member #2,981
December 2002
|
Are the files open? |
Seraph6
Member #7,517
July 2006
|
When they are deleted, no. I think I tried it with them open once (which doesn't sound right, but I tried it anyway), and I got an error. |
Kitty Cat
Member #2,815
October 2002
|
What does it say when you use this: -- |
Seraph6
Member #7,517
July 2006
|
Thanks, that was really handy. I wasn't aware of strerror(errno). However, as useful as that was, I'm not sure why that is the case. The file is not read only or hidden, and it shouldn't be open. |
Billybob
Member #3,136
January 2003
|
How is the file created? What Operating System?
|
Seraph6
Member #7,517
July 2006
|
The file was created with the games save_file(int x) function.
My operating system is WinXP and I am the owner of this PC, not to mention the sole user. |
juvinious
Member #5,145
October 2004
|
how are you running the app? Your CWD is probably not being set to the one containing the files. Open up the shell, go to the directory and run it and see if you have the same results. __________________________________________ |
Seraph6
Member #7,517
July 2006
|
I'm sorry, but that comment just went over my head. |
Johan Halmén
Member #1,550
September 2001
|
Both your code examples start like That's strange. Sure you haven't typed in the code in your post, but pasted? You should get compiler errors from that. OTOH x might be a macro that compiles fine but screws everything. Before remove(), try to examin the state of the file and write it on the screen. Is the file open, is it protected, whatever. I bet it is open. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
Jakub Wasilewski
Member #3,653
June 2003
|
I agree with Johan, "Permission denied" under Windows is usually only caused by the file still being open, either by the same or another process. Double-check that all handles to the file are closed. --------------------------- |
Audric
Member #907
January 2001
|
Indeed, in Windows "Permission denied" means "the OS won't let you" You can try Sysinternals' "Process Explorer" to check for open files: This program is very much like a task manager on steroids. |
James Stanley
Member #7,275
May 2006
|
I think the explanation is that you are in the wrong directory. Does Dev-C++ run your programs from its bin directory or from the directory you loaded the project from? EDIT: remove("C:/home/you/mb1.txt"); Or whatever Windows uses. |
Seraph6
Member #7,517
July 2006
|
[quote Johan]it should be like You're correct. However, when I changed that, it didn't fix the error. [quote Johan]Sure you haven't typed in the code in your post, but pasted?</quote> I copied and pasted my code right in. Except for the very first for loop near the top of this post. That was just an example. The actual code is like this. 1void update_po_menu()
2{
3 if(cursor->x < (menu->x + 300))
4 {
5 scroll_disabled = 1;
6 menu->state = 1;
7 for(n=0; n<4; n++) sframe[n]->alive = 1; //draw save frames
8
9 if(menu->x < 0)
10 {
11 menu->x += menu->speed;
12 if(menu->x > 0) menu->x = 0; //don't push the menu out too far
13 }
14 else //operations available once the pop-out menu is fully extended
15 {
16 //play/credits view switcher
17 if((cursor->x > 50) && (cursor->y > 20) && (cursor->x < 125) && (cursor->y < 70) &&
18 (mouse_b & 1) && (view_ct == button_delay))
19 {
20 credits = 0;
21 }
22 else if((cursor->x >= 125) && (cursor->y > 20) && (cursor->x < 200) && (cursor->y < 70) &&
23 (mouse_b & 1) && (view_ct == button_delay))
24 {
25 credits = 1;
26 }
27 else
28 {
29 view_ct++;
30
31 if(view_ct > button_delay) view_ct = button_delay;
32 }
33
34 //color pickers
35 if(cursor->y > 650)
36 {
37 //if cursor is over red color slider and left-mouse button is held
38 if((cursor->x >= 55) && (cursor->y > 656) && (cursor->x <= 210) && (cursor->y < 682) && (mouse_b & 1))
39 {
40 r = .68 * cursor->x + 100;
41 if(r < 140) r = 140;
42 else if(r > 240) r = 240;
43 }
44 //if cursor is over green color slider and left-mouse button is held
45 else if((cursor->x >= 55) && (cursor->y > 687) && (cursor->x <= 210) && (cursor->y < 713) && (mouse_b & 1))
46 {
47 g = .68 * cursor->x + 100;
48 if(g < 140) g = 140;
49 else if(g > 240) g = 240;
50 }
51 //if cursor is over red color slider and left-mouse button is held
52 else if((cursor->x >= 55) && (cursor->y > 718) && (cursor->x <= 210) && (cursor->y < 744) && (mouse_b & 1))
53 {
54 b = .68 * cursor->x + 100;
55 if(b < 140) b = 140;
56 else if(b > 240) b = 240;
57 }
58 }
59
60 //save frames
61 for(n=0; n<4; n++)
62 {
63 //if cursor is over save frame...
64 if((cursor->x > sframe[n]->x) && (cursor->y > sframe[n]->y) &&
65 (cursor->x < sframe[n]->x + 200) && (cursor->y < sframe[n]->y + 125))
66 {
67 if(save_check(n) == 0) //if no save file, make save option
68 {
69 sframe[n]->red = 150;
70 sframe[n]->green = 255;
71 sframe[n]->blue = 150;
72
73 sframe[n]->type = 1;
74
75 if(mouse_b & 1)
76 {
77 save_file(n);
78
79 sframe[n]->red = 250;
80 sframe[n]->green = 255;
81 sframe[n]->blue = 250;
82 }
83 }
84 else //if there is a save file, make save/load/delete options
85 {
86 sframe[n]->type = 2;
87
88 //if cursor is over save icon
89 if((cursor->x > sframe[n]->x) && (cursor->y > sframe[n]->y) &&
90 (cursor->x < sframe[n]->x + 65) && (cursor->y < sframe[n]->y + 125))
91 {
92 sframe[n]->red = 150;
93 sframe[n]->green = 255;
94 sframe[n]->blue = 150;
95
96 if(mouse_b & 1)
97 {
98 save_file(n);
99
100 sframe[n]->red = 250;
101 sframe[n]->green = 255;
102 sframe[n]->blue = 250;
103 }
104 }
105 //if cursor is over load icon
106 else if((cursor->x >= sframe[n]->x + 65) && (cursor->y > sframe[n]->y) &&
107 (cursor->x < sframe[n]->x + 120) && (cursor->y < sframe[n]->y + 125))
108 {
109 sframe[n]->red = 150;
110 sframe[n]->green = 150;
111 sframe[n]->blue = 255;
112 }
113 //if cursor is over delete icon
114 else if((cursor->x >= sframe[n]->x + 120) && (cursor->y > sframe[n]->y) &&
115 (cursor->x < sframe[n]->x + 200) && (cursor->y < sframe[n]->y + 125))
116 {
117 sframe[n]->red = 255;
118 sframe[n]->green = 150;
119 sframe[n]->blue = 150;
120
121 if(mouse_b & 1)
122 {
123 trash_file(n);
124
125 sframe[n]->red = 255;
126 sframe[n]->green = 250;
127 sframe[n]->blue = 250;
128 }
129 }
130 }
131 }
132 //otherwise get color back to normal
133 else if((sframe[n]->red < 255) || (sframe[n]->green < 255) || (sframe[n]->blue < 255))
134 {
135 sframe[n]->type = 0;
136
137 if(sframe[n]->red < 255) sframe[n]->red++;
138 if(sframe[n]->green < 255) sframe[n]->green++;
139 if(sframe[n]->blue < 255) sframe[n]->blue++;
140 }
141 }
142 }
143 }
144 else
145 {
146 scroll_disabled = head->state;
147 menu->state = 0;
148 for(n=0; n<4; n++) sframe[n]->alive = 0; //don't draw save frames
149
150 if(menu->x > -250)
151 {
152 menu->x -= menu->speed;
153 if(menu->x < -250) menu->x = -250; //don't push the menu in too far
154 for(n=0; n<4; n++) sframe[n]->alive = 1; //draw save frames
155 }
156 }
157}
I just figured it would be a lot of code to go through. [quote Johan]Before remove(), try to examin the state of the file and write it on the screen. Is the file open, is it protected, whatever. I bet it is open.</quote> Capital idea! However, file handling in C is new to me, and I wasn't really well versed with it in C++ to begin with. So I'm not sure how to do that, but I am currently googling it. [quote Jakub]"Permission denied" under Windows is usually only caused by the file still being open, either by the same or another process. Double-check that all handles to the file are closed.</quote> Yup, that sounds like the case. However, every function that handles a file starts with FILE* mb_save; and ends with fclose(mb_save); And it's only three functions, so it's not easy to miss one. [quote Audric]You can try Sysinternals' "Process Explorer" to check for open files</quote> Thanks, I will look into that. [quote James]I think the explanation is that you are in the wrong directory. Does Dev-C++ run your programs from its bin directory or from the directory you loaded the project from?</quote> I do believe it runs programs from the directory the project was loaded from. Also, the fact that it can be deleted in main() leads me to believe this as well. [quote James]You could try typing the full path to the file</quote> No good, the error remains the same. You don't think that the file being on a partitioned drive would affect it do you? I mean, this works in main(), so I assume that's not the problem. |
Richard Phipps
Member #1,632
November 2001
|
Are you sure that no other program has the file or directory open? |
Seraph6
Member #7,517
July 2006
|
I'm gonna throw this up in case anyone really wants to check my file handling.
The only other file handling that the program does is the main() function with the data file. Speaking of which, you don't think the *argv[] in main's parameters would be causing this do you? EDIT: I think I may have figured it out. Apparently the constant checking of if a file exists is interfering with completing other actions on the file. Which is odd because logically, the way the functions run, they shouldn't be interfering with each other. But I got rid of constantly checking on the file's existance, and it deleted fine. Now I just have to reprogram the same effect without constantly checking the file. |
Steve Terry
Member #1,989
March 2002
|
I hope you realize your save_check function does NOT close the file if it is found, notice you return from the function yet your flcose statement is at the bottom. If you close it before returning, it should fix your issue. Why would you check to see if a file exists anyway every time in a loop? Besides that use stat instead of fopen to check if a file exists, it will be much quicker and less resources will be used. Cookies for teh win!!!!!!! ___________________________________ |
Evert
Member #794
November 2000
|
What's wrong with using Allegro's exists() function? |
Seraph6
Member #7,517
July 2006
|
Steve said: I hope you realize your save_check function does NOT close the file if it is found Thanks, I notice that now. Very stupid mistake that slipped by me. Steve said: Why would you check to see if a file exists anyway every time in a loop? I think I did it so that... you know what? I don't remember why. Hmm... Steve said: Besides that use stat instead of fopen to check if a file exists, it will be much quicker and less resources will be used. Thanks, I assume that checks the status of the file (hence "stat"). I'm not sure I completely get how it works quite yet, but it's not too hard to understand. Evert said: What's wrong with using Allegro's exists() function? I'm not familiar with that function. Is there somewhere on allegro.cc I could read about it? A note to everyone who posted here, my credits for the game are short, so I decided I could fit all who contributed help in a little thanks section of the credits. |
BAF
Member #2,981
December 2002
|
int file_exists(const char *filename, int attrib, int *aret); And your switch way of figuring out which file to load is very ugly. You could just make the filename based on the id:
|
|