Reading line by line two files, but with different line positions?

This is a discussion on Reading line by line two files, but with different line positions? within the C++ Programming forums, part of the General Programming Boards category; hey ^^ Well, I got a hold of Borland C++ Builder 6 trial, so it's all good now I guess. ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    22

    Reading line by line two files, but with different line positions?

    hey ^^

    Well, I got a hold of Borland C++ Builder 6 trial, so it's all good now I guess. However my friend still wants me to use TurboC++ 3.01 for DOS, so if you know how to make TLINK.EXE work correctly, or what are the environment variables to configure so it works, I would be very, very glad .

    Anyways, this is not just advertisement... >_>;; as I didn't want to lose time waiting for a solution to my TC++ problem, I'm trying with BC++ 6 trial, and got this far:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    using namespace std;
    
    /***********************************************************************
    ************************************************************************
    ***********************************************************************/
    
    int main()
    {
      string LineNEW, LineOLD;
      int line;
    
      ifstream esp ("new.txt", ios::in);
      ifstream old ("old.txt", ios::in);
      ofstream to ("output.txt", ios::out);
      if (!esp.is_open()){ cout << "Error loading new.txt\n"; getchar(); return 0; }
      if (!old.is_open()){ cout << "Error loading old.txt\n"; getchar(); return 0; }
      if (!to.is_open()){ cout << "Error creating output.txt\n"; getchar(); return 0; }
    
      while (! esp.eof())
      {
        line += 1; 
        getline (esp, LineNEW);
        getline (old, LineOLD);
    
        cout << "New: " << LineaESP << endl;
        cout << "Old: " << LineaOLD << endl << endl;
      }
    
      cout << "Finished.";
      esp.close();old.close();to.close();
      getchar();
      return 0;
    }
    I need to read two files, compare strings between the two, and if there needs something to be replaced, write it in output.txt, along with the unchanged stuff.

    I would like to know if it's possible to move the read pointer to the previous/next line, or to a specific one, instead of abstract positions I get when using tellg() and seekg()... or how to read/write files using LINES instead of pointers and positions and all that "detailed small stuff". And, if it's possible, to have an independent position between New and Old, so like, if Line #2 in New does not match Line #2 in Old, move to the next line in Old so it matches Line #2 in New... that's for avoiding line breaks and such (the two files have the same structure, though sometimes they're separated with more than one line break, so I'd like to manually handle that).

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > However my friend still wants me to use TurboC++ 3.01
    Your friend wants to cripple you before you've even begun?
    That ancient fossil knows nothing about modern C++ practices or features.
    It would be much better if you got your friend to upgrade to something in this century.

    > #include <iostream.h>
    If you're going to use namespaces, then you should have
    #include <iostream>

    > #include <string.h>
    string.h is a C header, it does not declare your string object later on.

    > while (! esp.eof())
    Read the FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    > line += 1;
    What was the initial value of line?

    > instead of abstract positions I get when using tellg() and seekg()
    That's all there is
    You could do these things

    // record where this line starts, and read it
    pos[line] = tell();
    getline();

    // go back to a specific line and read it
    seekg(pos[line]);
    getline();

    Though unless both files are huge, reading the whole lot into memory and doing whatever you want should be considered (say a std::vector of strings).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading words line by line from a file
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 12:34 AM
  2. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  3. Reading file line by line
    By chrisjmoss in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 01:58 PM
  4. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 05:21 PM
  5. line reading problem
    By papous in forum C Programming
    Replies: 2
    Last Post: 11-22-2001, 11:29 AM

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