Change words in string

Hello all, I need to change all words in text file, which contains letters o, y and a . I need to change these words to their row number . But the problem is that, some words it changes, some change from middle of word, some don't change at all , but row number also shows incorrectly ( like 1324516136 instead of 1 and so on ). Part of my 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <string>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <iostream>
using namespace std;

const string CDFV = "Input.txt"; //...........input file
const string CSKYR = " .,!?:;()"; //............ Seperators
void EditLine(string dfv,int &nr);
bool SearchSymbols(string row, char ch, char ch1, char ch2);


int main()
{
int nr = 0;
EditLine(CDFV,nr);
}

void EditLine(string CDFV, int &nr)
{
	string row;
	string word;
	std::string s;
    std::stringstream out;
	ifstream fd(CDFV.c_str());
	int zpr = 0, spr = 0;
	while (!fd.eof()){
		getline(fd,row);
		
        while ((zpr = row.find_first_not_of(CSKYR, spr)) != -1) // zpr - position of next word's first symbol
        {
		     nr++;      // word's number
             spr = row.find_first_of(CSKYR, zpr);   // find seperator position in row
             word = row.substr(zpr, spr - zpr);     // get a word
             if (SearchSymbols(word, 'o','y','a') == true ) {	
		        out << nr;
                s = out.str();
		        row.replace(zpr,spr-zpr,s);
        }	 			
   }  
		zpr = 0, spr = 0; 
		cout<<row<<endl;
}
	
fd.close();
}

bool SearchSymbols(string row, char ch, char ch1, char ch2)
{
   bool Symbols = false;
   for (unsigned int i = 0; i < row.length(); i++)
      if (tolower(row[i]) == tolower(ch)|| tolower(row[i]) == tolower(ch1) || tolower(row[i]) == tolower(ch2))
         Symbols = true;
   return Symbols;
}

Last edited on
i am sorry to tell you friend that your code looks so much pain. it is not readable. please if you can, use only meaningful variable names and function names.
eg. for a number, use "number" or at least "num" without using somthinlike "n".

I tried reading your code and found that you are passing all the characters at once to the SearchSymbols function, which is not very good. try doin it using an array of Chars and you can then just pass two arguments. it gives you the ability to upgrade your code to suite any number of chars just with few adjestments.

my advise is try doing this with
read()
and
write()
functions.

I am repling like this because i see that you havent got any replies and you maybe got stuck.
I havent used the libraries that you are using. ( i am more of a C person, not C++ )

try this library,
fcntl.h

its totally easy to handle, ask me if u need help with FCNTL.H header

here is an example that i made with fcntl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <fcntl.h>

#define TEXTSIZE 1000

char text[TEXTSIZE];

main(int argc, char *argv[] ) {

	int textfd,i=0;  // textfd is the File Descripter

	char currentChar;

	//initialize text array
	FILE *textfile = fopen(argv[1],"r");
	while((currentChar = getc(textfile))!=EOF){
		text[i] = currentChar;
		i++;
	}
	text[i] = '\0';
	printf("This is the text: %s\n",text);

}


also, i think those big numbers are memory addresses.
hope you get it done.
Cheers.

ps.
some links for you
http://www.cplusplus.com/reference/clibrary/cstdio/FILE/
http://www.cplusplus.com/reference/clibrary/cstdio/feof/
http://www.cplusplus.com/reference/clibrary/cstdio/fgetc/
http://www.cplusplus.com/reference/clibrary/cstdio/getc/
http://www.cplusplus.com/reference/clibrary/cstdio/fclose/
Topic archived. No new replies allowed.