what search method and what replacing method should i use to search a file for occurances of a word and then replace those occurances with another word?
what search method and what replacing method should i use to search a file for occurances of a word and then replace those occurances with another word?
can you just use getline() then run it through a loop? or am i outta my league?
The function that I use is this:
You may have to create this into a Prototype function or else put inside of a Class.
int msovf::Instr(LPCSTR String,LPCSTR Search)
{
CHAR *sResult;
int iLocation;
sResult = strstr(String,Search);
iLocation = sResult - String;
if(iLocation < 0)
{
return -1;
}
return iLocation;
}
The 'strstr' call searches for text... basically. So you do something like this:
int x = Instr("Where did my car go!?","car");
Then I believe it should return a 12(in this case, otherwise the location of whatever you are looking for). If the instance is not found it should return a -1. I hope that this works to help you with whatever you need it for.
Use grep instead of programming yourself.
Hello, everyone.
i kinda have to code it the old school way :P
here what i have to date :P
doesnt work yet :P
Code:#include <iostream.h> #include <fstream.h> #include <string.h> int main() { char filename[20], wordtofind[20], wordtoreplace[20], input[200]; int length=0,length2=0, count=0, count2=0, temp; cout<<"Enter the name of file: "; cin>>filename; cin.ignore(20,'\n'); cout<<"Enter word to search for: "; cin.getline(wordtofind,20); cout<<"Enter word to replace it with: "; cin.getline(wordtoreplace,20); length=strlen(wordtofind); fstream file; file.open(filename,ios::nocreate|ios::in|ios::out); if(file.fail()){ cout<<"File could not be openend"<<endl; return (0); } else{ while(cin.getline(input,200,'\n')) { length2=strlen(input); for(count=0;count<length2;count++) { if(input[count]==wordtofind[0]){ temp=count; for(count2=0;count2<=length;count2++) { if(input[temp++]!=wordtofind[count2]) break; if(count2==length) { file<<wordtoreplace; } } } } } } file.close(); return (0); }
Refrain from using grep
Coding it urself is much more exciting. It helps sharpen ur skills.
What? oldschool way? Why?
I guess you could just use CopyMemory to copy the string into an array then check it byte by byte... until you find alternatives.
But still... why what you call oldschool?