Array help Index out of range exception was unhandled - c#

I am trying to populate combo boxes from a text file using comma as a delimiter everything was working fine, but now when I debug I get the "Index out of range exception was unhandled" warning. I guess I need a fresh pair of eyes to see where I went wrong, I commented on the line that gets the error //Fname = fields[1];
private void xViewFacultyMenuItem_Click(object sender, EventArgs e)
{
const string fileStaff = "source\\Staff.txt";
const char DELIM = ',';
string Lname, Fname, Depart, Stat, Sex, Salary, cDept, cStat, cSex;
double Gtotal;
string recordIn;
string[] fields;
cDept = this.xDeptComboBox.SelectedItem.ToString();
cStat = this.xStatusComboBox.SelectedItem.ToString();
cSex = this.xSexComboBox.SelectedItem.ToString();
FileStream inFile = new FileStream(fileStaff, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(inFile);
recordIn = reader.ReadLine();
while (recordIn != null)
{
fields = recordIn.Split(DELIM);
Lname = fields[0];
Fname = fields[1]; // this is where the error appears
Depart = fields[2];
Stat = fields[3];
Sex = fields[4];
Salary = fields[5];
Fname = fields[1].TrimStart(null);
Depart = fields[2].TrimStart(null);
Stat = fields[3].TrimStart(null);
Sex = fields[4].TrimStart(null);
Salary = fields[5].TrimStart(null);
Gtotal = double.Parse(Salary);
if (Depart == cDept && cStat == Stat && cSex == Sex)
{
this.xEmployeeListBox.Items.Add(recordIn);
}
recordIn = reader.ReadLine();
}
Source file --
Anderson, Kristen, Accounting, Assistant, Female, 43155
Ball, Robin, Accounting, Instructor, Female, 42723
Chin, Roger, Accounting, Full, Male,59281
Coats, William, Accounting, Assistant, Male, 45371
Doepke, Cheryl, Accounting, Full, Female, 52105
Downs, Clifton, Accounting, Associate, Male, 46887
Garafano, Karen, Finance, Associate, Female, 49000
Hill, Trevor, Management, Instructor, Male, 38590
Jackson, Carole, Accounting, Instructor, Female, 38781
Jacobson, Andrew, Management, Full, Male, 56281
Lewis, Karl, Management, Associate, Male, 48387
Mack, Kevin, Management, Assistant, Male, 45000
McKaye, Susan, Management, Instructor, Female, 43979
Nelsen, Beth, Finance, Full, Female, 52339
Nelson, Dale, Accounting, Full, Male, 54578
Palermo, Sheryl, Accounting, Associate, Female, 45617
Rais, Mary, Finance, Instructor, Female, 27000
Scheib, Earl, Management, Instructor, Male, 37389
Smith, Tom, Finance, Full, Male, 57167
Smythe, Janice, Management, Associate, Female, 46887
True, David, Accounting, Full, Male, 53181
Young, Jeff, Management, Assistant, Male, 43513

For the sake of anyone who doesn't want to look at the mammoth code you've posted, here's the relevant bit:
while (recordIn != null)
{
fields = recordIn.Split(DELIM);
Lname = fields[0];
Fname = fields[1]; // this is where the error appears
Given the exception you've seen, that basically means that recordIn doesn't contain the delimiter DELIM (a comma). I suggest you explicitly check for the expected size and throw an exception giving more details if you get an inappropriate line. Or if it's a blank line, as others have suggested (and which does indeed seem likely) you may want to just skip it.
Alternatively, here's a short but complete console application which should help you find the problem:
using System;
using System.IO;
class Test
{
static void Main()
{
string[] lines = File.ReadAllLines("source\\Staff.txt");
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
string[] fields = line.Split(',');
if (fields.Length != 6)
{
Console.WriteLine("Invalid line ({0}): '{1}'",
i + 1, line);
}
}
}
}

That could be because of blank line that appear at the top in the text file.

Have you checked for an empty row at the end of your text file?

After this:
fields = recordIn.Split(DELIM);
you need this:
if (fields.length < 6)
{
// the current recordIn is the problem!
}
else
{
Lname = fields[0];
// etc.
}
recordIn = reader.ReadLine(); // make sure to put this after the else block!
You should do this routinely when reading from files, because there are often leading or trailing blank lines.

You've most likely got an extra blank line at the end of your input file, which therefore only has one (empty) field, giving you index out of range at index 1.

Related

C# Regex Split but include empty string if fails to split

I am trying to split a string into an array of strings.
My current string looks like this and this is all in one string. It also has newlines (\r\n) and spaces. I put a better-looking example here.
BFFPPB14 Dark Chocolate Dried Cherries 14 oz (397g)
INGREDIENTS: DARK CHOCOLATE (SUGAR, CHOCOLATE LIQUOR, COCOA BUTTER,
ANHYDROUS MILK FAT, SOYA LECITHIN, VANILLIN [AN ARTIFICIAL FLAVOR]), DRIED
TART CHERRIES (CHERRIES, SUGAR), GUM ARABIC, CONFECTIONER'S GLAZE.
CONTAINS: MILK, SOY
ALLERGEN INFORMATION: MAY CONTAIN TREE NUTS, PEANUTS, EGG AND
WHEAT.
01/11/2019
Description: Sweetened dried Montmorency cherries that are panned with dark chocolate.
Storage Conditions: Store at ambient temperatures with a humidity less than 50%.
Shelf Life: 9 months
Company Name
Item No.: 701804
Bulk: 415265
Supplier: Cherryland's Best
WARNING: CHERRIES MAY CONTAIN PITS
My Regex looks like this
List<string> result = Regex.Split(text, #"INGREDIENTS: |CONTAINS: |ALLERGEN INFORMATION: |(\d{1,2}/\d{1,2}/\d{2,4})|Description: |Storage Conditions: |Shelf Life: |Company Name|Item No.: |Bulk: |Supplier: |WARNING: ").ToList();
This is what result looks like
Note: The first string is the product name
Sometimes I get strings that don't have a supplier or a warning, I want the split to have empty strings if it doesn't find that split value.
EX:
result[0] = "blabla"
result[1] = ""
result[2] = "blabla"
That way I know that result 1 was split on the value (INGREDIENTS: ) and I can assign it to something
Using a regex may have performance concerns if you are using this in a high volume application. Below is one possible regex you could use. It is somewhat difficult to parse the product line and the "company name" line since it wasn't clear if the product code had a pattern and the company name line didn't have a ':' like the other fields, so the regex is somewhat "hacky" in those areas:
using System;
using System.Text.RegularExpressions;
using System.Linq;
namespace so20190113_01 {
class Program {
static void Main(string[] args) {
string text =
#"BFFPPB14 Dark Chocolate Dried Cherries 14 oz (397g)
INGREDIENTS: DARK CHOCOLATE (SUGAR, CHOCOLATE LIQUOR, COCOA BUTTER, ANHYDROUS MILK FAT, SOYA LECITHIN, VANILLIN [AN ARTIFICIAL FLAVOR]), DRIED TART CHERRIES (CHERRIES, SUGAR), GUM ARABIC, CONFECTIONER'S GLAZE.
CONTAINS: MILK, SOY
ALLERGEN INFORMATION: MAY CONTAIN TREE NUTS, PEANUTS, EGG AND WHEAT.
01/11/2019
Description: Sweetened dried Montmorency cherries that are panned with dark chocolate.
Storage Conditions: Store at ambient temperatures with a humidity less than 50%. Shelf Life: 9 months
Company Name
Item No.: 701804
Bulk: 415265
Supplier: Cherryland's Best
WARNING: CHERRIES MAY CONTAIN PITS";
string pat =
#"^\s*(?<product>\w+\s+\w+\s+\w*[^:]+)$
|^ingredients:\s*(?<ingredients>.*)$
|^contains:\s*(?<contains>.*)$
|^allergen\s+information:\s*(?<allergen>.*)$
|^(?<date>(\d{1,2}/\d{1,2}/\d{2,4}))$
|^description:\s*(?<description>.*)$
|^storage\sconditions:\s*(?<storage>.*)$
|^shelf\slife:\s*(?<shelf>.*)$
|^company\sname\s*(?<company>.*)$
|^item\sno\.:\s*(?<item>.*)$
|^bulk:\s*(?<bulk>.*)$
|^supplier:\s*(?<supplier>.*)$
|^warning:\s*(?<warning>.*)$
";
Regex r = new Regex(pat, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
// Match the regular expression pattern against a text string.
Match m = r.Match(text); // you might want to use the overload that supports a timeout value
Console.WriteLine("Start---");
while (m.Success) {
foreach (Group g in m.Groups.Where(x => x.Success)) {
switch (g.Name) {
case "product":
Console.WriteLine($"Product({g.Success}): '{g.Value.Trim()}'");
break;
case "ingredients":
Console.WriteLine($"Ingredients({g.Success}): '{g.Value.Trim()}'");
break;
// etc.
}
}
m = m.NextMatch();
}
Console.WriteLine("End---");
}
}
}
I think a parser is the only way. Originally, I tried using this regex:
^([\w \.]+?):([\s\S]+?)(?=((^[\w \.]+?):))
The key component there is the look-ahead ?= which allows the string to match all text from label to label. However, it doesn't work on the final line item since it does not precede another label and I could not find a regex that stops matching at a pattern that may not exist. If that regex exists, you can do it all in one line of code:
KeyValuePair<string, string>[] kvs = null;
//one line of code if the look-ahead would also consider non-existent matches
kvs = Regex.Matches(text, #"^([\w \.]+?):([\s\S]+?)(?=((^[\w \.]+?):))", RegexOptions.Multiline)
.Cast<Match>()
.Select(x => new KeyValuePair<string, string>(x.Groups[1].Value, x.Groups[2].Value.Trim(' ', '\r', '\n', '\t')))
.ToArray();
This code does it well enough. Also, the document is not formatted consistently in that Company Name does not precede a colon. This is the only anchor pattern that will work since various lines are broken by new lines.
KeyValuePair<string, string>[] kvs = null;
//Otherwise, you have to write a parser
//get all start indexes of labels
var matches = Regex.Matches(text, #"^.+?:", RegexOptions.Multiline).Cast<Match>().ToArray();
kvs = new KeyValuePair<string, string>[matches.Length];
KeyValuePair<string, string> GetKeyValuePair(Match match1, int match1EndIndex)
{
//get the label
var label = text.Substring(match1.Index, match1.Value.Length - 1);
//get the desc and trim white space
var descStart = match1.Index + match1.Value.Length + 1;
var desc = text
.Substring(descStart, match1EndIndex - descStart)
.Trim(' ', '\r', '\n', '\t');
return new KeyValuePair<string, string>(label, desc);
}
for (int i = 0; i < matches.Length - 1; i++)
{
kvs[i] = GetKeyValuePair(matches[i], matches[i + 1].Index);
}
kvs[kvs.Length - 1] = GetKeyValuePair(matches[matches.Length - 1], text.Length);
foreach (var kv in kvs)
{
Console.WriteLine($"{kv.Key}: {kv.Value}");
}
So if your requirement is :
find a line with starting with with specif word
use Linq
use StartsWith
code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApp12
{
class Program
{
public static void Main(string[] args)
{
// test string
var str = #"BFFPPB10 Dark Chocolate Macadamia Nuts 11 oz (312g)\r\nINGREDIENTS: DARK CHOCOLATE (SUGAR, CHOCOLATE, COCOA BUTTER, \r\nANHYDROUS MILK FAT, SOY LECITHIN, VANILLA), MACADAMIA NUTS, SEA SALT.\r\nCONTAINS: MACADAMIA NUTS, MILK, SOY.\r\nALLERGEN INFORMATION: MAY CONTAIN OTHER TREE NUTS, PEANUTS, EGG AND\r\nWHEAT.\r\n01/11/2019\r\nDescription: Dry roasted, salted macadamias covered in dark chocolate.\r\nStorage Conditions: Store at ambient temperatures with a humidity less than 50%. \r\nShelf Life: 12 months\r\nBlain's Farm & Fleet\r\nItem No.: 701772\r\nBulk: 421172\r\nSupplier: Devon's\r\n";
// Keys
const string KEY_INGREDIENTS = "INGREDIENTS:";
const string KEY_CONTAINS = "CONTAINS:";
const string KEY_ALLERGEN_INFORMATION = "ALLERGEN INFORMATION:";
const string KEY_DESCRPTION = "Description:";
const string KEY_STORAGE_CONDITION = "Storage Conditions:";
const string KEY_SHELFLIFE = "Shelf Life:";
const string KEY_ITEM_NO = "Item No.:";
const string KEY_BULK = "Bulk:";
const string KEY_SUPPLIER = "Supplier:";
const string KEY_WARNING = "WARNING:";
const string KEY_YEAR_Regex = #"^\d{1,2}/\d{1,2}/\d{4}$";
const string KEY_AFTER_COMPANY_NAME = KEY_ITEM_NO;
// Helpers
var keys = new string[]
{ KEY_INGREDIENTS, KEY_CONTAINS, KEY_ALLERGEN_INFORMATION, KEY_DESCRPTION, KEY_STORAGE_CONDITION,
KEY_SHELFLIFE, KEY_ITEM_NO, KEY_BULK, KEY_SUPPLIER, KEY_WARNING };
var lines = str.Split(new string[] { #"\r\n" }, StringSplitOptions.RemoveEmptyEntries);
void log(string key, string val)
{
Console.WriteLine($"{key} => {val}");
Console.WriteLine();
}
void removeLine(string line)
{
if (line != null) lines = lines.Where(w => w != line).ToArray();
}
// get Multi Line Item with key
string getMultiLine(string key)
{
var line = lines
.Select((linetxt, index) => new { linetxt, index })
.Where(w => w.linetxt.StartsWith(key))
.FirstOrDefault();
if (line == null) return string.Empty;
var result = line.linetxt;
for (int i = line.index + 1; i < lines.Length; i++)
{
if (!keys.Any(a => lines[i].StartsWith(a)))
result += lines[i];
else
break;
}
return result;
}
// get single Line Item before spesic key if the Line is not a key
string getLinebefore(string the_after_key)
{
var the_after_line = lines
.Select((linetxt, index) => new { linetxt, index })
.Where(w => w.linetxt.StartsWith(the_after_key))
.FirstOrDefault();
if (the_after_line == null) return string.Empty;
var the_before_line_text = lines[the_after_line.index - 1];
//not a key
if (!keys.Any(a => the_before_line_text.StartsWith(a)))
return the_before_line_text;
else
return null;
}
// 1st get item without key
var itemName = lines.FirstOrDefault();
removeLine(itemName);
var year = lines.Where(w => Regex.Match(w, KEY_YEAR_Regex).Success).FirstOrDefault();
removeLine(year);
var companyName = getLinebefore(KEY_AFTER_COMPANY_NAME);
removeLine(companyName);
//2nd get item with Keys
var ingredients = getMultiLine(KEY_INGREDIENTS);
var contanins = getMultiLine(KEY_CONTAINS);
var allergenInfromation = getMultiLine(KEY_ALLERGEN_INFORMATION);
var description = getMultiLine(KEY_DESCRPTION);
var storageConditions = getMultiLine(KEY_STORAGE_CONDITION);
var shelfLife = getMultiLine(KEY_SHELFLIFE);
var itemNo = getMultiLine(KEY_ITEM_NO);
var bulk = getMultiLine(KEY_BULK);
var supplier = getMultiLine(KEY_SUPPLIER);
var warning = getMultiLine(KEY_WARNING);
// 3rd log
log("ItemName", itemName);
log("Ingredients", ingredients);
log("contanins", contanins);
log("Allergen Infromation", allergenInfromation);
log("Year", year);
log("Description", description);
log("Storage Conditions", storageConditions);
log("Shelf Life", shelfLife);
log("CompanyName", companyName);
log("Item No", itemNo);
log("Bulk", bulk);
log("Supplier", supplier);
log("warning", warning);
Console.ReadLine();
}
}
}
will output
ItemName => BFFPPB10 Dark Chocolate Macadamia Nuts 11 oz (312g)
Ingredients => INGREDIENTS: DARK CHOCOLATE (SUGAR, CHOCOLATE, COCOA
BUTTER, ANHYDROUS MILK FAT, SOY LECITHIN, VANILLA), MACADAMIA NUTS,
SEA SALT.
contanins => CONTAINS: MACADAMIA NUTS, MILK, SOY.
Allergen Infromation => ALLERGEN INFORMATION: MAY CONTAIN OTHER TREE
NUTS, PEANUTS, EGG ANDWHEAT.
Year => 01/11/2019
Description => Description: Dry roasted, salted macadamias covered in
dark chocolate.
Storage Conditions => Storage Conditions: Store at ambient
temperatures with a humidity less than 50%.
Shelf Life => Shelf Life: 12 months
CompanyName => Blain's Farm & Fleet
Item No => Item No.: 701772
Bulk => Bulk: 421172
Supplier => Supplier: Devon's
warning =>

How to remove pieces of data from string

I have a text file with multiple entries of this format:
Page: 1 of 1
Report Date: January 15 2018
Mr. Gerald M. Abridge ID #: 0000008 1 Route 81 Mr. Gerald Michael Abridge Pittaburgh PA 15668 SSN: XXX-XX-XXXX
Birthdate: 01/00/1998 Sex: M
COURSE Course Title CRD GRD GRDPT COURSE Course Title CRD GRD GRDPT
FALL 2017 (08/28/2017 to 12/14/2017) CS102F FUND. OF IT & COMPUTING 4.00 A 16.00 CS110 C++ PROGRAMMING I 3.00 A- 11.10 EL102 LANGUAGE AND RHETORIC 3.00 B+ 9.90 MA109 CALC WITH APPLICATIONS I 4.00 A 16.00 SP203 INTERMEDIATE SPANISH I 3.00 A 12.00
EHRS QHRS QPTS GPA Term 17.00 17.00 65.00 3.824 Cum 17.00 17.00 65.00 3.824
Current Program(s): Bachelor of Science in Computer Science
End of official record.
So far, I have read the text file into a string, full. I want to be able to remove first two lines of each of the entries. How would I go about doing this?
Here's the code that I used to read it in:
using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
{
string full = sr.ReadToEnd();
}
If all the lines you want to skip begin with the same strings, you can put those prefixes in a list and then, when you're reading the lines, skip the any that being with one of the prefixes:
This will leave you with a list of strings that represent all the file lines that don't begin with one of the specified prefixes:
var filePath = #"f:\public\temp\temp.txt";
var ignorePrefixes = new List<string> {"Page:", "Report Date:"};
var filteredContent = File.ReadAllLines(filePath)
.Where(line => ignorePrefixes.All(prefix => !line.StartsWith(prefix)))
.ToList();
If you want all the content as a single string, you can use String.Join:
var filteredAsString = string.Join(Environment.NewLine, filteredContent);
If Linq isn't your thing, or you don't understand what it's doing, here's the "old school" way of doing the same thing:
List<string> filtered = new List<string>();
foreach (string line in File.ReadLines(filePath))
{
bool okToAdd = true;
foreach (string prefix in ignorePrefixes)
{
if (line.StartsWith(prefix))
{
okToAdd = false;
break;
}
}
if (okToAdd)
{
filtered.Add(line);
}
}
public static IEnumerable<string> ReadReportFile(FileInfo file)
{
var line = String.Empty;
var page = "Page:";
var date = "Report Date:";
using(var reader = File.OpenText(file.FullName))
while((line = reader.ReadLine()) != null)
while(line.IndexOf(page) == -1 AND line.IndexOf(date) == -1)
yield return line;
}
Code is pretty straight forward, while line is not null and doesn't contain page or date, return line. You could condense or even get fancier, building lookups for your prefix etc. but if the code is simple or not needed to be that complex, this should suffice.

C# if statement skipped even if condition is met

im writing a basic program on c# for school and im having trouble with an if statement where the condition gets met but the code gets skipped as if the condition wasn't met.
//this runs when i select a cell on the dataGridView
private void dataGridView1_CellClick(object sender,DataGridViewCellEventArgs e)
{
string estado = "";
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
id_lbl.Text = row.Cells[0].Value.ToString();
nombre_lbl.Text = row.Cells[1].Value.ToString();
apellido_lbl.Text = row.Cells[2].Value.ToString();
estado = row.Cells[7].Value.ToString();
}
id_lbl.Visible = true;
nombre_lbl.Visible = true;
apellido_lbl.Visible = true;
if(estado == "Activo")
{
baja_btn.Enabled = true;
}
else if (estado == "NoActivo")
{
alta_btn.Enabled = true;
}
else
{
MessageBox.Show(estado);
}
}
code runs but if statement jumps directly to else code and message box displays Activo, how ever, baja_btn.Enabled = true; does not run.
same is true if i select row with NoActivo. if jumps straight to else...
Note: the ACTUAL value of estado just before entering the if statement is Activo. so as it is it SHOULD go into the first condition but it skips all the way to else..
Two strings are not equal. To see where and why, let's try debug report (just before if):
...
String report = String.Format(
"Tested [{0}] encoded {1} of length {2}\r\nActual [{3}] encoded {4} of length {5}",
estado,
String.Join(" ", estado.Select(c => ((int) c).ToString("x4"))),
estado.Length,
"Activo", // <- copy/paste all "Activo" from the if
String.Join(" ", "Activo".Select(c => ((int) c).ToString("x4"))),
"Activo".Length);
MessageBox.Show(report);
if(estado == "Activo") // <- if of the question
...
Please, have a look at the report: where do you have differences? I've got two equal strings
Tested [Activo] encoded 0041 0063 0074 0069 0076 006f of length 6
Actual [Activo] encoded 0041 0063 0074 0069 0076 006f of length 6
but in your case there should be differences
EDIT: the experiment has shown that the tested value is
Tested [Activo ] 0041 0063 0074 0069 0076 006f 0020 0020 0020 0020 of lenght 10
So you have trailing spaces, it´s typical for many RDBMS: you have, say, CHAR(10) field in the table, and so you're given 10 character string. To solve the problem you can just trim the trailing spaces:
if (estado.TrimEnd() == "Activo")
{
baja_btn.Enabled = true;
}

Insert a running number in textfile in c#

I would like to add a running number inside a textfile but it must be in specific column. etc: line 1, column 15.
original file : it has data name,profession,birthdate,status
john teacher 1988 married
marcel engineer1976 single
emi professo1975 married
convert file:insert running number between birthdate and status
john teacher 1988 D001married
marcel engineer1976 D002single
emi professo1975 D003married
note : every data start at their own specific column. name start at column 1, profession start at column 7,birthday start at column 15 and status start at column 20.
how do I do this using c#.
Loop through each row of the text and run something like this on each line:
const int STATUS_START_COLUMN_NUM = 19;
line = line.Substring(0,STATUS_START_COLUMN_NUM - 1) + "D" + String.Format("{0:000}",i) + line.Substring(STATUS_START_COLUMN_NUM);
i++;
This is exactly what String.Insert() is for.
List<string> data = new List<string>
{
"john teacher 1988 married",
"marcel engineer1976 single",
"emi professo1975 married"
};
Console.WriteLine("Before: ");
data.ForEach(Console.WriteLine);
Console.WriteLine();
int runningNumber = 1;
for (int i = 0; i < data.Count; i++)
{
data[i] = data[i].Insert(20, String.Format("D{0:000}", runningNumber));
runningNumber++;
}
Console.WriteLine("After: ");
data.ForEach(Console.WriteLine);
Results:
Before:
john teacher 1988 married
marcel engineer1976 single
emi professo1975 married
After:
john teacher 1988 D001married
marcel engineer1976 D002single
emi professo1975 D003married
Here's a sample solution that makes use of StringBuilder and TextWriter with the use of string.Format() and additional functionality Truncate to get the proper length of string.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplicationStringBuilder
{
class Program
{
static void Main(string[] args)
{
/*
john teacher 1988 married
marcel engineer1976 single
emi professo1975 married
*/
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Format("{0, -7}{1, -8}{2, -5}{3}", Truncate("john", 6), Truncate("teacher", 8), 1988, Truncate("married", 7)));
sb.AppendLine(string.Format("{0, -7}{1, -8}{2, -5}{3}", Truncate("marcel", 6), Truncate("engineer", 8), 1976, Truncate("single", 7)));
sb.AppendLine(string.Format("{0, -7}{1, -8}{2, -5}{3}", Truncate("emi", 6), Truncate("professor", 8), 1975, Truncate("married", 7)));
string s = sb.ToString();
Console.WriteLine(s);
//Write to file
using (TextWriter tw = File.CreateText("C:\\test.txt"))
{
tw.Write(s);
tw.Flush();
tw.Close();
}
Console.WriteLine("File written to C:\\test.txt"); ;
Console.ReadLine();
}
static public string Truncate(string value, int max)
{
return value.Substring(0, Math.Min(value.Length, max));
}
}
}
What happens here are the following:
Truncate each string you passed on Truncate function to get only the string within the given maximum value
Pass each value to string.Format() padding each item with the blank spaces needed
Build all the string via StringBuilder and convert it to a string
Create a text file and assign it TextWriter filling it with the string that was built previously

Processing a text file where the fields are not consistent

A vendor is providing a delimited text file but the file can and likely will be custom for each customer. So if the specification provides 100 fields I may only receive 10 fields.
My concern is the overhead of each loop. In all I am using a while and 2 for loops just for the header and there will at least as many for the detail.
My answer is as follows:
using (StreamReader sr = new StreamReader(flName))
{
//Process first line to get field names
flHeader = sr.ReadLine().Split(charDelimiters);
//Check first field to determine header or detail file
if (flHeader[0].ToUpper() == "ORDERID")
{
header = true;
} else if (flHeader[0].ToUpper() == "ORDERITEMID"){
detail = true;
}
}
//Use TextFieldParser to read and parse files
using (TextFieldParser parser = new TextFieldParser(flName))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(delimiters);
while (!parser.EndOfData)
{
string[] fields = parser.ReadFields();
//Send read line to header or detail processor
if (header == true)
{
if (flHeader[0] != fields[0])
{
ProcessHeader(fields);
}
}
if (detail == true)
{
if (flHeader[0] != fields[0])
{
ProcessDetail(fields);
}
}
}
//Header Processor snippet
//Declare header class
Data.BLL.OrderExportHeader_BLL OrderHeaderBLL = new Data.BLL.OrderExportHeader_BLL();
foreach (string field in fields)
{
int fldCnt = fields.Count();
//Loop through each field then use the switch to determine which field is to be filled in
for (int flds = 0; flds < fldCnt; flds++ )
{
string strField = field.Trim();
switch (flHeader[flds].ToUpper())
{
case "ORDERID":
OrderHeaderBLL.OrderID = strField;
break;
}
}
}
//header file
OrderID ManufacturerID CustomerID SalesRepID PONumber OrderDate CustomerName CustomerNumber RepNumber Discount Terms ShipVia Notes ShipToCompanyName ShipToContactName ShipToContactPhone ShipToFax ShipToContactEmail ShipToAddress1 ShipToAddress2 ShipToCity ShipToState ShipToZip ShipToCountry ShipDate BillingAddress1 BillingAddress2 BillingCity BillingState BillingZip BillingCountry FreightTerm PriceLevel OrderType OrderStatus IsPlaced ContactName ContactPhone ContactEmail ContactFax Exported ExportDate Source ContainerName ContainerCubes Origin MarketName FOB SubTotal OrderTotal TaxRate TaxTotal ShippingTotal IsDeleted IsContainer OrderGUID CancelDate DoNotShipBefore WrittenByName WrittenForName WrittenForRepNumber CatalogCode CatalogName ShipToCode
491975 18 0 2621 1234 7/17/2014 RepZio 2499174 0 Test 561-351-7416 max#repzio.com 465 Ocean Ridge Way Juno Beach FL 33408 7/18/2014 465 Ocean Ridge Way Juno Beach FL 33408 USA 0 ShopZio True Max Fraser 561-351-7416 max#repzio.com False ShopZio 0.00 ShopZio 1500.0000 1500.0000 0.000 0.0000 0.0000 False False 63960a7b-86b7-47a2-ad11-9763a6b52fd0 7/31/2014 7/18/2014
Your sample data is the key, and your sample is currently obscure, but I think it matches the description that follows.
Per your example of 10 fields out of a possible 100.
In parsing each line, you only need to split in into 10 fields. It looks like you are delimited by whitespace, but you have a problem in that fields can contain embedded whitespace. Perhaps your data is actually tab delimited in which case you are ok.
For simplicity, I am going to assume your 100 fields are name 'fld0', 'fld1', ..., 'fld99'
Now, assuming the received file contains this header
fld10, fld50, fld0, fld20, fld80, fld70, fld0, fld90, fld50, fld60
and a line of data looks like
Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliet
e.g.
split[0] = "Alpha", split[1] = "Bravo", etc.
You parse the header and find that the indexes in your master list of 100 fields are 10,50,0 etc.
So you build a lookupFld array with these index value, i.e., lookupFld[0] = 10, lookupFld[1] = 50, etc
Now, as you process each line, split into 10 fields and you have an immediate indexed lookup of the correct corresponding field in your master field list.
Now MasterList[0] = "fld0", MasterList[1] = "fld1", ..., MasterList[99] = "fld99"
for (ii=0; ii<lookupFld.count; ++ii)
{
// MasterField[lookupFld[ii]] is represented by with split[ii]
// when ii = 0
// lookupFld[0] is 10
// so MasterField[10] /* fld10 */ is represented by split[0] /* alpha */
}

Categories

Resources