1

I have data which I need to break and write into a .CSV file so it is a dummy code it is not writing full data in the file I contain thousand data and suppose if I take max 400 rows in a file it writes till 280 then start writing in another file from 401 Also I tried with 200 so it writes till 170 and started writing another file from 201 so my data is missing and my last file is coming blank

This is my code - please help:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int totalrows = 1000;   //Total data to write
            int maxrows = int.Parse(Console.ReadLine());    //Maximum rows to write in a file
            int maxfile = 0;    //Maximum file needed
            int selectedrow=0;  //Select row till which we need to write in single file
            int executedrow = 0;    //Record till that its already executed
            if(maxrows!=0)      //Calculate Maximum files needed to write complete data
                maxfile = totalrows / maxrows;
            string data = "";
            //Folder to store file
            string folder = @"C:\Users\Himanshu Goyal\Documents\WriteInFile\";
            string filename, filepath;
            //Write in File Object
            for (int i = 0; i <= maxfile; i++)
            {
                selectedrow += maxrows;
                if (selectedrow > totalrows)
                    selectedrow = totalrows;
                //Create csv file
                filename = DateTime.UtcNow.ToString("ddMMyyyyhhmm") +i+"of"+maxfile+".csv";
                //Full File Path
                filepath = folder + filename;
                StreamWriter writer = new StreamWriter(filepath);
                writer.WriteLine("Numbers" + "\n");
                for (int j = executedrow; j <= selectedrow; j++)
                {
                    data += j + "\n";
//                    Console.WriteLine(executedrow);
                    executedrow++;
                }
                writer.WriteLine(data);
                Console.WriteLine(data);
                data = "";
            }
        }
    }
}
1
  • 2
    I would suggest you use github.com/JoshClose/CsvHelper NuGet package they have handled all standard error handling and it will be really easy to integrate into your application. Commented Jul 24, 2020 at 16:45

1 Answer 1

1

Nothing special but this work for me!

 using (StreamWriter sw = new StreamWriter(new FileStream(filepath, FileMode.Create, FileAccess.Write)))
                try
                {
                    //Count rows in csv file and add new row number
                    for (int j = executedrow; j <= selectedrow; j++)
                    {
                        data += j + "\n";
                        ////                    console.writeline(executedrow);
                        executedrow++;
                        
                    }
                    sw.WriteLine(data);
                    sw.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.GetBaseException().ToString());
                }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you helped me Just want to know what was the issue.
The StreamWriter class has its own buffer, if you don't dispose/close it, the rest of the buffer will be lost. Here is the thread that explain this. stackoverflow.com/questions/32068969/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.