[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

c-sharp file manipoation (jgordin2)

how do i in C-sharp:
1. read a flat file: abc1 123
                            abc2  456
                            abc3  789
                                 ......
2. while i am processing the rows of this file i need to populate a dynamic data structure (smth like a vector in c++)

3. pass the dynamic structure to a function that is able to process parameters
0
jgordin
Asked:
jgordin
  • 9
  • 6
  • 4
  • +1
1 Solution
 
Bob LearnedCommented:
You can use a StreamReader:

using (StreamReader reader = new StreamReader(fileName))
{
    while (reader.Peek() != -1)
    {
         string line = reader.ReadLine();
    }
}

Bob
0
 
jgordinAuthor Commented:
that addresses the first part of the question. how do parse the line (space or tab del) and populate dynamic datastructure that i can aftewards parse as a parameter to a function. the function should be able to process entries of the dynamic structure. thanks.
0
 
Bob LearnedCommented:
You can parse the line by using the Split function.

Bob
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jgordinAuthor Commented:
i need a sample code. thanks.
0
 
Bob LearnedCommented:
string parts[] = text.Split(',');

Bob
0
 
jgordinAuthor Commented:
can you please put a sample code that:

1. read a flat file: abc1 123
                            abc2  456
                            abc3  789
                                 ......
2. while i am processing the rows of this file i need to populate a dynamic data structure (smth like a vector in c++)

3. pass the dynamic structure to a function that is able to process parameters
0
 
Gautham JanardhanCommented:
System.IO.StreamReader FReader =
                        new System.IO.StreamReader(@"C:\test.txt");
                  string str = FReader.ReadLine();
                  DataTable FTable =
                        new DataTable();
                  FTable.Columns.Add("Col1");
                  FTable.Columns.Add("Col2");
                  while(str != string.Empty && str != null)
                  {
                         DataRow FRow = FTable.NewRow();
                        string []Dat = str.Split(" ".ToCharArray());
                        FRow["Col1"] = Dat[0];
                        FRow["Col2"] = Dat[0];
                        FTable.Rows.Add(FRow);
                        str = FReader.ReadLine();
                  }
0
 
Gautham JanardhanCommented:
2. while i am processing the rows of this file i need to populate a dynamic data structure (smth like a vector in c++)

3. pass the dynamic structure to a function that is able to process parameters

can u explain
0
 
jgordinAuthor Commented:
2. i need to pass created table (FTable) to a function.
3. function should process/access the rows of the table. that is, i need to extract the data from col1 and col2:  
         function(table)
           {
                for every row
                {
                   get col1 and col2
                   function2(col1,col2)
               }
               
           }
0
 
REA_ANDREWCommented:
do you mean adding these values to say a List<string> and then passing this dynamic array to a function like

List<string>YourArray = new List<string>();

public void YourFunction(params string[] values)
{
//....
}

YourFunction(YourArray.ToArray());
0
 
Gautham JanardhanCommented:
           void ProcessPostedFile(DataTable FTable)
            {
                  for(int i=0;i<FTable.Rows.Count;i++)
                  {
                        string Value1 = FTable.Rows[i]["Col1"].ToString();
                        string Value2 = FTable.Rows[i]["Col1"].ToString();
                  }
            }
0
 
REA_ANDREWCommented:
DataTable yourDT = new DataTable()
yourDT.Columns.AddRange(new DataColumn[]{
new DataColumn("Col1"),
new DataColumn("Col2")
});

using(System.IO.StreamReader sr1 = new System.IO.StreamReader(@"C:\test.txt"))
{
string line = null;
while((line = sr1.ReadLine())!=null)
{
DataRow dr1 = yourDT.NewRow();
dr1.ItemArray = new string[]{System.Text.RegularExpressions.Split(" ",line)[0],System.Text.RegularExpressions.Split(" ",line)[1]};
}
}

foreach(DataRow dr in yourDT.Rows)
{
string firstValue = dr[0].ToString();
string secondValue = dr[1].ToString();
}

try that

Andrew
0
 
jgordinAuthor Commented:
i think gauthampj's solution to read the file and populate dynamic table is what i need. i just need to more steps:
2. i need to pass created table (FTable) to a function.
3. function should process/access the rows of the table. that is, i need to extract the data from col1 and col2:  
         function(table)
           {
                for every row
                {
                   get col1 and col2
                   function2(col1,col2)
               }
               
           }
0
 
REA_ANDREWCommented:
sorry that should be

System.Text.RegularExpressions.Regex.Split
0
 
Gautham JanardhanCommented:
 void ProcessPostedFile(DataTable FTable)
            {
                  for(int i=0;i<FTable.Rows.Count;i++)
                  {
                        string Value1 = FTable.Rows[i]["Col1"].ToString();
                        string Value2 = FTable.Rows[i]["Col1"].ToString();
                  }
            }
0
 
jgordinAuthor Commented:
does it have to be Dat[0] or Dat[1] fpr second column? also, is there a way trim (left and right) spaces. thanks.
0
 
jgordinAuthor Commented:
when compile i get an error nammespace name 'DataTable' could not be found. how do i fix that problem. thanks.
0
 
jgordinAuthor Commented:
i also get simillar errors for DataRow.
0
 
REA_ANDREWCommented:
you need to include

using System.Data;

so the DataTable in full would be

System.Data.DataTable

Andrew
0
 
REA_ANDREWCommented:
it is also a zero based array so

0 = 1
1 = 2
2 = 3
etc...

To trim you can use the String function

so

dr[0].Trim();

Andrew
0
 
jgordinAuthor Commented:
thanks for using System.Data;

does it have to be:
FRow["Col1"] = Dat[0];
                FRow["Col2"] = Dat[0];
                FRow["Col3"] = Dat[0];

or

FRow["Col1"] = Dat[0];
                FRow["Col2"] = Dat[1];
                FRow["Col3"] = Dat[2];

0
 
REA_ANDREWCommented:
FRow["Col1"] = Dat[0]; //Asigns the first object to the Col1 Column
FRow["Col2"] = Dat[1];//Asigns the second object to the Col2 Column
FRow["Col3"] = Dat[2];//Asigns the third object to the Col3 Column

Andrew
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now