1

So I am writing a C# console application. I have a text file that I want to send to a database. The plan is to have multiple text files and only one insert. Everything seems to go fine for the first line. Once I get to the 2nd line the array thinks it only has a length of 2.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UKImporter
{
class Program
{
    static void Main(string[] args)
    {
        string[] lines = System.IO.File.ReadAllLines(@"C:\out\output.txt");
        System.Console.WriteLine("Contents of writeLines2.txt =:");

        foreach (string line in lines)
        {
            string sellername, sku, date1, quantity1, date2, asin, date3, date4, FNSKU;
            char[] tabs = { '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',  };
            string[] words = line.Split(tabs);
            sellername = words[0];
            sku = words[1];
            date1 = words[2];
            quantity1 = words[3];
            date2 = words[4];
            asin = words[5];
            date3 = words[6];
            date4 = words[7];
            FNSKU = words[8];
            Console.WriteLine("\t" + line);
            UKDataBLL u = new UKDataBLL();
            //u.AddToDatabase(sku, DateTime.Now, Convert.ToInt16(quantity1), DateTime.Now, asin, DateTime.Now, DateTime.Now, FNSKU);

            foreach (string s in words)
            {
                System.Console.WriteLine(s);
            }

        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();

    }
}
}

Edit Here is some text of the file

A2LQ9QFN82X636 ACD_fivecrowns 6/1/11 5:30 0 6/1/11 5:30 B00000IV35 6/1/11 5:30 6/1/11 5:30 X0000PGTT9

A2LQ9QFN82X63 ACD_caylus_magna_carta 6/1/11 5:30 0 6/1/11 5:30 B000N3SOUM 6/1/11 5:30 6/1/11 5:30 X0000PGM23

A2LQ9QFN82X63 AMX_JrSpaceHelmet-LBL 6/1/11 5:30 0 6/1/11 5:30 B0008F6WMM 6/1/11 5:30 6/1/11 5:30 X0000PQBUL

12
  • post the content of that file Commented Jun 2, 2011 at 17:29
  • Silly question, but you know the second line has tabs, yes? Opening the file in binary mode will verify this (should see 11 / 0B) listed on that row Commented Jun 2, 2011 at 17:29
  • You are referring to the lines array? Commented Jun 2, 2011 at 17:30
  • No need to post the whole file, but a few lines (copy pasted) would help to investigate/reproduce the problem. Commented Jun 2, 2011 at 17:35
  • 1
    @renuiz why would my comment be silly? The code expects a tab delimited input file. The code for line 2 is behaving as if there are only 2 tabs. Either the logic was incorrect or the data is incorrect and given that it worked for the initial line, it lead me to believe the data was in error. Answer I supplied below verifies the processing logic is sound (although it could be improved as others have commented) Commented Jun 2, 2011 at 18:06

4 Answers 4

3

You only need

 string[] words = line.Split('\t');

And then you have to verify that the contents and your operation match, a crude idea:

 System.Diagnostics.Trace.Assert (words.Count == 9);
Sign up to request clarification or add additional context in comments.

3 Comments

I believe you mean words.Length ==9, which I tried and it giving an error on the second time through.
And what is your debugger telling you?
I am a little confuse what you are refering to. I know I have an Output from my debugger.
2

Using the above code, I created a simple output file step that met your intended structure and your code does correctly parse the file out. Your issue appears to be based on the data

    string[] content = new string[] { "a\tb\tc\td\te\tf\tg\th\ti", "a\tb\tc\td\te\tf\tg\th\ti" };
    System.IO.File.WriteAllLines(@"C:\sandbox\output.txt", content);

2 Comments

I am confused @billinkc why would I try and output what I am reading in?
I was simply providing code that generates the expected file format. Running that verifies your main logic is correct. Instead it is the data in the file that does not meet the template format.
1

The tabs parameter to the split only needs to have one tab character. You are telling it all of the possible values to split on. As written, you are telling it to split on a tab or a tab or and so on.

It is possible that the second line is not in the correct format.

Can you send a copy of a couple of lines from the import file?

Comments

1

I dont really see why you need to specify all those tab characters in the split method.

Just do something like this:

foreach (string line in lines)
{
    string[] columns= line.Split('\t');
    if (columns.Length != 9) // Or how many colums the file should have.
        continue; // Line probably not valid

    // Now access all the columns of the line by using 
    // columns[0], columns[1], etc
}

Comments

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.