Thread: file i/o

  1. #1
    Unregistered
    Guest

    file i/o

    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?

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    can you just use getline() then run it through a loop? or am i outta my league?

  3. #3
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    This is what I use

    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.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    49
    Use grep instead of programming yourself.
    Hello, everyone.

  5. #5
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    What is grep?

  6. #6
    Unregistered
    Guest
    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);
    }

  7. #7
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Smile gogogo!

    Refrain from using grep

    Coding it urself is much more exciting. It helps sharpen ur skills.

  8. #8
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM