Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-08-2005, 11:02 PM   #1 (permalink)
deepak_kr_mehta
banned
 
deepak_kr_mehta's Avatar
 
Join Date: May 2005
Posts: 23
deepak_kr_mehta is on a distinguished road
coding is not working

THIS FOLLOWING CODE IS NOT WORKING
Code:
#include<iostream.h>
#include<conio.h>
int main()
{
char ch;
fstream file("m.txt",ios::in|ios::ate|ios::out);
file.seekg(0);
while(file.eof()==0)
{
file.get(ch);
if(ch=='h')
ch='i';
}
file.seekg(0);
while(file.eof()==0)
{
file.get(ch);
cout<<ch;
}
return 0;
}
this code is for replacing the characters in the file named "m.txt" but it is not working and not showing any error.
plz solve this query
from
deepak kr.
deepak_kr_mehta is offline   Reply With Quote
Old 05-09-2005, 12:18 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
redhead is on a distinguished road
This is your problem
Code:
while(file.eof()==0)
{
file.get(ch);
if(ch=='h')
ch='i';
}
You identify the char correct, but once you've set ch to teh char you want to change it to, then you read past the point to place it.
A way of achieving it, would be to keep an array of positions, where the char was found, then in the end search through the file and replace the chars at the previus located postions.

Or you could simply replace it in the file, when ever you encounter it.
Something like this:
Code:
#include <iostream>
#include <fstream>

int main()
{
    char ch;
    fstream file("m.txt",ios::in|ios::ate|ios::out);
    file.seekg(0);
    while(file.eof()==0)
    {
        file.get(ch);
        if(ch=='h')
        {// seek back one char and replace it
            file.seekp(file.tellg() - sizeof(char));
            file << 'i';
        }
    }
    file.close();
    return 0;
}
Note: none of this code has been tested.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-12-2005, 03:44 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code like...
Code:
file.seekp(file.tellg() - sizeof(char));
...is not advisable, since the standard never guarantees proper conversion.
Also, after a read we want to reposition the write pointer. And after a write, we want to reposition the put pointer. Otherwise we end up with undefined behaviour.
Also...
Code:
while(file.eof()==0)
... we don't want to use eof() to decide whether file i/o should stop.
In general here is the code with comments:
Code:
#include <iostream>
#include <fstream>
#include <ios>

int main()
{
  //1) Open file in binary for more accurate positioning.
  //2) std::ios_base::ate is not needed.
  std::fstream file( "m.txt", std::ios_base::in |
    std::ios_base::out | std::ios_base::binary );
  //I initialize ch as well, just a neat thing to do.
  char ch( 0 );
  //Stream reader returns a value: acts like "false" if stream not good.
  while( file.get( ch ) )
  {
    if( 'c' == ch )
    {
      file.putback( ch );
      //Reposition put pointer after a read.
      file.tellp();
      file.put( '*' );
      //reposition get pointer after a write.
      file.tellg();
    }
  }
  return 0;
}
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mudding And coding. DiaBoliK Standard C, C++ 3 01-23-2005 10:36 AM
Does anyone know programming and coding?? dsantamassino Lounge 5 10-31-2004 02:16 PM
coding with notices on sde PHP 1 05-23-2003 08:43 PM
what to drink while coding saline Lounge 24 03-05-2003 10:45 PM
cgi not working on a new script sde PHP 3 05-20-2002 08:29 PM


All times are GMT -8. The time now is 10:34 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting