any real l33t people wanna check this out for me

This is a discussion on any real l33t people wanna check this out for me within the C++ Programming forums, part of the General Programming Boards category; im not sure where my bug is but something is acting really weird in here Code: #include <iostream.h> #include <fstream.h> ...

  1. #1
    Unregistered
    Guest

    any real l33t people wanna check this out for me

    im not sure where my bug is but something is acting really weird in here
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    
    int main()
    {
    	char totalcontents[10000]="";
    	char FileContents[500], WordToDelete[100], WordToInsert[100], StoreFileContents[500];
    	char filename[100];
    	cout<<"Enter the name of file: ";
    	cin>>filename;	
    	cout<<"Enter word to search for: ";
    	cin>>WordToDelete;
    	cin.ignore(20,'\n');
    	cout<<"Enter word to replace it with: ";
    	cin.getline(WordToInsert,20);
    	fstream file;
    	file.open(filename,ios::nocreate|ios::in|ios::out);
    	if(file.fail()){
    		cout<<"File could not be opened"<<endl;
    		return (1);
    	}	
    	while(file.getline(FileContents,500,'\n'))
    	{
    		int length1=strlen(FileContents);
    		int length2=strlen(WordToDelete);
    		int length3=strlen(WordToInsert);
    		int count,count2,count3,count4=0,count5=0;
    		for(count=0;count<=length1;count++)
    		{		
    			if(FileContents[count]==WordToDelete[0])
    			{
    				for(count2=count;count2<=(count+length2);count2++)
    				{
    					//if(FileContents[count2]!=WordToDelete[count2-count])
    					//	break;
    					if(count2==(count+length2)){
    						strcpy(StoreFileContents,FileContents);
    						if(length2==length3)
    						{
    							for(count3=count;count3<(count+length2);count3++)
    							{
    								FileContents[count3]=WordToInsert[count3-count];
    							}
    						}
    						else if(length2<length3)
    						{	
    							for(count3=count;count3<(count+length3);count3++)//replace original
    							{
    								FileContents[count3]=WordToInsert[count3-count];
    							}
    							length1=(length3-length2)+length1;
    							for(count4=count3;count4<length1;count4++)
    							{
    								FileContents[count4]=StoreFileContents[count4-1];
    							}						
    						}
    						else if(length2>length3)
    						{
    							for(count3=count;count3<(count+length3);count3++)//replace original
    							{
    								FileContents[count3]=WordToInsert[count3-count];
    							}
    							length1=(length3-length2)+length1;
    							for(count4=count3;count4<length1;count4++)
    							{
    								FileContents[count4]=StoreFileContents[count4+1];
    							}
    							FileContents[length1]=' ';
    						}
    			
    					}
    				}
    			}		
    		}	
    	//here we put the thingy back in		
    	strcat(totalcontents,FileContents);
    	}	
    	file.close();
    	file.open(filename,ios::trunc|ios::in|ios::out);
    	if(file.fail()){
    		cout<<"File could not be opened"<<endl;
    		return (1);
    	}
    	file<<totalcontents;
    	file.close();
    	return (0);
    }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    >something is acting really weird in here

    What happened when you tested it with what input and what did you expect instead ? We need information to help you.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    My guess is that the file contents disappear.. you'll have to put ios::ate alongwith ios:ut.

    but as nvoigt said we need more info.

  4. #4
    Unregistered
    Guest
    here is an update it working a bit better now yay
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    
    int main()
    {
    	char FileContents[500]="i gotta go to the store gottascape", WordToDelete[100]="gotta", WordToInsert[100]="got to", StoreFileContents[500];
    	/*char filename[100];
    	cout<<"Enter the name of file: ";
    	cin>>filename;	
    	cout<<"Enter word to search for: ";
    	cin>>WordToDelete;
    	cin.ignore(20,'\n');
    	cout<<"Enter word to replace it with: ";
    	cin.getline(WordToInsert,20);
    	*/
    		int length1=strlen(FileContents);
    		int length2=strlen(WordToDelete);
    		int length3=strlen(WordToInsert);
    		int count,count2,count3,count4=0,count5=0;
    		cout<<FileContents<<endl;
    		for(count=0;count<=length1;count++)
    		{		
    			if(FileContents[count]==WordToDelete[0])
    			{
    				for(count2=count;count2<=(count+length2);count2++)
    				{
    					if((count2-count)==(length2)){
    						strcpy(StoreFileContents,FileContents);
    						if(length2==length3)
    						{
    							for(count3=count;count3<(count+length2);count3++)
    							{
    								FileContents[count3]=WordToInsert[count3-count];
    							}
    						}
    						else if(length2<length3)
    						{								
    							for(count3=count;count3<(count+length3);count3++)//replace original
    							{
    								FileContents[count3]=WordToInsert[count3-count];
    							}
    							length1=(length3-length2)+length1;
    							for(count4=count3;count4<length1;count4++)
    							{
     								FileContents[count4]=StoreFileContents[count4-(length3-length2)];
    							}						
    						}
    						else if(length2>length3)
    						{
    							for(count3=count;count3<(count+length3);count3++)//replace original
    							{
    								FileContents[count3]=WordToInsert[count3-count];
    							}
    							length1=(length3-length2)+length1;
    							for(count4=count3;count4<length1;count4++)
    							{
     								FileContents[count4]=StoreFileContents[count4+(length2-length3)+1];
    							}							
    						}
    			
    					}
    					if((FileContents[count2]!=WordToDelete[count2-count])||(FileContents[count+length2]!=' '))
    						break;
    				}
    			}		
    		}	
    	cout<<FileContents<<endl;
    	return (0);
    }

  5. #5
    Unregistered
    Guest
    ok new problem here i what im trying to do...
    i want to replace all occurances of a word in a file with another word...

    i need some help on how to replace the stings that im chanign back into the file!

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    
    int main()
    {
    	int numberoflines=0;
    	char FileContents[500], WordToDelete[100], WordToInsert[100], StoreFileContents[500], filename[100];;
    	cout<<"Enter the name of file: ";
    	cin>>filename;	
    	cout<<"Enter word to search for: ";
    	cin>>WordToDelete;
    	cin.ignore(20,'\n');
    	cout<<"Enter word to replace it with: ";
    	cin.getline(WordToInsert,20);
    		
    	ifstream file;
    	file.open(filename,ios::nocreate);
    	if(file.fail()){
    		cout<<"File could not be opened"<<endl;
    		return (1);
    	}
    
    	while(file.getline(FileContents,500,'\n'))
    	{	
    		numberoflines++;
    		int length1=strlen(FileContents);
    		int length2=strlen(WordToDelete);
    		int length3=strlen(WordToInsert);
    		int count,count2,count3,count4=0,count5=0, count6=0;		
    		for(count=0;count<=length1;count++)
    		{		
    			if(FileContents[count]==WordToDelete[0])
    			{
    				for(count2=count;count2<=(count+length2);count2++)
    				{
    					if((count2-count)==(length2)){
    						strcpy(StoreFileContents,FileContents);
    						if(length2==length3)
    						{
    							for(count3=count;count3<(count+length2);count3++)
    							{
    								FileContents[count3]=WordToInsert[count3-count];
    							}
    						}
    						else if(length2<length3)
    						{								
    							for(count3=count;count3<(count+length3);count3++)//replace original
    							{
    								FileContents[count3]=WordToInsert[count3-count];
    							}
    							length1=(length3-length2)+length1;
    							for(count4=count3;count4<length1;count4++)
    							{
      								FileContents[count4]=StoreFileContents[count4-(length3-length2)];
    							}						
    						}
    						else if(length2>length3)
    						{
    							count6++;
    							for(count3=count;count3<(count+length3);count3++)//replace original
    							{
    								FileContents[count3]=WordToInsert[count3-count];
    							}
    							length1=(length3-length2)+length1;
    							for(count4=count3;count4<length1;count4++)
    							{
      								FileContents[count4]=StoreFileContents[count4+(length2-length3)];
    							}
    							for(count4;count4<((length2-length3)+length1);count4++)
    							{
    								FileContents[count4]=' ';
    							}
    						}
    			
    					}
     					if((FileContents[count2]!=WordToDelete[count2-count])||((FileContents[count+length2]!=' ')&&(FileContents[count+length2]!='.')))
    						break;
    				}
    			}		
    		}
    		for(count3=0;count3<length1;count3++)
    		{
    			cout<<FileContents[count3];
    		}
    		cout<<endl;
    	}
    	file.close();
    	return (0);
    }

  6. #6
    Unregistered
    Guest
    You have the whole file stored in FileContents right? So why don't you just rewrite the whole file from your updated string using ios::trunc (Although technically by putting ios:ut it implies ios::trunc)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i can't check out people profiles
    By invisibleghost in forum Tech Board
    Replies: 2
    Last Post: 07-31-2004, 01:42 PM
  2. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  3. the us constitution
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 121
    Last Post: 05-28-2002, 04:22 AM
  4. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 03:38 PM
  5. Religious Bull****
    By Witch_King in forum A Brief History of Cprogramming.com
    Replies: 119
    Last Post: 10-23-2001, 07:14 AM

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