Refresher..

This is a discussion on Refresher.. within the C++ Programming forums, part of the General Programming Boards category; I haven't coded for a couple of years now and when I did, I wasn't the greatest but I got ...

  1. #1
    jed
    jed is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    Refresher..

    I haven't coded for a couple of years now and when I did, I wasn't the greatest but I got by. I have a couple of problems that I have to figure out. First being, how would you get a program to print to a text file? I've never had to do it before so I'm a little lost. The other thing is where can I go to get a better understanding of lexing and parsing? The objective is that i'm trying to code a simple lexing and parsing program. any help would be great, thanks.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    umm, well here is an example it might help ya
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
       string somevar = "somevartext";
       ofstream out ("textfile.txt");
       out << "Some text";
       out << somevar;
       out.close();
       ifstream in ("textfile.txt");
       if ( in.is_open() ) {
          getline( in, somevar );
          cout << 'File Text: " << somevar << endl;
          in.close();
          return 0;
    }
       else {
          return 1;
    }
    }
    lol, I know that is most likely useless but enjoy

    EDIT: I edited the code alittle
    Last edited by Raigne; 08-26-2006 at 01:48 PM.

  3. #3
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Quote Originally Posted by jed
    First being, how would you get a program to print to a text file?
    It's pretty much the same as printing to the screen except you have to make the stream yourself instead of use cout.
    Code:
    #include <fstream>
    #include <iostream>
    
    int main()
    {
      // make your own stream to myFile.txt
      std::ofstream os("myFile.txt");
    
      // make sure that it's open before printing to it
      if (os)
      {
        // use it just like cout
        os << "Let's print to a file!\n";
      }
      else
      {
        std::cerr << "Something bad happened\n";
      }
    
      // the stream closes automatically
    
      return 0;
    }
    I don't know anything about lexing and parsing, sorry.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    74
    About the lexing/parsing I had to write a full compiler in C before and we used an external programming language/program that would generate the necessary C code for us, which is much easier than writing C code by hand. Basically you use sort of a script language to define rules and pattern matching rules and it generates C code for you to include in your project. Depending on the complexity of your parsing/lexing problem you might want to check out:

    Lex http://en.wikipedia.org/wiki/Lex_programming_tool
    &
    Yacc http://en.wikipedia.org/wiki/Yacc

    There are also versions for C++ and Windows environments.

  5. #5
    jed
    jed is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    Lex/parser generator for C++

    Where could I find a generator for C++ environment? Are there any sharewares out there? thanks a bunch.

  6. #6
    jed
    jed is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    Would anyone have some simple lex and pars codes that I can look at and get an idea? thanks

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,579
    Boost.Spirit and Boost.Wave. The first is a parser framework and the second is built on top of Spirit to implement a lexer.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Applied philosophy refresher
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 04-28-2009, 10:55 PM
  2. Need a refresher, the difference between static and dynamic.
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 06-27-2008, 03:28 AM
  3. Refresher
    By Dark_Phoenix in forum Game Programming
    Replies: 6
    Last Post: 10-11-2006, 09:13 AM
  4. Replies: 3
    Last Post: 12-16-2002, 09:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21