file i/o

This is a discussion on file i/o within the C++ Programming forums, part of the General Programming Boards category; how can i replace the word im searching for? my way doesnt work Code: #include <iostream.h> #include <fstream.h> #include <string.h> ...

  1. #1
    Unregistered
    Guest

    file i/o

    how can i replace the word im searching for? my way doesnt work

    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(file.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);
    }

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    what doesn't work about it? i'll try to help, but give me a little more info

  3. #3
    Unregistered
    Guest
    everything works except im having trouble replacing the original text with the new one... not sure how to do it

  4. #4
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    would something like this work:

    Code:
    if(input[count]==wordtofind[0]){
    	temp[/*looped index*/]=input[count];
    	//then ostream temp?
    i wish i could help more, but i'm really not knowledgable with io stuff.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    unregistered,

    How did you manage to get past

    cin.ignore(20,'\n');?

    This should have ignored the first 20 characters (or so) of your data file name.

    A hint , perhaps?

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    Unregistered
    Guest
    Originally posted by skipper
    unregistered,

    How did you manage to get past

    cin.ignore(20,'\n');?

    This should have ignored the first 20 characters (or so) of your data file name.

    A hint , perhaps?

    compile it buddy it does exactly what i want it to do :P

  7. #7
    Unregistered
    Guest
    Originally posted by blight2c
    would something like this work:

    Code:
    if(input[count]==wordtofind[0]){
    	temp[/*looped index*/]=input[count];
    	//then ostream temp?
    i wish i could help more, but i'm really not knowledgable with io stuff.
    but that wont work if the word i want to replace it by is bigger than the original corrrect?

  8. #8
    Unregistered
    Guest
    anyone have any other suggestion? here is what i have now....

    this is way harder than i tought it would be

    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,length3=0, count=0, count2=0,count3=0,count4=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(file.getline(input,200,'\n'))
    		{
    			length2=strlen(input);
    			length3=strlen(wordtoreplace);
    			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){							
    							for(count3=0;count3<count;count3++)
    							{
    								file.seekp(count3);
    								file<<input[count3];
    							}
    							for(count4=0;count4<length3;count3++)
    							{
    								file.seekp(count3++);
    								file<<wordtoreplace[count3];
    							}
    							for(count3;count3<length2;count3++)
    							{
    								file<<input[count3];
    							}
    						}
    					}
    				}
    			}
    		}
    		}
    	file.close();
    
    	return (0);
    }

  9. #9
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    there is ios::ate that lets you overwrite on already existing data of a file. you could try that.

    but first you'll have move filecursor to the place where the text is and then write over there.

  10. #10
    Unregistered
    Guest
    Originally posted by ihsir
    there is ios::ate that lets you overwrite on already existing data of a file. you could try that.

    but first you'll have move filecursor to the place where the text is and then write over there.
    but that wont work if im searching for the word "you" and for example i want to replace it with "thou"

    right?

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I would read entire file into program, storing input into a large array. I would then search the array for the first letter of the desired word and if found search for all subsequent letters of the desired word. If subseqeunt letters don't match I would advance from where I had left off. I all letters match I would delete the desired word and replace it with the new word. When I was all done I would rewrite back to the file. Except for some minor details, that first level approach is pretty much what you are trying to do.

    The details are all important however. FIrst, you have to know the relative size of the two words in question. If the word to be deleted is the same length as the word to add, then you can just identify the starting point and overwrite the appropriate elements of the array.

    If the word to be replaced is shorter than the replacement then you need to shift the contents of the array to the right by the amount of the difference.

    If the word to be replaced is longer than the replacement, then you need to shift the contents of the array to the left by the amount of the difference, assuming there is enough space in the array to handle it.

    I would use the length of the shorter word in the loop for comparison.

    If you feel comfortable with lists, you could use those instead of arrays for the container to hold the file's content if you wish.

    Code:
    char fileContents[100] = "three blind mice";
    char wordToDelete[] = "three";
    char wordToInsert[] = "two";
    int length1 = strlen(wordToDelete);
    int length2 = strlen(wordToInsert);
    int i, j;
    int search;
    int difference;
    int start;
    int stop;
    if(length1 <= length2)
    {
    search = length1;
    difference = length2 - length1;
    }
    else
    {
    search = length2;
    difference = lenth1 - length2;
    }
    for(i = 0; i < search; i++)
    {
       if(fileContents[i] = wordToDelete[0];
       {
          start = i;
          for(j = 0; j < length1; j++)
          {
              if(fileContents[i + j] != wordToDelete[j];
                 j = length1 + 10;
          }
          if(j == length1)
          {
              if(length1 == length2)
              {
                  for(j = start; j < start + length1; j++)
                  {
                      fileContents[start + j] = wordToInsert[j];
                   }
               }
               else if(length1 > length2)
               {
                   for(j = start; j < start + length2; j++)
                   {
                       fileContents[start + j] = wordToInsert[j];
                   }
                   for(j = start + length2; j < strlen(fileContents) + 1; j++)
                   {
                       fileContents[j] = fileContents[j + difference]
                    }
                else if(length1 < length1)
                ////you can do the rest, I gotta run, sorry

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, 08:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01: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, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21