简体   繁体   中英

Finding the longest line in a file, Comparison between signed and unsigned integer expressions

Hello all I am working on an assigment that finds the longest word and line in a file, I am currently working on it but got stuck on when I received this error.

error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]

Now i understand the error but i am having a hard time fixing it, I have tried fixing it on my own first ofcourse but was forced to resort this for help.

#include <iostream>
#include<fstream>

using namespace std;

int main(int argc, char *argv[]) {

   int longest = 0;
   string line;

    if(argc == 0)
    {
       cout << " " << endl;
    }
   else
   {

   for(int i = 1;i<argc;i++){
      ifstream file (argv[i]); //declare in the for loop
      if (!file.is_open() ){
      cout << argv[i] << " FILE NOT FOUND\n"; // watch out for /n
      }
      else{
         while (getline(file,line))
         {
            if(line.size() > longest){
               longest = line.size();
               cout << "The length of the longest Line is: " << longest << endl;
         }
         }


}
   }
   }
   }          

I know the error is here:

if(line.size() > longest){ 

EDIT: SOLUTION: The fix here was to declare longest using size_t instead of int, what I tried doing when trying to fix it was include size_t in the if statement, which is completely wrong on my part.

WRONG: int longest = 0; RIGHT: size_t longest = 0; This only applies in cases like these though.

As mentioned, you should make longest unsigned.

Besides, the check argc==0 should be argc<2 (because argv[0] is the program name).

Finally, it seems weird to reprint the "longest line" claim any time a line is longer than the current max.

Bonus: there's no real need to read the data into strings, which incurs memory allocation costs. You can just count the bytes between newlines.

Here's my simplified take:

Live On Coliru

#include <vector>
#include <iostream>
#include <fstream>
#include <iterator>

namespace {
    struct accumulator {
        size_t peak = 0;
        void operator()(size_t n) { peak = std::max(n, peak); }
    };
}

int main(int argc, char **argv) {
    accumulator sample;

    for(auto fname : std::vector<std::string>(argv+1, argv+argc)) {
        std::ifstream ifs(fname, std::ios::binary);
        if (!ifs) {
            std::cout << fname << " FILE NOT FOUND\n";
            continue;
        }

        std::istreambuf_iterator<char> f(ifs), l;

        auto scan_until = [&](char delimiter) {
            size_t count = 0;
            while (f!=l && *f++ != delimiter) ++count;
            return count;
        };

        while (f!=l) 
            sample(scan_until('\n'));
    }

    std::cout << "The length of the longest line is: " << sample.peak << "\n";
}

When run on its own source:

The length of the longest line is: 78

Bonus

Simple change to print a longest line per file as well:

Live On Coliru

/Archive2/8b/001ad4e031178d/main.cpp 44
/Archive2/8b/00964b446531eb/main.cpp 37
/Archive2/8b/01880ea6d95d38/main.cpp 50
/Archive2/8b/029f129c393ce5/main.cpp 63
... 390 lines snipped...
/Archive2/8b/f94c53ac4aee45/main.cpp 93
/Archive2/8b/f9ab4b38599e1a/main.cpp 80
/Archive2/8b/fabac3a5309ea7/main.cpp 72
/Archive2/8b/fb63a91a5b2b29/main.cpp 33
/Archive2/8b/fba5d4cf806d91/main.cpp 95
/Archive2/8b/fc2fbcfddff198/main.cpp 42
/Archive2/8b/fc4bbeb11aa3e9/main.cpp 78
/Archive2/8b/fc56c06f6ed2a1/main.cpp 37
/Archive2/8b/fd5770ca49eb30/main.cpp 78
/Archive2/8b/fe0b1121edfb0a/main.cpp 59
/Archive2/8b/ff0f709404e6a1/main.cpp 84
Overall longest line: 1353

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM