Thank you Hanook, for your the help.
But, what i want is: when i open a file, i want read the first, say 300, and the last value, say 400, of the first column. then prepare an array where i put all numbers from 300 to 400. Then, compare each element of the column A with the array element one by one, from the file. If array element is present in the column A, then save it in an another file along with the corresponding value of it from column B. If it is not, then put a null value. And, do it till end of the file.
I have prepared some codes, check where i am going wrong.
please check the code below, that i have prepared.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace missing_values_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog opd = new OpenFileDialog();
opd.InitialDirectory = @"c:\";
opd.Title = "Choose your file for finding max values";
opd.CheckFileExists = false;
opd.CheckPathExists = false;
opd.DefaultExt = "csv";
opd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
opd.FilterIndex = 2;
//string filechose = opd.FileName;
if (opd.ShowDialog() == DialogResult.OK)
{
try
{
var dataline = File.ReadAllLines(opd.FileName);
string[] start = dataline.First().Split(',');
string x = start[0];
int ix = Convert.ToInt32(x);
//label1.Text = x;
string[] end = dataline.Last().Split(',');
string y = end[0];
int iy = Convert.ToInt32(y);
//label1.Text = y;
List<string> list = new List<string>();
for (int runs = ix; runs <= iy; runs++)
{
string srun = runs.ToString();
list.Add(srun);
}
string[] terms = list.ToArray();
//string vterm = string.Join("",terms);
//richTextBox1.Text = vterm.ToString();
List<string> listA = new List<String>();
List<string> listB = new List<String>();
foreach (string line in dataline)
{
while (line != null)
{
string[] myColumns = line.Split(',');
listA.Add(myColumns[0]);
listB.Add(myColumns[1]);
}
//i++;
}
string[] firstlistA = listA.ToArray();
string[] firstlistB = listB.ToArray();
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = @"c:\";
sfd.Title = "Choose file for saving.";
//opd.CheckFileExists = false;
//opd.CheckPathExists = false;
sfd.DefaultExt = "csv";
sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
sfd.FilterIndex = 2;
//string filechose = opd.FileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
var csv = new StringBuilder();
for (int i = 0; i <= (iy - ix); i++)
{
for (int j = 0; j <= firstlistA.Length; j++)
{
for (int k = 0; k <= firstlistB.Length; k++)
{
if (terms[i] == firstlistA[j])
{
foreach(char a in firstlistA[j])
{
while (firstlistB[k] != null)
{
string first = terms[i];
string second = firstlistB[k];
var newLine = string.Format("{0},{1}{2}", first, second, Environment.NewLine);
csv.Append(newLine);
}
}
}
else
{
string first = terms[i];
string second = null;
var newLine = string.Format("{0},{1}{2}", first, second, Environment.NewLine);
csv.Append(newLine);
}
}
}
}
sfd.FileName = csv.ToString();
//File.AppendAllText(@"c:\output.csv", csv.ToString());
}
}
catch (Exception g)
{
}
}
}
}
}