i have text file with a series of words in it. some word contain the expression '.zip' , i'm trying to rerad the characters of the file and replace the .'zip' with '.rar'

this is my prog, bu it's not responding

#include <iostream.h>
#include <fstream.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
	fstream f1;
	f1.open ("student.txt", ios::in | ios:: out);
	//f1 << "This is shit!";

 int i=0;
static char str[100];


while (!f1.eof())
{
	f1.getline(str,100);
	f1<<str<<endl;
if (str[i]=='z')

{cout<<str[i+2];
	
		if(str[i+1]=='i')
	
		{if(str[i+2]=='p')
		{
			str[i]=='r';
			str[i+1]='a';
			str[i+2]='r';
			}
	}
}

i++;

	
}

f1.close();
}

Wow, um, I'd probably better give you a better example first:

#include <string.h>
#include <iostream.h>

int main()
{
  char line[100];

  while ( cin.getline ( line, sizeof line ) ) {
    char *p = strstr ( line, ".zip" );

    if ( p != 0 )
      strcpy ( p, ".rar" );

    cout<< line <<'\n';
  }

  return 0;
}

So, let's start from the top in your code:

>#include <iostream.h>
>#include <fstream.h>
I'll assume you're using a pre-standard compiler, but you really should get a new one and start using standard C++. Many modern compilers won't even accept your code.

>#include<stdlib.h>
You don't use anything from stdlib.h, so don't include it.

>#include<conio.h>
You don't even use names from conio.h, so don't include it.

>void main()
main returns int in C++, it always has.

>fstream f1;
>f1.open ("student.txt", ios::in | ios:: out);
You can combine these, as fstream has a constructor that opens a file:

fstream f1 ( "student.txt", ios::in | ios::out );

Also, f1 isn't a very good name.

>static char str[100];
The static keyword is unnecessary here.

while (!f1.eof())
{
  f1.getline(str,100);

This is a buggy pattern that can easily be fixed. The bug is that the eof() member function doesn't return true until after you've tried and failed to read from the file. But because you're already in the body of the loop at that point, you're going to process a failed read. Most of the time that results in processing the last line of the file twice. The fix is very simple, as getline returns the stream state:

while ( f1.getline ( str, 100 ) )
{
  // Process str

>f1<<str<<endl;
The rule for read/write streams is that you cannot switch from read to write (or write to read) without an intervening flush or seek. It's a tricky little dance, which is why I recommend using two separate stream objects: one for input and one for output. Unless you really know what you're doing, it's also best to use two separate files as well.

Your search for ".zip" is incorrect. You need to search the while string, but you only search the first three characters starting with i. Note that i is only incremented after processing each line, so on the first line you'll process characters 0, 1, and 2. On the second line you'll process characters 1, 2, and 3, and so on. This becomes a problem if the string doesn't contain enough characters and you access an element outside the bounds of the array. What you need is a nested loop:

size_t len = strlen ( str );

for ( i = 0; i < len - 3; i++ ) {
  if ( str[i] == '.'
    && str[i + 1] == 'z'
    && str[i + 2] == 'i'
    && str[i + 3] == 'p' )
  {
    str[i + 1] = 'r';
    str[i + 2] = 'a';
    str[i + 3] = 'r';
  }
}

>f1.close();
The destructor will close your file automatically.

Here's something more complete that shows the changes I'm talking about:

#include <string.h>
#include <iostream.h>

int main()
{
  char str[100];

  while ( cin.getline ( str, 100 ) ) {
    cout<<"Before: "<< str <<'\n';

    int len = strlen ( str );
    int i;

    for ( i = 0; i < len - 3; i++ ) {
      if ( str[i] == '.'
        && str[i + 1] == 'z'
        && str[i + 2] == 'i'
        && str[i + 3] == 'p' )
      {
        str[i + 1] = 'r';
        str[i + 2] = 'a';
        str[i + 3] = 'r';
      }
    }

    cout<<"After: "<< str <<'\n';
  }

  return 0;
}

thanx ever so much
it wuz gr8 help
i thought i knew a bit of c++ ! ;)

This article has been dead for over six months. Start a new discussion instead.