Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9226311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:47:56+00:00 2026-06-18T04:47:56+00:00

I wrote a small function that reads a csv file using textField line by

  • 0

I wrote a small function that reads a csv file using textField line by line , edit it a specific field then write it back to a CSV file.

Here is the code :

private void button2_Click(object sender, EventArgs e)
    {
        String path = @"C:\file.csv";
        String dpath = @"C:\file_processed.csv";
        List<String> lines = new List<String>();

        if (File.Exists(path))
        {
            using (TextFieldParser parser = new TextFieldParser(path))
            {
                String line;

                parser.HasFieldsEnclosedInQuotes = true;
                parser.Delimiters = new string[] { "," };



                while ((line = parser.ReadLine()) != null)
                {
                      string[] parts = parser.ReadFields();

                     if (parts == null)
                     {
                         break;
                     }                   

                          if ((parts[12] != "") && (parts[12] != "0"))
                          {

                              parts[12] = parts[12].Substring(0, 3);
                              //MessageBox.Show(parts[12]);
                          }

                    lines.Add(line);
                }
            }
            using (StreamWriter writer = new StreamWriter(dpath, false))
            {
                foreach (String line in lines)
                    writer.WriteLine(line);
            }
            MessageBox.Show("CSV file successfully processed ! ");
        }
    }

The field I want to edit is the 12th one (parts[12]):

for example : if parts[12] = 000,000,234 then change to 000

the file is created the problem is it does not edit the file and half the records are missing. I am hoping someone could point the mistake.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:47:57+00:00Added an answer on June 18, 2026 at 4:47 am

    You call both parser.ReadFields() and parser.ReadLine(). Each of them advance the cursor by one. That’s why you’re missing half the rows. Change the while to:

    while(!parser.EndOfData)
    

    Then add parts = parser.ReadFields(); to the end of the loop. Not having this is why you’re edit isn’t being seen.

    You can also remove:

    if (parts == null)
    {
        break;
    } 
    

    Since you no longer have line, you’ll need to use the fields to keep track of your results:

    lines.Add(string.Join(",", parts));//handle string escaping fields if needed.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a small app that turns out to be using a lot of
I have a small script we're using to read in a CSV file containing
I'm trying to write a small Powershell function that will return some summary data
I wrote a small tcp client using boost::asio, providing the following function: typedef boost::function<void(const
Trying to write a small function that updates a paragraph with an image's alt
I've wrote a small piece of code that reads the current battery charge/discharge on
i want to create functions what get the folder size. I write small function:
I wrote a small internal web app using (a subset of) pylons . As
I wrote some small apps using .NET 3.5 but now I am stuck with
I wrote a small program, that creates files at an interval of 1 minute.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.