Yeah, it's being gay. I think it has something to do with the option that lets you choose if you want to start from where they left off or delete old contents of the file and start over...
Code:Look in new post
Yeah, it's being gay. I think it has something to do with the option that lets you choose if you want to start from where they left off or delete old contents of the file and start over...
Code:Look in new post
Last edited by bikr692002; 04-07-2006 at 02:14 PM.
I am only mentioning the 1 error, but this same type is duplicated elsewhere:
you are creating a_file in a local scope. It is closed and destroyed at the closing braceCode:{ std::ofstream a_file(File.c_str(), std::ios::ate); Loop=false; }//<----a_file destructed here.
That's what I though, but then how can I let the user select the option?
1 of 2 ways, 1st since you are already using the a_file name as a iistream object, use a different identifier for the ofstream, but define it in a higher(more outer?) scope, like where you define the istream object.
Or...
use a fstream object for your initial a_file which has the combined ability of input or output. Then just use that same object in your if/else brackets
Last edited by Darryl; 04-07-2006 at 01:37 PM.
>.< I'm confused, wouldn't that not make a diffrence since it gets terminated at the closing bracket???
it only gets terminated at the closing bracket because of scope rules. Stuff declared inside brackets usually only exist in between those brackets and go out-of-scope once you jump off the brackets. If you have for instance:And you try to compile it you'll getCode:// c.cpp #include <iostream> int main() { int a=10; if (a==10) { int b= 20; } std::cout << b << std::endl; return 0; }This is because the variable b was declared inside the if bracketing and as such b was local to the if, once you bail out of the if statement b goes out-of-scope and gets deleted.Code:c.cpp: In function 'int main()': c.cpp:11: error: 'b' was not declared in this scope
Hope this helps a bit. cheers.
Last edited by LinuxCoder; 04-07-2006 at 01:31 PM.
Thanks =D.
Anyways, I am making alot of functions in my header and then using those in the source so the source code doesn't get HUGE and confusing. Also, that way, the file selection thing (I know, I make no sense) is in the scope(s) (Of the header file)
Code:Look at bottom.
---And the header file---
Code:Don't use it anymore.
Last edited by bikr692002; 04-07-2006 at 05:17 PM.
This is not the best/correct way of using header files, you should write all the functions in fn.cpp for instance then create a fn.h header and put all the functions prototype there, something like:I am making alot of functions in my header and then using those in the source so the source code doesn't get HUGE and confusing.
then include fn.h in your main.cpp and in all files where you use any of the functions declared and written in fn.cpp. This will tell the compiler how the functions must be called, by telling it what the function returns, what arguments and argument types need to be passed in while telling the compiler that the actual implementation of the functions (the real code) is in some other place.Code://fn.h #ifndef _FN_H_ //preprocessor inclusion guards to prevent multiple inclusions of fn.h #define _FN_H_ //thus preventing multiple declarations of the functions. int GetFileSize(std::string File); void Delete(std::string File); int GetDiskSize(std::string Dir); void DisplayOptions(); // etc.... #endif
That's the correct way of using header files since header files are used to prevent multiple declarations of the same functions over and over again which would result in compiler errors.
Hope this helps and sorry if not. Cheers
Last edited by LinuxCoder; 04-07-2006 at 03:12 PM.
It's just more convient for me this way I guess.
Well, as long as you know the right way of doing things you're obviously free to do them wrong if you want to, i just felt the moral need to point out a better use for header files. The rest is now up to you
I must tell you though that i find lazyness/commodity a lame excuse for having bad coding practice which can introduce a whole new level of errors and such. Just my opinion, not mean to offend anyone.
Cheers
There is no "right way" or "wrong way" of coding, I did this so it's easier for me to visualize and since I was writing the same lines over and over again, I figured "Hey, I could write a function for that" so then I don't have to keep repeating identicle things...
Not trying to argue of course...
Alright alright, you guys win... Anyways it gives me a better feeling when I just finished a longass code, I feel like I accomplished more...
Better?Code:#include <fstream> #include <iostream> #include <windows.h> int main() { typedef BOOL (WINAPI *PGETDISKFREESPACEEX)(LPCSTR, PULARGE_INTEGER,PULARGE_INTEGER,PULARGE_INTEGER); PGETDISKFREESPACEEX pGetDiskFreeSpaceEx; pGetDiskFreeSpaceEx=(PGETDISKFREESPACEEX)GetProcAddress(GetModuleHandle ("kernel32.dll"), "GetFreeDiskSpaceExA"); long end,begin; __int64 i64FreeBytes; bool Loop,Restart,FileExist; Loop=true; std::string Dir,File,Option,OptToRestart; int Loops,SizeOfDisk,SizeOfFile,OrigSizeOfFile; std::cout<<"Welcome to C.J.'s Security 'suite'!\n"; std::cout<<"Please enter what you want to do.\n"<<std::endl; std::cout<<"To select Secure Disk Wipe, input SDW.\n"; std::cout<<"To select Secure Disk Cleanse, input SDC.\n"; std::cout<<"To select Secure File Deletion, input SFD.\n"; std::cout<<"To select ---this option is not yet available---.\n"; std::cout<<"To select ---this option is not yet available---."<<std::endl; std::cout<<"Option:"; getline(std::cin,Option,'\n'); if(Option=="SDW") { std::cout<<"Sorry,This option is not yet available"<<std::endl; } if(Option=="SDC") { std::cout<<"Please enter Disk(Ex. C:\\):"; getline(std::cin,Dir,'\n'); if(Dir.length()!=3) { while(Loop==true) { Dir=""; std::cout<<"Please enter Disk:"; getline(std::cin,Dir,'\n'); if(Dir.length()==3) { Loop=false; } } Loop=true; } Dir=File; File=Dir+"SecureDiskCleanse.txt"; FileExist=true;/*NEED TO WRITE THIS FUNCTION...*/ if(FileExist==true) { std::cout<<"Do you want to start where you left off?(Y or N):"; getline(std::cin,OptToRestart,'\n'); if(OptToRestart=="Y") { Restart=true; } if(OptToRestart=="y") { Restart=true; } else { Restart=false; } if (Restart=true) { std::ofstream a_file(File.c_str(), std::ios::ate); while(Loop==true) { GetDiskFreeSpaceExA(Dir.c_str(), NULL, NULL, (PULARGE_INTEGER)&i64FreeBytes); SizeOfDisk=(i64FreeBytes/1024); if(SizeOfDisk>120) { for(Loops=0;Loops<25;Loops++) { a_file<<"SecureDiskCleanse.SecureDiskCleanse."; a_file<<"SecureDiskCleanse.SecureDiskCleanse."; a_file<<"SecureDiskCleanse.SecureDiskCleanse."; a_file<<"SecureDiskCleanse.SecureDiskCleanse."; } if(SizeOfDisk>18) { GetDiskFreeSpaceExA(Dir.c_str(), NULL, NULL, (PULARGE_INTEGER)&i64FreeBytes); SizeOfDisk=(i64FreeBytes/1024); } } if(SizeOfDisk<=1) { Loop=false; std::cout<<"No space left on "<<Dir<<"\n"<<std::endl; } } a_file.close(); } if(Restart==false) { std::ofstream a_file(File.c_str(), std::ios::trunc); while(Loop==true) { GetDiskFreeSpaceExA(Dir.c_str(), NULL, NULL, (PULARGE_INTEGER)&i64FreeBytes); SizeOfDisk=(i64FreeBytes/1024);; if(SizeOfDisk>120) { for(Loops=0;Loops<25;Loops++) { a_file<<"SecureDiskCleanse.SecureDiskCleanse."; a_file<<"SecureDiskCleanse.SecureDiskCleanse."; a_file<<"SecureDiskCleanse.SecureDiskCleanse."; a_file<<"SecureDiskCleanse.SecureDiskCleanse."; } if(SizeOfDisk>18) { a_file<<"SecureDiskCleanse."; GetDiskFreeSpaceExA(Dir.c_str(), NULL, NULL, (PULARGE_INTEGER)&i64FreeBytes); SizeOfDisk=(i64FreeBytes/1024); } } if(SizeOfDisk<=1) { Loop=false; std::cout<<"No space left on "<<Dir<<"\n"<<std::endl; } } a_file.close(); } } else { std::ofstream a_file(File.c_str()); while(Loop==true) { GetDiskFreeSpaceExA(Dir.c_str(), NULL, NULL, (PULARGE_INTEGER)&i64FreeBytes); SizeOfDisk=(i64FreeBytes/1024); if(SizeOfDisk>120) { for(Loops=0;Loops<25;Loops++) { a_file<<"SecureDiskCleanse.SecureDiskCleanse."; a_file<<"SecureDiskCleanse.SecureDiskCleanse."; a_file<<"SecureDiskCleanse.SecureDiskCleanse."; a_file<<"SecureDiskCleanse.SecureDiskCleanse."; } if(SizeOfDisk>18) { a_file<<"SecureDiskCleanse."; GetDiskFreeSpaceExA(Dir.c_str(), NULL, NULL, (PULARGE_INTEGER)&i64FreeBytes); SizeOfDisk=(i64FreeBytes/1024); } } if(SizeOfDisk<=1) { Loop=false; std::cout<<"No space left on "<<Dir<<"\n"<<std::endl; } a_file.close(); } } File="Del "+File; system(File.c_str()); std::cout<<"Disk Cleansed."<<std::endl; std::cin.get(); } if(Option=="SFD") { std::cout<<"Please enter file path and name.:"; getline(std::cin,File,'\n'); std::ifstream OrigSize_File(File.c_str()); begin=OrigSize_File.tellg(); OrigSize_File.seekg(0,std::ios::end); end=OrigSize_File.tellg(); OrigSize_File.close(); OrigSizeOfFile=(end-begin); std::ofstream a_file(File.c_str(), std::ios::trunc); while(Loop==true) { std::ifstream Size_File(File.c_str()); begin=Size_File.tellg(); Size_File.seekg(0,std::ios::end); end=Size_File.tellg(); Size_File.close(); SizeOfFile=(end-begin); a_file<<"Secure File Deletion."; if(SizeOfFile>(OrigSizeOfFile+10)) { Loop=false; std::cout<<"Finished overwrite."<<std::endl; } } File="Del "+File; system(File.c_str()); std::cout<<"File erased."<<std::endl; std::cin.get(); } else { std::cin.get(); } return 0; }
Last edited by bikr692002; 04-07-2006 at 05:26 PM.
>> Better?
Do you mean was it better to put all the code back into a single function? Then, no, that's not better. Separating it into functions was a good thing. Putting those functions into a separate file was a good thing. Putting them into a header into a source file was an unconventional thing, but in this case not that big of a deal. If you really wanted to make it better than it was with the previous header, you would have put prototypes into the header and the function definitions into a second source file as was suggested.
I saved the old source and header also, so I'll do that now... I just wanted a decently sized program...
Man, it seems i haven't explained myself correctly or you haven't understood what i really meant. Anyway i want to apologize if my explanations confused you instead of enlightening you and if they ended up throwing extra, unneeded work on your back. That was not my intention at any moment, i thought i was helping you out.
Cheers