Thread: Removing comments from textual files

  1. #16
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I think you lost me now...
    This would be a real bullet proof system.
    I don't know if I'm capable of doing that, maybe pythons re would be smarter choice.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #17
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Well, you can choose to handle as much or as little as you like. You prolly won't see many trigraphs or quad-chars in non-IOCCC code anyway
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #18
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Fo C++ Comments I got this little guy working

    Code:
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include <string>
    #include <iterator>
    
    
    
    using std::ofstream;    using std::ifstream;
    using std::string;
    using std::cout;    using std::cin;     using std::endl;
    
    
    
    int main()
    {
        string c_comm = "/*";
        string comm = "//";
        ifstream infile("Daniel.txt");
        ofstream outfile("Out.txt");
    
        string line, no_comm;
        string::iterator found;
    
        while(getline(infile, line))
        {
            //if comm not found, 'found' points to endl
            found = std::search(line.begin(), line.end(), comm.begin(), comm.end());
            no_comm.clear();
            no_comm.append(line.begin(), found);
            outfile << no_comm << endl;
        }
    
        infile.close();
        outfile.close();
    
    	return 0;
    }
    input
    Code:
    Daniel.txt
    {
        ifstream infile("Test.txt");
        ofstream outfile("Test2.txt");
        string str, search_start="/*", search_end = "*/";
        string :: size_type siz;
        bool comment = false;//flag that indicates if line is commented
        //read line
        while (getline(infile, str))
        {
            //write line to file if there is no markers /*, */ or line is not commented
            if (str.find("/*") == string :: npos && str.find("*/") == string :: npos && !comment)
            {
                outfile << str << endl;
            }
            else //line is commented
            {
                comment = true;
                siz = str.find("*/");//try to find end of comment
                if (siz != string :: npos)
                {
                    //close comment part and continu, read next line
                    comment = false;
                    continue;
                }
                else
                {
                    //read lines until end of comment marek is found 
                    while (siz != string :: npos)
                    {
                        getline(infile, str);
                        siz = str.find("*/");
                        comment = false;
                    }
                }
            }
        }
        return 0;
    output
    Code:
    Out.txt
    {
        ifstream infile("Test.txt");
        ofstream outfile("Test2.txt");
        string str, search_start="/*", search_end = "*/";
        string :: size_type siz;
        bool comment = false;
        
        while (getline(infile, str))
        {
            
            if (str.find("/*") == string :: npos && str.find("*/") == string :: npos && !comment)
            {
                outfile << str << endl;
            }
            else 
            {
                comment = true;
                siz = str.find("*/");
                if (siz != string :: npos)
                {
                    
                    comment = false;
                    continue;
                }
                else
                {
                    
                    while (siz != string :: npos)
                    {
                        getline(infile, str);
                        siz = str.find("*/");
                        comment = false;
                    }
                }
            }
        }
        return 0;
    Last edited by indigo0086; 08-09-2006 at 08:17 AM.

  4. #19
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    ok I'm trying to do the c style comments, but need to test for end of line on the iterators, so how do I do that from a string. is the null character the end of line?

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This might be a good time to learn LEX. This simple lexical analyzer generator is fully capable of performing the task you want in the way I described, and it's a pretty good way of illustrating the problem.

    After that, you might want to try it again using Boost.Spirit, a parser generator library with truly amazing capabilities.

    Or you could try to hand-implement the same thing, based on the general understanding you got when you did the thing in LEX.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM