Quest o()xx[{:::::::::::::::>
Quest o()xx[{:::::::::::::::>
Explore posts from servers
CC#
Created by Quest o()xx[{:::::::::::::::> on 1/27/2025 in #help
Linux wc vs. C# Character Count: BOM Causes Off-by-One Error
Hi everyone, I’m working on building a custom wc tool to mimic the behavior of the wc command from Linux. I’m currently implementing the function that counts characters in a file. However, I noticed that my program’s result is consistently 1 character less than the result from the Linux wc tool for some files. After debugging, I realized that the discrepancy happens when the file contains a BOM (Byte Order Mark) at the start. My current method doesn’t account for the BOM, and I’m wondering how I should handle this if I want my tool to accurately mimic Linux’s wc. Here’s the code I’m using for counting characters:
private static void CountCharInFile(string filePath)
{
var lines = File.ReadLines(filePath);
var numChar = lines.Sum(line => line.Length + Environment.NewLine.Length);
Console.WriteLine($"{numChar} {Path.GetFileName(filePath)}");
}
private static void CountCharInFile(string filePath)
{
var lines = File.ReadLines(filePath);
var numChar = lines.Sum(line => line.Length + Environment.NewLine.Length);
Console.WriteLine($"{numChar} {Path.GetFileName(filePath)}");
}
9 replies
KPCKevin Powell - Community
Created by Quest o()xx[{:::::::::::::::> on 12/13/2023 in #front-end
Need help with text alignment on different browsers
No description
4 replies
KPCKevin Powell - Community
Created by Quest o()xx[{:::::::::::::::> on 11/9/2023 in #front-end
How to make <div> fit image width
No description
4 replies