Opening and editing text files(c++)

  • Drac
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Jan 31, 2004
  • Posts: 11
  • Loc: In my chair.... Unless Im not
  • Status: Offline

Post February 2nd, 2004, 10:52 pm

Hey all, I am just wondering in c++ how do I access a text file, and find and replace a string of text, as I am writting a script converter(sphereserver).
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post February 2nd, 2004, 10:52 pm

  • Drac
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Jan 31, 2004
  • Posts: 11
  • Loc: In my chair.... Unless Im not
  • Status: Offline

Post February 3rd, 2004, 10:22 pm

Sorry for bump :(, But I really need to know this, any documentation that might lead me in the right direction would be greatly appreciated
  • RichB
  • Guru
  • Guru
  • User avatar
  • Joined: May 17, 2003
  • Posts: 1121
  • Loc: Boston
  • Status: Offline

Post February 4th, 2004, 1:09 am

Here are some File I/O tutorials/articles that I found through Google:

http://www.cpp-home.com/FileIO_tutorial.php
http://www.gamedev.net/reference/articl ... le1127.asp
http://www.cs.uregina.ca/links/class-in ... ileio.html
http://www.cs.hmc.edu/~geoff/classes/hm ... es/io.html
http://www.ecst.csuchico.edu/~juliano/C ... s-cpp.html
http://cplus.about.com/library/weekly/aa051802a.htm
http://www.cplusplus.com/doc/tutorial/tut6-1.html
http://gethelp.devx.com/techtips/cpp_pr ... in0601.asp

You can find more by searching google for things like C++ "file i/o". But unless you already know some C++ I think it's going to be a bit of a stretch to jump right into file i/o.
Free Programming Resources
  • rjmthezonenet
  • Expert
  • Expert
  • User avatar
  • Joined: Jan 14, 2004
  • Posts: 526
  • Loc: St. John's, Newfoundland, Canada
  • Status: Offline

Post February 4th, 2004, 8:13 am

Yes, why have you choosen a tool like c++ for a string replacement? Its a bit like using a power drill to tighten the screws on your glasses.

This is a job for Perl, or something similar.
  • Drac
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Jan 31, 2004
  • Posts: 11
  • Loc: In my chair.... Unless Im not
  • Status: Offline

Post February 4th, 2004, 4:05 pm

Hehe, Your not suppose to use a power drill to to tighten glasses? :oops:

Thanks, Ill look into those :D.
  • rjmthezonenet
  • Expert
  • Expert
  • User avatar
  • Joined: Jan 14, 2004
  • Posts: 526
  • Loc: St. John's, Newfoundland, Canada
  • Status: Offline

Post February 5th, 2004, 8:06 am

Yeah, check out Perl. Let's say you want to replace cat with dog... but, only in the second column of a tab delimited text file, here's an example:

Code: [ Select ]
#/usr/bin/perl -w

# Put before/after in strings
# You can just as easily take them from the command line.
$stringa = "cat";
$stringb = "dog";

# Loop through each line of standard input. Perl is so smart, you
# really don't have to give it much options.
while (<>) {
 chomp;                     # chew off the line endings.
 @row = split (/\t/,$_);          # split each line into array. use tabs.
 $count = 1;
 foreach $col (@row) {          # loop through each array element
  if ($count == 2) {            # if 2nd column, do substitution
   $col =~ s/$stringa/$stringb/;  # switch a for b!!
   print "$col\t";               # print out new version
  } else {
   print "$col\t";
  }
 $count++;
 }
 print "\n";                    # each line in tab file ends with LF
}
  1. #/usr/bin/perl -w
  2. # Put before/after in strings
  3. # You can just as easily take them from the command line.
  4. $stringa = "cat";
  5. $stringb = "dog";
  6. # Loop through each line of standard input. Perl is so smart, you
  7. # really don't have to give it much options.
  8. while (<>) {
  9.  chomp;                     # chew off the line endings.
  10.  @row = split (/\t/,$_);          # split each line into array. use tabs.
  11.  $count = 1;
  12.  foreach $col (@row) {          # loop through each array element
  13.   if ($count == 2) {            # if 2nd column, do substitution
  14.    $col =~ s/$stringa/$stringb/;  # switch a for b!!
  15.    print "$col\t";               # print out new version
  16.   } else {
  17.    print "$col\t";
  18.   }
  19.  $count++;
  20.  }
  21.  print "\n";                    # each line in tab file ends with LF
  22. }


(This is, by the way, my second Perl script. Its not hard to learn).

You would then run the script as scripname filename.tab. You may also take input from comman line re-directs, files, databases, heck... you name it! Output, in this example, goes to the screen. You could re-direct it (>) to a file, blah blah.... you get the picture. :-)

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 227 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.