hi friends i have created a code which searches for the particular word from two different files and creates a new file with the records in it but i am not able to search digits from the string the code of mine goes here:

StreamReader sr = new StreamReader(@"C:\Documents and Settings\jitendra\Desktop\RIC_SELL_080318.csv");  StreamReader sr1 = new StreamReader(@"C:\Documents and Settings\jitendra\Desktop\RIC_customer_20080318.csv");

 StreamWriter sw = new StreamWriter(@"C:\strings.txt");

 string strline = "";   string strline1 = "";            
string[] values = null; string[] _values = null;

  while (!sr.EndOfStream && !sr1.EndOfStream)
            {
                strline = sr.ReadLine();
                strline1 = sr1.ReadLine();

                values = strline.Split(',');
                _values = strline1.Split(',');

                if ((values.Length > 3 && values[2].StartsWith("52")) && (_values.Length > 3 && _values[2].StartsWith("52")))
                {
                    sw.WriteLine(values[0].ToString() +"\t\t"+ strline1);
                }

on increasing the string digit's of values[2].StartsWith("52") and _values[2].StartsWith("52") the file is created but it does not contain any record it is empty so can you plz help......

Dani AI

Generated

A couple of likely causes behind a zero-length result were touched on earlier by and , but the practical fix is to stop comparing the two CSVs line-for-line and instead normalize a key column from one file into a lookup, then scan the other file and test membership. Line-synchronized reads only work if rows are guaranteed to be in the same order and count. Differences in quoting, whitespace, BOM/encoding, or extra characters will also make StartsWith fail when you actually need an exact match.

A simple, robust pattern:

  • Read the smaller CSV into a HashSet or Dictionary keyed by the column you want to match (trim quotes/space).
  • Iterate the second CSV, normalize its key the same way, then check Contains (or lookup full record).
  • Use using blocks so readers/writers are closed automatically.

Example (conceptual) showing the lookup approach:

// build key set from file B
var keys = new HashSet<string>(StringComparer.Ordinal);
using (var rB = new StreamReader("fileB.csv"))
{
  string line;
  while ((line = rB.ReadLine()) != null)
  {
    var cols = line.Split(',');
    if (cols.Length > 2) keys.Add(cols[2].Trim().Trim('"'));
  }
}

// scan file A and write matches
using (var rA = new StreamReader("fileA.csv"))
using (var w = new StreamWriter("out.txt"))
{
  string line;
  while ((line = rA.ReadLine()) != null)
  {
    var cols = line.Split(',');
    if (cols.Length > 2 && keys.Contains(cols[2].Trim().Trim('"')))
      w.WriteLine(line + "\t" + /* optional: matching line from B */);
  }
}

Troubleshooting tips: print or log a few sample tokens to confirm their exact content, watch for quoted fields and extra whitespace, and prefer equality (Equals) for full-digit matches rather than StartsWith unless a prefix match is intended. If fields may contain commas or quotes, use a CSV parser (for example, TextFieldParser or CsvHelper) instead of Split(',') (TextFieldParser docs, CsvHelper homepage).

Recommended Answers

All 5 Replies

Make sure that you have atleast 4 tokens on the target line in each file, since you are checking to see if there are MORE than 3 in your code.

Next, make sure you Close() all the files when you are done. Closing it is what causes it to write to the disk. I suggest that you also Flush() the output file prior to closing it.

I copied/pasted your code into a workbench, created some text files with 4 tokens, and it worked fine as long as there were 4 tokens, and I flushed and closed the output file, otherwise..... you get a zero byte output file.

// Jerry

Hi guys...
I'm new in c# and i have some problems running this code.
What library do we have to include for StreamReader to work?

I hope you undertsood my question...

using System.IO;

hi Jerry shaw thanks for the help my code worked but there is one problem that on searching that complete digits from one file i have to match that digit from another file and on matching the code i have to write the record in another file. see the code which i have written

StreamReader sr = new StreamReader(@"C:\Documents and Settings\jitendra\Desktop\RIC_SELL_080318.csv"); // first file to open
            
            StreamReader sr1 = new StreamReader(@"C:\Documents and Settings\jitendra\Desktop\RIC_customer_20080318.csv"); // second file for opening

            StreamWriter sw = new StreamWriter(@"C:\strings.txt"); // creating a text file
string strline = "";
            string strline1 = "";

            string[] values = null;
            string[] _values = null;

            while (!sr.EndOfStream && !sr1.EndOfStream)
            {
                   strline = sr.ReadLine();
                  strline1 = sr1.ReadLine();

                   values = strline.Split(',');
                  _values = strline1.Split(',');

               if (values.Length > 3 && values[2].StartsWith("521222") && (_values.Length > 3 && _values[2].StartsWith("52")))
                 {
                     sw.WriteLine(strline.ToString() + "\t\t" + strline1.ToString());
                 }
            }
            sw.Flush(); 
            sw.Close();
            sr.Close(); sr1.Close();
        }

i have impleted this code but what happens is that when i want to match the digits of values[2].StartsWith("521222") with _values[2].StartsWith("521222") again i get the file created with no records what is the mistake i am creating i don't know please help me....

Place a debug-bookmark on the line that places strline.ToString() into sw. Run under debug and see if that line is ever executed. I think you will find that it is not.
In order for it to fall into that conditional statement the text line (after split) must have atleast (4) elements, and the text has to match. You can use the deguger to see why the condition fails.

IOW, it is not the streamwriter that has the problem.

// Jerry

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.