Advertisement Jump to content
  • Advertisement

xyuri

Member
  • Content Count

    324
  • Joined

  • Last visited

Community Reputation

166 Neutral

About xyuri

  • Rank
    Member
  1. xyuri

    Can I handle a HUP signal?

    It certainly does. Thank you very much :)
  2. I have been programming C++ on linux for a little bit now, but have made an app which needs to be closed gracefully. Is there a way I can handle either a HUP or Ctrl-C commands? Any input would be greatly appreciated. Thanks :-)
  3. How am i programming in C ? I am using classes and compiling with a C++ compiler. Not trying to argue, just understand.
  4. The problem appears to have been that the file needed to be closed. I do have a destructor in the actual class that I am using though, but it did not help. I am under the understanding that a destructor is called when the class does out of scope? Why then was it not called. I was also free()'ng the object. Was the destructor not surposed to be reached under these circumstances?
  5. Thank you richardurich for your response. My code compiles fine, as runs without errors, only problem is that it does not replicate the file identical to the original file. The following is working code which DOES WORK AS I INTENT IT TO. ifstream ifs; ofstream ofs; ifs.open("testfile",ios::binary); ofs.open("testfile.duplicate",ios::binary); char* buffer; int chunksize = 16; // size of each chunk int read = 0; // bytes already read int size; // file size // just get size { ifs.seekg(0, ios::end); size = ifs.tellg(); ifs.seekg(0, ios::beg); } // pseudo: // while "size" > "read": // read "chunksize" to buffer else "size"-"read" // write to file // update "read" variable while (size > read) { if ((size-read) < chunksize) { // only remainder buffer = (char*)malloc(size-read); ifs.read(buffer,size-read); ofs.write(buffer,size-read); free(buffer); read += size-read; } else { // chunk sized chunk buffer = (char*)malloc(chunksize); ifs.read(buffer,chunksize); ofs.write(buffer,chunksize); read += chunksize; } } ifs.close(); ofs.close(); Just put it in your main and you will see it duplicates the file "testfile". class X { public: X() { _f.open("testfile.duplicate",ios::binary); } void write(char** c, int* i) { _f.write(*c,*i); } private: ofstream _f; }; ifstream ifs; ifs.open("testfile",ios::binary); char* buffer; int chunksize = 16; // size of each chunk int read = 0; // bytes already read int size; // file size // just get size { ifs.seekg(0, ios::end); size = ifs.tellg(); ifs.seekg(0, ios::beg); } // pseudo: // while "size" > "read": // read "chunksize" to buffer else "size"-"read" // write to file // update "read" variable X* x = new X(); while (size > read) { if ((size-read) < chunksize) { // only remainder buffer = (char*)malloc(size-read); ifs.read(buffer,size-read); int tmp = size-read; x->write(&buffer,&tmp); free(buffer); read += size-read; } else { // chunk sized chunk buffer = (char*)malloc(chunksize); ifs.read(buffer,chunksize); x->write(&buffer,&chunksize); read += chunksize; } } ifs.close(); Obviously the code is in a relevant place that it is run (just in main, and the X class is just above main). The second example creates a file which is smaller than the original. I dont know why using a class has this effect, it is beyond me. Does anybody know why?
  6. I have the need to read a file in chunks, then write it in another file one chunk at a time. So I started by simply writing code in a single function to see if it works, which it does. But since I have placed the code into classes it does not work as I intend. Basically, I have a buffer which gets populated with a "chunk" of the file. This buffer is a char pointer (char* buffer) which is then written to a file through a simple ofstream. The reading is NOT an issue, the following illustrates. By buffer and integer of how many bytes (chars) in this "chunk": char* c = (char*)malloc(16 * sizeof(char)); int i = 0; Here I get the chunks from a class I made, which works fine ... the problem is writing to the new file. This, for example, works 100% good. ofstream ofs; ofs.open("testfile.duplicate",ios::binary); while (gfc->more()) { gfc->getChunk(&c,&i); ofs.write(c,i); } But, I cannot use a class to do this: class X { public: X() { _f.open("testfile.duplicate",ios::binary); } void write(char** c, int* i) { _f.write(*c,*i); } private: ofstream _f; }; X* x = new X(); while (gfc->more()) { gfc->getChunk(&c,&i); x->write(&c,&i); } the first example works perfectly, but the second doesnt create the destination file properly. Any idea why having the X class (which was constructed purely for this demonstration) acts wierdly? Any input on this is greatly appreciated. Thank you :) Edit: Thank you to whoever deleted my double post ... you got to it before i could even delete it myself :D
  7. xyuri

    pointer to pointer

    Thank you both for your responses :) I have the feeling that this has happened to me in the past then forgotten about it :( Thanks again :)
  8. I am writing a function using borland c++ builder 6 to accept a pointer to a pointer to an image ;) (yes, pointer to pointer) but the syntax doesnt seem to work for me. this is my function header: void __fastcall TForm1::AdjustImage (TImage** ipp, int v, int h) now that compiles fine, but the errors come when I need to dereference (and derefence again) that ipp variable. A TImage has a "parent" properly, but "**ipp.parent" doesnt work, the "ipp->->parent" approach doesnt work either. How is this done?
  9. xyuri

    max value for DOUBLE ?

    Well, the reason I am thinking about this is because with my app I allow the user to input a DOUBLE type number, but if I enter anything larger than around 200 it seems to treat the number wrongly ... I am under the assumtion that this is caused by the DOUBLE value looping back to 0? I thought the actual max value was larger than ~100 to ~200 ??
  10. I have searched the web a bit but cant seem to find where in the standard set of libraries to find a declaration for the max value for the double primitive data type. I have also searched the library myself and cant find it ... it must be there right? Anyone know?
  11. holy s***! See what heppens when you dont code c++ for a few months! :) I feel so stupid for making so many stupid mistakes! Ok, time for some coffee and fresh air ;) Thank you all for your extremely informative informaion :)
  12. I doint know why, but this is copying the conventions that are used in the c++ book that I learn from, but it compiles with a errors that seemingly come from nothing. This is my header file (banking.h): //| //| banking.h //| //| Author.: Michael Dawson //| Date...: 02/05/2006 //| //| Purpose: //| #ifndef BANKING_H #define BANKING_H class Account { public: Account (double); void credit (double); bool debit (double); double getBalance(void); private: double balance; } class SavingsAccount : Account { double balance; SavingsAccount(double, double); double calculateInterest(void); } class ChequesAccount : Account { double balance; ChequesAccount(double, double); void credit (double); bool debit (double); } #endif And this is my code file (banking.cpp): #include "banking.h" // // Constructors. // Account::Account(double balance) { //this.balance = balance; } SavingsAccount::SavingsAccount(double balance, double interest) { Account(balance); this.interest = interest; } ChequesAccount::ChequesAccount(double balance, double fee) { Account(balance); this.fee = fee; } // // Debit and Credit. // void Account::credit (double amount) { this.balance += amount; } boolean Account::debit (double amount) { if (this.balance >= amount) { this.balance -= amount; return true; } else { return false; } } void ChequesAccount::credit (double amount) { base.credit(amount); balance -= fee; } boolean ChequesAccount::debit (double amount) { if (base.debit(amount)) balance -= fee; } // // Other. // double Account::getBalance(void) { return this.balance; } double SavingsAccount::calculateInterest(void) { return (this.interest/this.balance)*100; } Errors: Quote:banking.cpp:6: error: new types may not be defined in a return type banking.cpp:6: error: two or more data types in declaration of `Account' banking.cpp:6: error: two or more data types in declaration of `Account' banking.cpp:6: error: return type specification for constructor invalid banking.cpp: In constructor `SavingsAccount::SavingsAccount(double, double)': banking.cpp:9: error: no matching function for call to `Account::Account()' banking.h:14: note: candidates are: Account::Account(const Account&) banking.cpp:6: note: Account::Account(double) banking.cpp:10: error: declaration of 'Account balance' shadows a parameter banking.cpp:10: error: no matching function for call to `Account::Account()' banking.h:14: note: candidates are: Account::Account(const Account&) banking.cpp:6: note: Account::Account(double) banking.cpp:11: error: request for member `interest' in `this', which is of non-class type `SavingsAccount* const' banking.cpp: In constructor `ChequesAccount::ChequesAccount(double, double)': banking.cpp:13: error: no matching function for call to `Account::Account()' banking.h:14: note: candidates are: Account::Account(const Account&) banking.cpp:6: note: Account::Account(double) banking.cpp:14: error: declaration of 'Account balance' shadows a parameter banking.cpp:14: error: no matching function for call to `Account::Account()' banking.h:14: note: candidates are: Account::Account(const Account&) banking.cpp:6: note: Account::Account(double) banking.cpp:15: error: request for member `fee' in `this', which is of non-class type `ChequesAccount* const' banking.cpp: In member function `void Account::credit(double)': banking.cpp:22: error: request for member `balance' in `this', which is of non-class type `Account* const' banking.cpp: At global scope: banking.cpp:24: error: `boolean' does not name a type banking.cpp: In member function `void ChequesAccount::credit(double)': banking.cpp:34: error: `base' undeclared (first use this function) banking.cpp:34: error: (Each undeclared identifier is reported only once for each function it appears in.) banking.cpp:35: error: `fee' undeclared (first use this function) banking.cpp: At global scope: banking.cpp:37: error: `boolean' does not name a type banking.cpp: In member function `double Account::getBalance()': banking.cpp:48: error: request for member `balance' in `this', which is of non-class type `Account* const' banking.cpp: In member function `double SavingsAccount::calculateInterest()': banking.cpp:52: error: request for member `interest' in `this', which is of non-class type `SavingsAccount* const' banking.cpp:52: error: request for member `balance' in `this', which is of non-class type `SavingsAccount* const' That first error points to the first constructor in my code file, which looks fine to me ... but obviously isnt in some way? Its beyond me. I'll sort out the rest of the errors later, but if you could help me with them too then excellent :) I dont mean to be greedy though. I would really appreciate any help or information with this issue :) Thanks :)
  13. Quote:Original post by Fruny Quote:Original post by xyuri I need it to resize ;-) Only the first dimension can be dynamic. Consider using a 1-dimensional (row_size*col_size) array and manually computing the appropriate index as: index = column + row_size * row; Or you can use the boost::multi_array class, or a dynamically allocated array of dynamically allocated arrays (but that's more trouble than it's worth). std::vector is also a very good choice to take care of the array itself. Especially when resizing is involved. jyk -- he's using new, it can't be C. aaaah... only the first dimension... that explains some of the other error messages as well :-) yeah, might just go with a single dimension then. Thank you very much for your assistance :-)
  14. C++ ... and if it helps I'm writing it on BSD using G++.
  15. I need it to resize ;-)
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!