C# pass file text to list when file is entered via command line argument










0















I am writing a program for an assignment that is meant to read two text files and use their data to write to a third text file. I was instructed to pass the contents of the one file to a list. I have done something similar, passing the contents to an array (see below). But I can't seem to get it to work with a list.



Here is what I have done in the past with arrays:



 StreamReader f1 = new StreamReader(args[0]);
StreamReader f2 = new StreamReader(args[1]);
StreamWriter p = new StreamWriter(args[2]);
double array1 = new double[20];
double array2 = new double[20];
double array3 = new double[20];

string line;
int index;
double value;

while ((line = f1.ReadLine()) != null)
');
index = Convert.ToInt16(currentLine[0]);
value = Convert.ToDouble(currentLine[1]);
array1[index] = value;



If it is of any interest, this is my current setup:



 static void Main(String args)
{
// Create variables to hold the 3 elements of each item that you will read from the file
// Create variables for all 3 files (2 for READ, 1 for WRITE)
int ID;
string InvName;
int Number;

string IDString;
string NumberString;

string line;

List<InventoryNode> Inventory = new List<InventoryNode>();
InventoryNode Item = null;

StreamReader f1 = new StreamReader(args[0]);
StreamReader f2 = new StreamReader(args[1]);
StreamWriter p = new StreamWriter(args[2]);


// Read each item from the Update File and process the data

//Data is separated by pipe |









share|improve this question






















  • What is the structure of InventoryNode?

    – SwiftingDuster
    Nov 16 '18 at 2:51















0















I am writing a program for an assignment that is meant to read two text files and use their data to write to a third text file. I was instructed to pass the contents of the one file to a list. I have done something similar, passing the contents to an array (see below). But I can't seem to get it to work with a list.



Here is what I have done in the past with arrays:



 StreamReader f1 = new StreamReader(args[0]);
StreamReader f2 = new StreamReader(args[1]);
StreamWriter p = new StreamWriter(args[2]);
double array1 = new double[20];
double array2 = new double[20];
double array3 = new double[20];

string line;
int index;
double value;

while ((line = f1.ReadLine()) != null)
');
index = Convert.ToInt16(currentLine[0]);
value = Convert.ToDouble(currentLine[1]);
array1[index] = value;



If it is of any interest, this is my current setup:



 static void Main(String args)
{
// Create variables to hold the 3 elements of each item that you will read from the file
// Create variables for all 3 files (2 for READ, 1 for WRITE)
int ID;
string InvName;
int Number;

string IDString;
string NumberString;

string line;

List<InventoryNode> Inventory = new List<InventoryNode>();
InventoryNode Item = null;

StreamReader f1 = new StreamReader(args[0]);
StreamReader f2 = new StreamReader(args[1]);
StreamWriter p = new StreamWriter(args[2]);


// Read each item from the Update File and process the data

//Data is separated by pipe |









share|improve this question






















  • What is the structure of InventoryNode?

    – SwiftingDuster
    Nov 16 '18 at 2:51













0












0








0








I am writing a program for an assignment that is meant to read two text files and use their data to write to a third text file. I was instructed to pass the contents of the one file to a list. I have done something similar, passing the contents to an array (see below). But I can't seem to get it to work with a list.



Here is what I have done in the past with arrays:



 StreamReader f1 = new StreamReader(args[0]);
StreamReader f2 = new StreamReader(args[1]);
StreamWriter p = new StreamWriter(args[2]);
double array1 = new double[20];
double array2 = new double[20];
double array3 = new double[20];

string line;
int index;
double value;

while ((line = f1.ReadLine()) != null)
');
index = Convert.ToInt16(currentLine[0]);
value = Convert.ToDouble(currentLine[1]);
array1[index] = value;



If it is of any interest, this is my current setup:



 static void Main(String args)
{
// Create variables to hold the 3 elements of each item that you will read from the file
// Create variables for all 3 files (2 for READ, 1 for WRITE)
int ID;
string InvName;
int Number;

string IDString;
string NumberString;

string line;

List<InventoryNode> Inventory = new List<InventoryNode>();
InventoryNode Item = null;

StreamReader f1 = new StreamReader(args[0]);
StreamReader f2 = new StreamReader(args[1]);
StreamWriter p = new StreamWriter(args[2]);


// Read each item from the Update File and process the data

//Data is separated by pipe |









share|improve this question














I am writing a program for an assignment that is meant to read two text files and use their data to write to a third text file. I was instructed to pass the contents of the one file to a list. I have done something similar, passing the contents to an array (see below). But I can't seem to get it to work with a list.



Here is what I have done in the past with arrays:



 StreamReader f1 = new StreamReader(args[0]);
StreamReader f2 = new StreamReader(args[1]);
StreamWriter p = new StreamWriter(args[2]);
double array1 = new double[20];
double array2 = new double[20];
double array3 = new double[20];

string line;
int index;
double value;

while ((line = f1.ReadLine()) != null)
');
index = Convert.ToInt16(currentLine[0]);
value = Convert.ToDouble(currentLine[1]);
array1[index] = value;



If it is of any interest, this is my current setup:



 static void Main(String args)
{
// Create variables to hold the 3 elements of each item that you will read from the file
// Create variables for all 3 files (2 for READ, 1 for WRITE)
int ID;
string InvName;
int Number;

string IDString;
string NumberString;

string line;

List<InventoryNode> Inventory = new List<InventoryNode>();
InventoryNode Item = null;

StreamReader f1 = new StreamReader(args[0]);
StreamReader f2 = new StreamReader(args[1]);
StreamWriter p = new StreamWriter(args[2]);


// Read each item from the Update File and process the data

//Data is separated by pipe |






c# list file-io command-line arguments






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 2:03









ZL4892ZL4892

495




495












  • What is the structure of InventoryNode?

    – SwiftingDuster
    Nov 16 '18 at 2:51

















  • What is the structure of InventoryNode?

    – SwiftingDuster
    Nov 16 '18 at 2:51
















What is the structure of InventoryNode?

– SwiftingDuster
Nov 16 '18 at 2:51





What is the structure of InventoryNode?

– SwiftingDuster
Nov 16 '18 at 2:51












2 Answers
2






active

oldest

votes


















1














If you want to convert Array to List, you can just call Add or Insert to make it happen.



According to your code, you can do Inventory.Add(Item).



while ((line = f1.ReadLine()) != null)

string currentLine = line.Split('


like this.






share|improve this answer






























    0














    If I understand it correctly all you want to do is read two input file, parse the data in these file in a particular format (in this case int|double) and then write it to a new file. If this is the requirement, please try out the following code, as it is not sure how you want the data to be presented in the third file I have kept the format as it is (i.e. int|double)



     static void Main(string args)
    !ValidateFilePath(args[1]))

    return;


    Dictionary<int, double> parsedFileData = new Dictionary<int, double>();

    //Read the first file
    ReadFileData(args[0], parsedFileData);

    //Read second file
    ReadFileData(args[1], parsedFileData);

    //Write to third file
    WriteFileData(args[2], parsedFileData);


    private static bool ValidateFilePath(string filePath)

    try

    return File.Exists(filePath);

    catch (Exception)

    Console.WriteLine($"Failed to read file : filePath");
    return false;



    private static void ReadFileData(string filePath, Dictionary<int, double> parsedFileData)

    try

    using (StreamReader fileStream = new StreamReader(filePath))

    string line;
    while ((line = fileStream.ReadLine()) != null)

    string currentLine = line.Split('


    catch (Exception ex)

    Console.WriteLine($"Exception : ex.Message");



    private static void WriteFileData(string filePath, Dictionary<int, double> parsedFileData)

    try

    using (StreamWriter fileStream = new StreamWriter(filePath))

    foreach (var parsedLine in parsedFileData)

    var line = parsedLine.Key + "


    catch (Exception ex)

    Console.WriteLine($"Exception : ex.Message");




    There are few things you should always remember while writing a C# code :



    1) Validate command line inputs before using.



    2) Always lookout for any class that has dispose method, instantiate it inside using block.



    3) Proper mechanism in the code to catch exceptions, else your program would crash at runtime with invalid inputs or inputs that you could not validate!






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330435%2fc-sharp-pass-file-text-to-list-when-file-is-entered-via-command-line-argument%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      If you want to convert Array to List, you can just call Add or Insert to make it happen.



      According to your code, you can do Inventory.Add(Item).



      while ((line = f1.ReadLine()) != null)

      string currentLine = line.Split('


      like this.






      share|improve this answer



























        1














        If you want to convert Array to List, you can just call Add or Insert to make it happen.



        According to your code, you can do Inventory.Add(Item).



        while ((line = f1.ReadLine()) != null)

        string currentLine = line.Split('


        like this.






        share|improve this answer

























          1












          1








          1







          If you want to convert Array to List, you can just call Add or Insert to make it happen.



          According to your code, you can do Inventory.Add(Item).



          while ((line = f1.ReadLine()) != null)

          string currentLine = line.Split('


          like this.






          share|improve this answer













          If you want to convert Array to List, you can just call Add or Insert to make it happen.



          According to your code, you can do Inventory.Add(Item).



          while ((line = f1.ReadLine()) != null)

          string currentLine = line.Split('


          like this.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 2:08









          SlaneRSlaneR

          416313




          416313























              0














              If I understand it correctly all you want to do is read two input file, parse the data in these file in a particular format (in this case int|double) and then write it to a new file. If this is the requirement, please try out the following code, as it is not sure how you want the data to be presented in the third file I have kept the format as it is (i.e. int|double)



               static void Main(string args)
              !ValidateFilePath(args[1]))

              return;


              Dictionary<int, double> parsedFileData = new Dictionary<int, double>();

              //Read the first file
              ReadFileData(args[0], parsedFileData);

              //Read second file
              ReadFileData(args[1], parsedFileData);

              //Write to third file
              WriteFileData(args[2], parsedFileData);


              private static bool ValidateFilePath(string filePath)

              try

              return File.Exists(filePath);

              catch (Exception)

              Console.WriteLine($"Failed to read file : filePath");
              return false;



              private static void ReadFileData(string filePath, Dictionary<int, double> parsedFileData)

              try

              using (StreamReader fileStream = new StreamReader(filePath))

              string line;
              while ((line = fileStream.ReadLine()) != null)

              string currentLine = line.Split('


              catch (Exception ex)

              Console.WriteLine($"Exception : ex.Message");



              private static void WriteFileData(string filePath, Dictionary<int, double> parsedFileData)

              try

              using (StreamWriter fileStream = new StreamWriter(filePath))

              foreach (var parsedLine in parsedFileData)

              var line = parsedLine.Key + "


              catch (Exception ex)

              Console.WriteLine($"Exception : ex.Message");




              There are few things you should always remember while writing a C# code :



              1) Validate command line inputs before using.



              2) Always lookout for any class that has dispose method, instantiate it inside using block.



              3) Proper mechanism in the code to catch exceptions, else your program would crash at runtime with invalid inputs or inputs that you could not validate!






              share|improve this answer



























                0














                If I understand it correctly all you want to do is read two input file, parse the data in these file in a particular format (in this case int|double) and then write it to a new file. If this is the requirement, please try out the following code, as it is not sure how you want the data to be presented in the third file I have kept the format as it is (i.e. int|double)



                 static void Main(string args)
                !ValidateFilePath(args[1]))

                return;


                Dictionary<int, double> parsedFileData = new Dictionary<int, double>();

                //Read the first file
                ReadFileData(args[0], parsedFileData);

                //Read second file
                ReadFileData(args[1], parsedFileData);

                //Write to third file
                WriteFileData(args[2], parsedFileData);


                private static bool ValidateFilePath(string filePath)

                try

                return File.Exists(filePath);

                catch (Exception)

                Console.WriteLine($"Failed to read file : filePath");
                return false;



                private static void ReadFileData(string filePath, Dictionary<int, double> parsedFileData)

                try

                using (StreamReader fileStream = new StreamReader(filePath))

                string line;
                while ((line = fileStream.ReadLine()) != null)

                string currentLine = line.Split('


                catch (Exception ex)

                Console.WriteLine($"Exception : ex.Message");



                private static void WriteFileData(string filePath, Dictionary<int, double> parsedFileData)

                try

                using (StreamWriter fileStream = new StreamWriter(filePath))

                foreach (var parsedLine in parsedFileData)

                var line = parsedLine.Key + "


                catch (Exception ex)

                Console.WriteLine($"Exception : ex.Message");




                There are few things you should always remember while writing a C# code :



                1) Validate command line inputs before using.



                2) Always lookout for any class that has dispose method, instantiate it inside using block.



                3) Proper mechanism in the code to catch exceptions, else your program would crash at runtime with invalid inputs or inputs that you could not validate!






                share|improve this answer

























                  0












                  0








                  0







                  If I understand it correctly all you want to do is read two input file, parse the data in these file in a particular format (in this case int|double) and then write it to a new file. If this is the requirement, please try out the following code, as it is not sure how you want the data to be presented in the third file I have kept the format as it is (i.e. int|double)



                   static void Main(string args)
                  !ValidateFilePath(args[1]))

                  return;


                  Dictionary<int, double> parsedFileData = new Dictionary<int, double>();

                  //Read the first file
                  ReadFileData(args[0], parsedFileData);

                  //Read second file
                  ReadFileData(args[1], parsedFileData);

                  //Write to third file
                  WriteFileData(args[2], parsedFileData);


                  private static bool ValidateFilePath(string filePath)

                  try

                  return File.Exists(filePath);

                  catch (Exception)

                  Console.WriteLine($"Failed to read file : filePath");
                  return false;



                  private static void ReadFileData(string filePath, Dictionary<int, double> parsedFileData)

                  try

                  using (StreamReader fileStream = new StreamReader(filePath))

                  string line;
                  while ((line = fileStream.ReadLine()) != null)

                  string currentLine = line.Split('


                  catch (Exception ex)

                  Console.WriteLine($"Exception : ex.Message");



                  private static void WriteFileData(string filePath, Dictionary<int, double> parsedFileData)

                  try

                  using (StreamWriter fileStream = new StreamWriter(filePath))

                  foreach (var parsedLine in parsedFileData)

                  var line = parsedLine.Key + "


                  catch (Exception ex)

                  Console.WriteLine($"Exception : ex.Message");




                  There are few things you should always remember while writing a C# code :



                  1) Validate command line inputs before using.



                  2) Always lookout for any class that has dispose method, instantiate it inside using block.



                  3) Proper mechanism in the code to catch exceptions, else your program would crash at runtime with invalid inputs or inputs that you could not validate!






                  share|improve this answer













                  If I understand it correctly all you want to do is read two input file, parse the data in these file in a particular format (in this case int|double) and then write it to a new file. If this is the requirement, please try out the following code, as it is not sure how you want the data to be presented in the third file I have kept the format as it is (i.e. int|double)



                   static void Main(string args)
                  !ValidateFilePath(args[1]))

                  return;


                  Dictionary<int, double> parsedFileData = new Dictionary<int, double>();

                  //Read the first file
                  ReadFileData(args[0], parsedFileData);

                  //Read second file
                  ReadFileData(args[1], parsedFileData);

                  //Write to third file
                  WriteFileData(args[2], parsedFileData);


                  private static bool ValidateFilePath(string filePath)

                  try

                  return File.Exists(filePath);

                  catch (Exception)

                  Console.WriteLine($"Failed to read file : filePath");
                  return false;



                  private static void ReadFileData(string filePath, Dictionary<int, double> parsedFileData)

                  try

                  using (StreamReader fileStream = new StreamReader(filePath))

                  string line;
                  while ((line = fileStream.ReadLine()) != null)

                  string currentLine = line.Split('


                  catch (Exception ex)

                  Console.WriteLine($"Exception : ex.Message");



                  private static void WriteFileData(string filePath, Dictionary<int, double> parsedFileData)

                  try

                  using (StreamWriter fileStream = new StreamWriter(filePath))

                  foreach (var parsedLine in parsedFileData)

                  var line = parsedLine.Key + "


                  catch (Exception ex)

                  Console.WriteLine($"Exception : ex.Message");




                  There are few things you should always remember while writing a C# code :



                  1) Validate command line inputs before using.



                  2) Always lookout for any class that has dispose method, instantiate it inside using block.



                  3) Proper mechanism in the code to catch exceptions, else your program would crash at runtime with invalid inputs or inputs that you could not validate!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 2:42









                  user3164323user3164323

                  626




                  626



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330435%2fc-sharp-pass-file-text-to-list-when-file-is-entered-via-command-line-argument%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線