• Lucid Dreaming - Dream Views




    Page 1 of 5 1 2 3 ... LastLast
    Results 1 to 25 of 102
    Like Tree3Likes

    Thread: Ask me about C / C++

    1. #1
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935

      Ask me about C / C++

      I know there are a lot of people trying to learn C++ on this forum. I'm a professional C++ programmer, and it's an enormously complex language so ask me anything. There are lots of things you need to know to write good C++ code: iterators, inheritance, OOP, procedural programming, static and dynamic functions, macros, scope, consts, virtual methods, encapsulation, pointers, null objects, threading, mutexs, cross platform, callbacks.
      Mancon123 likes this.

    2. #2
      A Natural The Invisible Man's Avatar
      Join Date
      Nov 2008
      Gender
      Posts
      365
      Likes
      8
      I have a console application using a case block to open some files. I just need the syntax for an 'open file' command.


      Can you see me now?

    3. #3
      Not Baņņed Yet Dairyman's Avatar
      Join Date
      Feb 2009
      Location
      Australia
      Posts
      201
      Likes
      2
      DJ Entries
      2
      Can I ask, is this only with C++ or other C languages too?

      EDIT-
      Also, do compiled C++ programs have to run on an IDENTICAL system? (Same operating system, same CPU etc) I read this in an e-book once and switched back to Visual Basic.
      Last edited by Dairyman; 05-15-2009 at 11:00 AM.

    4. #4
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by The Invisible Man View Post
      I have a console application using a case block to open some files. I just need the syntax for an 'open file' command.
      Ugh, files are a pain. There are good libraries out there (I use wxWidgets,) but pure C is hard.
      Code:
      FILE* file = fopen("yourfile", "w");  //w for write, r for read, a for append
      if(file!=NULL){
        /*
          fwrite and fread are what you need
          fwrite(buffer, size of element, number of elements, file pointer)
        */
        size_t check =  fwrite("Stuff to write", sizeof(char), 14, file);
        if (check != 14) printf("Writing failed");
      }
      fclose(file)
      Quote Originally Posted by Dairyman View Post
      Can I ask, is this only with C++ or other C languages too?
      I only know C and C++, I don't know C# or objective C.

      Also, do compiled C++ programs have to run on an IDENTICAL system? (Same operating system, same CPU etc) I read this in an e-book once and switched back to Visual Basic.
      Yes, that's no different than any other programming language that does native compiling. A program written in C++ or C compiled on OSX will only run on OSX, Windows programs will only run on Windows. CPU doesn't matter... mostly (AMD=AMD64=Intel=Intel64!=PPC)

      It's very easy to fix though, a few preprocessor commands will allow you to compile on any system.

    5. #5
      A Natural The Invisible Man's Avatar
      Join Date
      Nov 2008
      Gender
      Posts
      365
      Likes
      8
      Crap, I meant C++. Sorry, but... um.... yeeeeeeeeeeeeaaaaaaaaaaah. Could I get that the Open File command on C++? Command for the Standard Library?
      Last edited by The Invisible Man; 05-17-2009 at 10:24 PM. Reason: Clarification. Sorry dude.


      Can you see me now?

    6. #6
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Yes, but it's in a different library in C++, I think it's in <iostream>

    7. #7
      A Natural The Invisible Man's Avatar
      Join Date
      Nov 2008
      Gender
      Posts
      365
      Likes
      8
      I have <iostream> and <stdlib.h>. What's the command?


      Can you see me now?

    8. #8
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      They are the same as C. C++ is a [nearly] complete superset of C.

    9. #9
      Motion Personified Alpha Achievements:
      Referrer Bronze 1000 Hall Points Veteran First Class
      mrdeano's Avatar
      Join Date
      Apr 2009
      Gender
      Location
      United Kingdom
      Posts
      1,053
      Likes
      58
      DJ Entries
      1
      What language did you learn before C & C++?

    10. #10
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Basic and assembly.

    11. #11
      A Natural The Invisible Man's Avatar
      Join Date
      Nov 2008
      Gender
      Posts
      365
      Likes
      8
      So you're saying
      Code:
      #include <iostream>
      #include <stdlib.h>
      int main()
      {
      FILE* file = fopen("yourfile", "w");  //w for write, r for read, a for append
      if(file!=NULL){
        /*
          fwrite and fread are what you need
          fwrite(buffer, size of element, number of elements, file pointer)
        */
        size_t check =  fwrite("Stuff to write", sizeof(char), 14, file);
        if (check != 14) printf("Writing failed");
          fclose(file)
                        }
      }
      This is what I type into my IDE to open the file/app/.exe?

      Sorry to bug you like this, but I'm not sure if you were answering my question.
      Last edited by The Invisible Man; 05-20-2009 at 05:54 AM. Reason: tabbing


      Can you see me now?

    12. #12
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Yes, that's how you open a file, write the words "Stuff to write" to it, and then close it again.

    13. #13
      Member Achievements:
      1 year registered Veteran First Class 5000 Hall Points

      Join Date
      Sep 2004
      Gender
      Location
      Seattle, WA
      Posts
      2,503
      Likes
      217
      You can do file i/o the fstream way too, which is usually cleaner.

    14. #14
      Kargaroc peacock486's Avatar
      Join Date
      Feb 2009
      LD Count
      Never counted
      Gender
      Location
      In a building in the snow
      Posts
      238
      Likes
      9
      DJ Entries
      16
      let's just say this. it's "has an ass rocket, kills self, and goes to hell" (AKA Infinity) hard.

      68000 ASM is probably easier.

    15. #15
      Veteran of the DV Wars Man of Steel's Avatar
      Join Date
      Mar 2007
      LD Count
      ~35
      Gender
      Location
      Houston, TX
      Posts
      4,553
      Likes
      94
      An idea I just had regarding ebooks and the library system led me here...

      Say a library wanted to set up a proprietary program and ebook format to offer ebooks from their website that could be downloaded, but would 'expire' in a set period—say 30 days. This way they could offer the same selection for download as they did at the physical location, but on the same principle. Would C or C++ be the preferred method of designing said program, or would another method be better? At the simplest level, what would be involved in something like this?

      Just a thought I had, while I was wishing I could just download any book I wanted to read—legally.

    16. #16
      I wuv LD :P pepolshet's Avatar
      Join Date
      Apr 2009
      Gender
      Location
      Dreamland
      Posts
      51
      Likes
      5
      *cough* warez-bb *cough*

    17. #17
      Banned
      Join Date
      May 2007
      LD Count
      Loads
      Gender
      Location
      Digital Forest.
      Posts
      6,864
      Likes
      386
      Quote Originally Posted by The Invisible Man View Post
      Crap, I meant C++. Sorry, but... um.... yeeeeeeeeeeeeaaaaaaaaaaah. Could I get that the Open File command on C++? Command for the Standard Library?
      Code:
      #include <iostream>
      #include <fstream>
      #include <string>
      
      using namespace std;
      
      int main(){
          fstream FILE("foo.txt",ios_base::trunc); //First, we open a file named "foo.txt", overwriting any existing data. (signified by ios_base::trunc for truncate ;))
          
          if(!FILE.is_open();){ //If the file opens, then continue
              FILE<<"Stuff to write\n"; //Write to the File stream using the insertion operators <<
              string In;
              FILE>>In; //Read from the stream using the extraction operators >>
          }
          return EXIT_SUCCESS; //Because macros are just way cooler than constants
      }
      Last edited by A Roxxor; 05-23-2009 at 01:46 AM.

    18. #18
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      Quote Originally Posted by Man of Steel View Post
      An idea I just had regarding ebooks and the library system led me here...

      Say a library wanted to set up a proprietary program and ebook format to offer ebooks from their website that could be downloaded, but would 'expire' in a set period—say 30 days. This way they could offer the same selection for download as they did at the physical location, but on the same principle. Would C or C++ be the preferred method of designing said program, or would another method be better? At the simplest level, what would be involved in something like this?

      Just a thought I had, while I was wishing I could just download any book I wanted to read—legally.
      Better reduce the "loan time" on those
      anymore than 48 hours and you risk having your "restriction" cracked

      Something like an expiring book just wouldn't work....
      I know you know better than this,
      but anyhow

      Step though the program with a "legitimate" book
      Step though the program with an "expired" book
      find the fork
      redirect the expired fork down the legitimate fork

      In all honesty, kids stuff

      As a book publisher, you have just spent god-knows how much money buying this DRM mechanism from someone, and it's cracked in hours

      About the only way to realistically overcome this, is to require authorisation over the internet each time you open an e-book
      which would be crap, and only annoy people

      The "loaning" of things is a mechanism devised in the physical world of scarce products that cost money to reproduce
      It doesn't translate (sanely) over into the digital world
      (\_ _/)
      (='.'=)
      (")_(")

    19. #19
      Veteran of the DV Wars Man of Steel's Avatar
      Join Date
      Mar 2007
      LD Count
      ~35
      Gender
      Location
      Houston, TX
      Posts
      4,553
      Likes
      94
      Quote Originally Posted by Ynot View Post
      Better reduce the "loan time" on those
      anymore than 48 hours and you risk having your "restriction" cracked

      Something like an expiring book just wouldn't work....
      I know you know better than this,
      but anyhow

      Step though the program with a "legitimate" book
      Step though the program with an "expired" book
      find the fork
      redirect the expired fork down the legitimate fork

      In all honesty, kids stuff

      As a book publisher, you have just spent god-knows how much money buying this DRM mechanism from someone, and it's cracked in hours

      About the only way to realistically overcome this, is to require authorisation over the internet each time you open an e-book
      which would be crap, and only annoy people

      The "loaning" of things is a mechanism devised in the physical world of scarce products that cost money to reproduce
      It doesn't translate (sanely) over into the digital world
      Aye, I agree/know that, realistically speaking, it would never work. Just wishful thinking, really, and thought it posed an interesting hypothetical question for this thread. I guess the more practical way to do something of that nature would be via the internet, using Flash to prevent easy copy & pasting. Even that would be less than ideal, of course.

      *sigh*

    20. #20
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Yes, C++ is the most powerful language that there is, you can use it for something like that. However, I wouldn't use a timestamp, seeing as people can change their system time. I remember Apple having a problem with people renting movies on iTunes and then setting back their clocks.

    21. #21
      Motion Personified Alpha Achievements:
      Referrer Bronze 1000 Hall Points Veteran First Class
      mrdeano's Avatar
      Join Date
      Apr 2009
      Gender
      Location
      United Kingdom
      Posts
      1,053
      Likes
      58
      DJ Entries
      1
      Hey, I am trying to cover the basics of C++.
      I think I have some of them down, I just need to double check if I'm right.

      Code:
      #include "stdafx.h"
      #include <iostream>
      
      using namespace std;
      
      int main()
      {
          cout << "Here is some text";
          cout << endl;
      
          cout << "What is 15 + 17? the answer is:";
          cout << 15 + 17;
      
          cin.get();
          return 0;
      }
      Above is the first code I have written for C++, I found a little beginners tutorial for it. It is important to understand the code, so here it goes.

      Code:
      #include "stdafx.h"
      #include <iostream>
      They tell the compiler to include these in the .exe
      They are both separate files containing code.
      stdafx.h is a header code and isostream contains different packages that do several things.

      Code:
      using namespace std;
      Tells the computer that it wants to use the package 'std' from ;isostream

      Code:
      int main()
      Is the main function which every piece of code should have.

      Code:
      {
      Opens the code.

      Code:
      cout << "Here is some text";
      'cout <<' gives the program a output, or something to print. In this case its a line of text.
      Cout is a keyword taken from the package 'std' which is in 'iostream;

      Code:
      cout << endl;
      'endl' ends the line

      Code:
      cout << 15 + 17;
      This tells the computer to print the answer of this calculation.

      Code:
      cin.get();
      This makes the program wait for an input by the user

      Code:
      return 0;
      Once it has the input, it tells the computer to revert back to what it was doing before hand

      Code:
      }
      closes code




      I was wondering if you could check if I have it all right.
      If there's anything I am missing as a beginner.. I would appreciate it if you point it out


      thanks

    22. #22
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by A Roxxor View Post
      Code:
      using namespace std;


      Quote Originally Posted by mrdeano View Post
      Code:
      #include "stdafx.h"
      #include <iostream>
      They tell the compiler to include these in the .exe
      They are both separate files containing code.
      stdafx.h is a header code and isostream contains different packages that do several things.
      Yes.

      Code:
      using namespace std;
      Tells the computer that it wants to use the package 'std' from ;isostream
      ... kind of... That tells it to use the std namespace throughout the program. It is considered better programming technique to only have a namespace over small scopes. It actually allows you to transcend scope, this is very useful in large programs so that you don't have duplicate variables and have to worry about concurrency.

      Code:
      int main()
      Is the main function which every piece of code should have.
      Yes, it's the entry point of the operating system, there is more to it, but you don't need to know that unless you are doing assembly optimizations.

      Code:
      cout << "Here is some text";
      'cout <<' gives the program a output, or something to print. In this case its a line of text.
      Cout is a keyword taken from the package 'std' which is in 'iostream;

      Code:
      cout << endl;
      'endl' ends the line
      Yes, cout is a stream that goes to the terminal, the real thing that you need to take away from that line is the << operator. There are lots of functions that use it, you can create classes and use STL libraries to send strings through pipes, files, even the internet.

      endl is a std const for cross compatibility, most programers use \n instead though, even though we shouldn't

      Code:
      cin.get();
      This makes the program wait for an input by the user
      Yes, more specifically keyboard input, inputs from other devices won't trigger this method.

      Code:
      return 0;
      Once it has the input, it tells the computer to revert back to what it was doing before hand
      Kind of, it tells the operating system to close the program and return an error code of 0. Returning things other than zero from the main loop tells the operating system that something went wrong, and depending on the code and the OS, it may attempt some type of recovery. From -127 to -1, the system may try a recovery, from 1 to 127, it is a user-defined error and will not produce any type of recovery on the OS level, it may be read by a calling or forking program though.

    23. #23
      Member
      Join Date
      May 2009
      Posts
      139
      Likes
      1
      I have a package called "atomic parsley". Instead of a make file, it has a shell script with commands to compile each unit without linking, then goes through a second time and links everything. Each source file seems to have all the includes it needs, as-is. But on the compile w/o linking stage, it's using g++ with command line options which I don't know the importance of, which fail with a syntax error on all the c compilers I've got, even the version of gcc I have.

      Are there any quick guides to creating a proper make file for projects lacking one?

    24. #24
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Ouch, creating makefiles for large projects suck

      What options are being set that you don't know the significance of? Most likely the problem is that the program was written in Visual C++ and not GNU C++. M$ doesn't adhere to C++ standards, so their software won't work in other compilers, that's why so few people write in Visual C++ outside of M$ itself.

      The basic makeup of a makefile is setting the flags to variables, the creating a command for each file. Here's a good tutorial.

      What options were being set that you didnt' understand?

      The most common ones are:
      -c : compile, but don't link
      -O1, O2, O3, Os : optimization levels
      -strip : strip unnecessary symbols and code
      -o : send to this output file
      -l : link a library
      -I : include paths
      -g : include debugging information it the executable
      -Wall : Warnings All - warns you about everything

    25. #25
      Member
      Join Date
      May 2009
      Posts
      139
      Likes
      1
      Yes, I've been figuring that this will suck. But I really need some kind of code for dumping itunes m4a tags to a plain text file Besides the gcc that came with the linux I'm using, I've got borland c++ and symantec c++ for windows.
      I got them back when I was taking some classes in C programming.

      There is a command line in the shell script similar to this for each source file during the compiling stage-

      g++ -g -O2 -Wall -MT obj_files/AP_commons.o -MD -MP -MF
      "./obj_files/AP_commons.Tpo" -c -o obj_files/AP_commons.o
      AP_commons.cpp

      Each of those compile commands gets the following error message, and then doesn't produce any output file-

      g++: cannot specify -o with -c or -S and multiple compilations

      When the link command is run later, there's nothing to link.

    Page 1 of 5 1 2 3 ... LastLast

    Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •