#Is there an easy way to get the amount of lines a txt / CSV contains?
51 messages · Page 1 of 1 (latest)
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.
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
[[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;
}
why use getline there?
for whole line
why not?
you could
char ch;
while (file.get(ch)) { // Read one character at a time
if (ch == '\n') {
++line_count;
}
}
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
std::istreambuf_iterator<char> it(file), end;
for (; it != end; ++it) {
if (*it == '\n') {
++line_count;
}
}
huh... yeah I guess
Isn't that a lot slower than reading an entire line at once or does the OS do a good enough job at caching anyways?
the srteams do their own buffering, so it's not as slow as you may think
like
std::count_if(std::istream_iterator<string>(file), std::istream_iterator<string>(), [](char letter){ return letter == '\n'};
or something like that?
#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'; });
}
well, but only if the stream is actually buffered.
Also if the size of each line is like 100k characters, then it could make a big difference, no?
not quite, I'd use istreambuf_iterator. istream_iterator is the formatted version where as istreambuf_iterator is the "raw" (just plain char) version
make sence
You have to jump through hoops to get a non-buffered stream
or work on an embedded system
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
👍
on an embedded system you will have a hard time working with files.
fair point
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!
@wintry timber Has your question been resolved? If so, type !solved :)
No it hasn't, but thank you for asking
Anyone?
they've already done that?
what do you mean it hasn't been solved?
Is this not what you were asking for?
#1316139359094702154 message
Could you guys make it so that it is a function , seperate from main that I could just copy paste into my own thing?
you ought to be able to do that much yourself. If not, your IDE probably has a way to refactor selected text out into a function.
also Wheatly is a bot.
Do you require an explanation of the function?
!solved
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
You don't need to (and shouldn't) do those checks before opening a file
The opening will fail anyway if those are false. If anything, you could do this analysis after is_open() returned false
!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.
- 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
- "Does this work?"
- "Can someone link me to XYZ?"
- "How the SKIBIDI do I implement a segmentation tree"
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
Where I can look to this bot documentation
try here