I have a text file

No.,A ,B ,C ,D ,E ,F ,G
1 ,10,11,13,15,10,15,14
2 ,12,10,15,14,9 ,19,12
. ,..,..,..,..,..,..,..
. ,..,..,..,..,..,..,..
. ,..,..,..,..,..,..,..
. ,..,..,..,..,..,..,..
. ,..,..,..,..,..,..,..
. ,..,..,..,..,..,..,..
n ,..,..,..,..,..,..,..

I would like to sum just A+B+C+D+E+F+G

Then put them in another text file
No. Sum
1, 88
2, 91
., ..
., ..
., ..
., ..
., ..
n, ..

Dani AI

Generated

The file shown in the first post is a simple CSV with a header ("No.,A,B,...") and one identifier field followed by numeric columns. The objective is to produce a new file with "No.,Sum" where Sum is the total of all numeric fields after the first column for each row. 's question about "splitting each line and summing the second number with the next" is best interpreted as summing all fields after the first for each row; was correct to point to a delimiter-based approach, but a few small robustness improvements are helpful (streaming read, safe parsing, header skip, trimming).

A compact, practical C# example that reads line-by-line, skips the header, parses integers safely and writes "No.,Sum":

using System;
using System.IO;
using System.Globalization;

class SumCsv
{
    static void Main()
    {
        var inputPath = "input.csv";
        var outputPath = "sums.csv";
        using var writer = new StreamWriter(outputPath);
        writer.WriteLine("No.,Sum");
        foreach (var line in File.ReadLines(inputPath))
        {
            if (string.IsNullOrWhiteSpace(line)) continue;
            var parts = line.Split(',');
            if (parts.Length < 2) continue;
            var id = parts[0].Trim();
            if (id.Equals("No.", StringComparison.OrdinalIgnoreCase)) continue; // header
            long sum = 0;
            for (int i = 1; i < parts.Length; i++)
            {
                var token = parts[i].Trim().Trim('"');
                if (int.TryParse(token, NumberStyles.Integer, CultureInfo.InvariantCulture, out int v))
                    sum += v;
            }
            writer.WriteLine($"{id},{sum}");
        }
    }
}

Notes and troubleshooting:

  • The simple Split(',') works for neatly formatted CSVs like the sample. If fields may be quoted and contain commas, use a proper CSV parser (for example TextFieldParser or a CSV library).
  • TryParse prevents exceptions on bad data; non-numeric tokens are ignored (treated as zero) in this sample—adjust behavior if validation is required.
  • Use long (or decimal) when sums may overflow int or when values have fractions.
  • For very large files, streaming via File.ReadLines and StreamWriter keeps memory use low. Adjust input/output paths and header-detection logic as needed.

Recommended Answers

All 3 Replies

Hi

What code have you got so far? Show us that and we can help you to break the problem down.

To get you started (if you haven't already), look at using the StreamReader to read your text file. Then the String.Split method to read each number for a line. Do the maths, then use the StreamWriter to write the data back to another file.

Hi
My problem is how split words each line and sum secound number with next.
Then, write just first word with sum.

I am a beginner in C#.
Thanks for help me in advance!

Well, your example shows that each number is separated by a comma, so this is what you would use for the split method. This will give you an array which you can loop around. The first element of that array will be ignored when adding each value together but used when writing to your new file.

Have a go and post your code with info on where it is not working and we can go from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.