Help converting numbers to word format in a txt file

Hi, I am trying to read into a file that has something like

I have 5 apples and 9 bananas.
Sam has 8 apples and 6 bananas.

and I need to replace the numbers with words. For example I need to change the "5" to five and so on. The problem is that im not sure how to access and then replace just the numbers. Any help will be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  int main()
{
	
	ifstream in_stream;
	in_stream.open("input.dat");
		if(in_stream.fail())
		{
			cout<< "Input file opening failed.\n";
			exit(1);
		}

	string line[30]; // creates SIZE empty strings
	int i=0;
	while(!in_stream.eof()) {
	  getline(in_stream,line[i]); // read the next line into the next string
	  ++i;
	}
	int numLines = i;

	for (i=0; i < numLines; ++i) {
	  cout << i << ". " << line[i]<<endl; // no need to test for empty lines any more
	}
Check this code:
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
#include <iostream>
#include <fstream>
#include <string>

const std::string filename = "Testing.TXT";


std::string number_to_text(int n)
{
    return "TEMP";
}


int main(){
    std::ifstream inp(filename);
    if(!inp) {
        std::cerr << "Error opening file " << filename;
        exit(-1);
    }
    std::string line;
    const std::string digits = "0123456789";
    do {
        std::getline(inp, line);
        if(inp.fail()) {
            std::cerr << "unknown error reading file";
            exit(-2);
        }
        std::string::size_type pos = 0, pos2 = 0;
        pos = line.find_first_of(digits);           //Search for
        pos2 = line.find_first_not_of(digits, pos); //group of digits
        while(pos != std::string::npos) {
            std::cout << line.substr(0, pos); //Output line before digits
            int number = stoi(line.substr(pos, pos2 - pos)); //Convert digits to a number
            std::cout << number_to_text(number);  //Output text representation of the number
            line.erase(0, pos2);  //Remove processed part
            pos = line.find_first_of(digits);           //Search for next
            pos2 = line.find_first_not_of(digits, pos); //group of digits
        }
        std::cout << line << '\n'; //output the rest of line
    } while (!inp.eof()); //Works perfectly with getline.
}
I have TEMP apples and TEMP bananas.
Sam has TEMP apples and TEMP bananas.
You only need to implement number_to_text function
Actually, you may try implementing an image converting tool within your applciation, which enables to converting and processing txt file to stream that is in the form of number or data or to byte arrary.

http://www.rasteredge.com/how-to/vb-net-imaging/convert-image-to-byte/

http://www.rasteredge.com/how-to/vb-net-imaging/convert-image-to-stream/
Topic archived. No new replies allowed.