#Is there an easy way to get the amount of lines a txt / CSV contains?

51 messages · Page 1 of 1 (latest)

wintry timber
#

I have several different files that have to be switched to and from (I can do that myself), but is there an (easy) method of obtaining that number? Or do I need to come up with a clever solution myself?

inland pewterBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

molten condor
#

The only way to know the number of lines in a file is to read the contents and count the number of new-line chars you see

#

You could do that up-front and store the info in a map for example, but you still have to actually count the lines

tiny fog
#
[[nodiscard]] auto countLines(const std::filesyste::path& filepath) -> std::size_t
{
    if (!std::filesystem::exists(filepath))
    {
        std::error_code ec{};
        throw std::filesystem::filesystem_error("File does not exist.", ec);
    }
    if (!std::filesystem::is_regular(filepath))
    {
        std::error_code ec{};
        throw std::filesystem::filesystem_error("Is not a regular file.", ec);
    }
    std::ifstream file(filepath);
    if (!file.is_open())
    {
        throw std::ios_base::failure("Failed to open the file: " + filename);
    }

    std::size_t line_count = 0;
    std::string line;

    // Read line by line
    while (std::getline(file, line))
    {
        ++line_count;
    }

    return line_count;
}
molten condor
#

why use getline there?

next linden
#

for whole line

tiny fog
#

why not?

#

you could

    char ch;
    while (file.get(ch)) {  // Read one character at a time
        if (ch == '\n') {
            ++line_count;
        }
    }
molten condor
#

you could just an iterator and just read from the internal buffer without allocating anything

#

then you can even pass it to one of the standard algos and skip writing the loop, std::count_if and std::istreambuf_iterator can work magic

tiny fog
#
    std::istreambuf_iterator<char> it(file), end;
    for (; it != end; ++it) {
        if (*it == '\n') {
            ++line_count;
        }
    }
#

huh... yeah I guess

molten condor
#

and that loop is just std::count_if

#

so you can make it a 1-liner

sharp nest
molten condor
#

the srteams do their own buffering, so it's not as slow as you may think

next linden
tiny fog
#
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <iterator>

[[nodiscard]]
auto countLines(const std::filesystem::path& filepath) -> std::size_t
{
    if (!std::filesystem::exists(filepath))
    {
        throw std::filesystem::filesystem_error(
          "File does not exist.", std::make_error_code(std::errc::no_such_file_or_directory)
        );
    }
    if (!std::filesystem::is_regular_file(filepath))
    {
        throw std::filesystem::filesystem_error("Is not a regular file.", std::make_error_code(std::errc::is_a_directory));
    }

    std::ifstream file(filepath);
    if (!file.is_open())
    {
        throw std::ios_base::failure("Failed to open the file: " + filepath.string());
    }

    std::istreambuf_iterator<char> begin(file);
    std::istreambuf_iterator<char> end;
    return std::count_if(begin, end, [](const char CHR) { return CHR == '\n'; });
}
sharp nest
molten condor
next linden
#

make sence

molten condor
sharp nest
molten condor
#

you can still have a buffered stream on embedded, std::cout even works!

#

if you're using some raw api to get the data, well then you still have to supply the buffer to copy in from the FS

sharp nest
#

👍

tiny fog
sharp nest
#

fair point

wintry timber
#

Could you guys make it so that it is a function , seperate from main that I could just copy paste into my own thing?

#

Thanks!

inland pewterBOT
#

@wintry timber Has your question been resolved? If so, type !solved :)

wintry timber
#

No it hasn't, but thank you for asking

wintry timber
#

Anyone?

tiny fog
#

also Wheatly is a bot.

#

Do you require an explanation of the function?

wintry timber
#

!solved

inland pewterBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

normal snow
#

The opening will fail anyway if those are false. If anything, you could do this analysis after is_open() returned false

wintry timber
#

Guys nevermind i figured it out

#

thank you for the help

sharp nest
#

!vampire

inland pewterBOT
# sharp nest !vampire
Help Vampires :vampire:

"Help Vampires" are people who will continually ask for help on a problem in the hope that someone will do their work for them while putting little to no effort in themselves.
We try to be helpful and a friendly community, however, please be mindful that folks on this discord are volunteering their time to help others learn. It's a waste of their time to be asked to teach you stuff you easily could have figured out yourself.

Symptoms of a Help Vampire :bat:
  • Inability to use google effectively
  • Inability to read any documentation
  • Inability to "try it and see"
  • Inability to provide a minimal example of the problem and instead posting walls of text, screenshots, and pastebin links.
  • Inability to provide relevant information, expecting helpers to have the same context they do
  • Expects answers to be handed to them
  • No apparent increase in comprehension after questions are answered
  • Targets a previous answerer hours or days later, often on unrelated matters
Examples :garlic:
  • "Does this work?"
  • "Can someone link me to XYZ?"
  • "How the SKIBIDI do I implement a segmentation tree"
formal peak
# inland pewter

could add "asks questions just for the sake of asking something so people engage with them" because there are a lot of people(not op) who do this

next linden
tiny fog