Opening and editing text files(c++)

  • Drac
  • Newbie
  • Newbie
  • Drac
  • Posts: 11
  • Loc: In my chair.... Unless Im not

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Drac
  • Newbie
  • Newbie
  • Drac
  • Posts: 11
  • Loc: In my chair.... Unless Im not

Post 3+ Months Ago

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
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

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.
  • rjmthezonenet
  • Expert
  • Expert
  • User avatar
  • Posts: 526
  • Loc: St. John's, Newfoundland, Canada

Post 3+ Months Ago

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
  • Drac
  • Posts: 11
  • Loc: In my chair.... Unless Im not

Post 3+ Months Ago

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
  • Posts: 526
  • Loc: St. John's, Newfoundland, Canada

Post 3+ Months Ago

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 83 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
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.