convert from c++ to c

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#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
You'' need to implement your own map and multimap (or get them from a 3rd party library) as C doesn't provide them. You can replace the C++ I/O stream library with the ANSI C equivalent.
Is it important to read in the text file before asking for the string to search?

It seems to me more efficient to get your search word first and then read in the file line by line checking for it.

And I probably wouldn't 'convert' that program, I'd probably just rewrite from scratch using the same specification.
Alternatively, consider wrapping it in a function and using extern "C". Then you could call the function from C.
Topic archived. No new replies allowed.