1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

code in c++

Discussion in 'C++' started by reta, Jun 15, 2010.

  1. reta

    reta New Member

    Joined:
    Jun 15, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hi to all

    I use the following code in c++ under linux, but I need to convert it to c under linux, any one can convert it from c++ to c.

    the details of the program is:
    It reads a text file and then asks the user to enter a string.
    It then displays how many times the string occurs and also prints the lines in which it occurs

    Code:
    #include<map>
    #include<iostream>
    #include<string>
    #include<fstream>
    #include<sstream>
    using std::istringstream;
    using std::ifstream;
    using std::string;
    using std::cout;
    using std::cin;
    using std::cerr;
    using std::map;
    using std::multimap;
    int main()
    {
        multimap<string,int> words;
        map<int,string> lines;
        string str;
        ifstream input("test.txt");
        if(input.fail())
        {
            cerr<<"\nThe file could not be opened.";
            return -1;
        }
        int i=1;
        while(getline(input,str))
        {
            istringstream in(str);
            string s;
            while(in>>s)
            {
                words.insert(make_pair(s,i));
            }
            lines.insert(make_pair(i,str));
            i++;
        }
        string search;
        cout<<"\nEnter a word to search: ";
        cin>>search;
        cout<<"\nThe number of matches = "<<words.count(search)<<'\n';
        multimap<string,int>::iterator it1=words.lower_bound(search);
        multimap<string,int>::iterator it2=words.upper_bound(search);
        while(it1!=it2)
        {
            int x=it1->second;
            map<int,string>::iterator iter=lines.find(x);
            cout<<'\n'<<x<<" ) "<<iter->second<<'\n';
            it1++;
            while(true)
            {
                if(it1!=it2 && it1->second==x)
                {
                    it1++;
     
                }
                else
                {
                    break;
                }
            }
        }
        return 0;
    }
    

    thanks in advance
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    You're pretty much going to have to rewrite it from scratch; this code uses a lot of C++ specific syntax all of which is invalid in C. However the C++ code is overcomplicated, you can do it in about quarter the code, and it will be a lot faster as well; just use fgets and strstr (which you could also do in C++, there's no need for this silly multimap stuff and reading the whole file into memory at the start).

    print greeting
    get string
    if fopen
    - while !eof
    - - get string from file
    - - test match with strstr
    - - - if it matches add 1 to a count and display the line
    - - (handle multiple matches if you need to)
    - end while
    end if
    display the count

    If you need to display the count THEN the matching lines, just loop over the file twice; this will be a lot easier than trying to store the matching lines in memory (and will scale to massive files, thus turning that horrendous demonstration of how NOT to write C++ code into a utility that might actually be useful).
     

Share This Page