Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8810367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:06:19+00:00 2026-06-14T03:06:19+00:00

I was trying to use what i have learned about file and resource handling

  • 0

I was trying to use what i have learned about file and resource handling in C++: I would like to write a diff-like utility.

Here It is my latest version

#include <iostream>
#include <cstdlib>
#include <fstream>

int main(int argc, char* argv[])
{
  if(argc!=3)
  {
    std::cout << "error: 2 arguments required, now exiting ..." << std::endl;
    exit (EXIT_FAILURE);
  }

  std::ifstream file_1(argv[1]);
  std::ifstream file_2(argv[2]);

  if( file_1.fail() || file_2.fail() )
  {
    std::cout << "error: can't open files, now exiting ..." << std::endl;
    exit (EXIT_FAILURE);
  }

  std::string dummy_1;
  std::string dummy_2;

  while(!file_1.eof()) // dummy condition
  {
    std::getline(file_1,dummy_1);
    std::getline(file_2,dummy_2);
    std::cout << ((dummy_1==dummy_2) ? "= " : "# ") << dummy_1 << std::endl << "  " << dummy_2 << std::endl;
  }

  return(0);
}

This are my guidelines:

  • compare 2 files
  • the user must pass the names of this 2 files directly to the executable, only this 2 arguments
  • to cover as much error handling as possible in C++
  • try to avoid platform specific steps or non-portable code

My actual problem is that i don’t know how to improve my dummy condition effectively.
For now the while iteration just follows the length of the first passed file and I would like to obiviously go all the way down in both files and solve this without introducing an overkill like an extra cicle to get and compare the length of this 2 files before doing the real comparison.

I also would like to know if my approach can be considered safe.

Eventually I could also accept answers proposing a solution with the boost libraries since they are quite portable and I already know that i will use them for other reasons.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:06:20+00:00Added an answer on June 14, 2026 at 3:06 am

    I started by writing rather a long comment on @Loki Astari’s answer, but it’s long enough (and, IMO, enough cleaner way to do the job) that it probably makes the most sense as an independent answer. In this case, you want something close to the standard loop, except that you keep reading as long as a read from one of the files succeeds. That being the case, @john is right, and it’s best to avoid using eof() as part of the loop condition.

    std::string line1, line2;
    static const char *prefixes[] = {"#  ", "=  "};
    


    while (std::getline(file_1, line1) || std::getline(file_2, line2))
    std::cout << prefixes[line1==line2] << line1 << “\n ” << line2 << “\n”;

    Edit: @user1802174 raised a good point — as it was, the loop didn’t actually read data in parallel at all. Since it was using || which does short-circuit evaluation, when/if the read from the first file succeeded, it didn’t read anything from the second file. Fortunately, he was wrong about one thing: it is fairly easy to fix. At least in this case, + works fine, although we do have to explicitly cast the result to bool. I’ve also added a fix for the fact that upon failure, getline leaves the previous content of the string intact, so we need to explicitly clear the strings every iteration of the loop to get the desired behavior.

    while (line1.clear(), line2.clear(), 
          (bool)std::getline(file_1, line1) + (bool)std::getline(file_2, line2))
    {
        std::cout << prefixes[line1==line2] << line1 << "\n   " << line2 << "\n";
    }
    

    This time I did a quick test. File 1:

    line1
    line 2
    

    File 2:

    line 1
    line 2
    line 3
    

    result:

    #  line1
       line 1
    =  line 2
       line 2
    #
       line 3
    

    While obviously still not a full-blown diff utility, I think this is doing what was intended.

    As in @Loki Astari’s answer, this will basically act as if the file with fewer lines was padded with as many empty lines at the end as necessary to match the longer file.

    As an aside, also note the use of "\n" instead of std::endl. Along with inserting a new-line, std::endl also flushes the output buffer, which you almost certainly don’t want in this case. Flushing the buffer still produces the correct results, but in many cases is likely to do so much more slowly.

    Edit: As far as coding style goes, it probably is a bit better to write the loop as a for loop instead of a while:

    for ( ; (bool)std::getline(file_1, line1) + (bool)std::getline(file_2, line2))
          ; line1.clear(), line2.clear())
    {
        std::cout << prefixes[line1==line2] << line1 << "\n   " << line2 << "\n";
    }
    

    I personally see little real gain from using C++ style casts here. If I wanted to get away from using (bool), I’d probably use another well-known idiom (which, admittedly, many people also dislike):

    for ( ; !!std::getline(file_1, line1) + !!std::getline(file_2, line2))
          ; line1.clear(), line2.clear())
    {
        std::cout << prefixes[line1==line2] << line1 << "\n   " << line2 << "\n";
    }
    

    If somebody really objects to using a comma operator, this is easy to rewrite as:

    while (!!std::getline(file_1, line1) + !!std::getline(file_2, line2))       
    {
        std::cout << prefixes[line1==line2] << line1 << "\n   " << line2 << "\n";
        line1.clear();
        line2.clear();
    }
    

    Personally, I don’t consider that an improvement, but others may disagree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make sense of whatever I learned about NPRuntime . Here
I'm trying to use the Ajax File Upload as featured here: http://valums.com/ajax-upload/ As you
Trying to use JSTL but have the following problem: Index.xhtml page: <?xml version=1.0 encoding=UTF-8?>
Hi I'm trying use a datepicker on a field I have. I'm trying to
I am trying use gem tire to search in my application. I have tables
I have been trying to use date_format , however as far as I know,
I have been trying to use array_unique to remove duplicates from the search results
I have been trying to use routes.rb for creating a URL /similar-to-:product (where product
I have been trying to use the hibernate dialect for SQLite from http://code.google.com/p/hibernate-sqlite/ in
I have am trying to use sed to get some info that is encoded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.