Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Quick C# Help Retrieve data using while (!sr.EndOfStream)

Quick C# Help Retrieve data using while (!sr.EndOfStream)

Scheduled Pinned Locked Moved C#
helpcsharpdata-structuresquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    d87c
    wrote on last edited by
    #1

    I ran into problem for my school assignment and it is to display whatever in a meal.csv file, here is a code we are starting with. The array string of itemDetails is where the data is store then passed into array ReadLines, then in Main, it is passed to mealContent, but when I tried Console.WriteLine(mealContent); it only prompted System.String[]. So how do I at least display it out in the command prompt. Thanks for the help guys~ Source code is also attached Code/meal.csv lunch,bento box b - sashimi,box combo,$9.59 dinner,vegetable sushi,6 rolls,$3.50 dinner,tuna roll,3 rolls,$4.50 dinner,roe, 2 rolls,$3.95 lunch,bento box a - chicken teriyaki,box combo,$8.59 lunch,bento box b - sashimi,box combo,$9.59 dinner,vegetable sushi,6 rolls,$3.50 dinner,tuna roll,3 rolls,$4.50 dinner,roe, 2 rolls,$3.95 lunch,bento box a - chicken teriyaki,box combo,$8.59 FileIOManager/FileLocation.cs using System; namespace FileIOManager { static class FileLocation { public const string INPUT_FILE = "../../Data/meals.csv"; } } using System; namespace FileIOManager { static class FileLocation { public const string INPUT_FILE = "../../Data/meals.csv"; } } FileIOManager/FileReader.cs using System; using System.IO; namespace FileIOManager { static public class FileReader { private static int GetLineCount() { StreamReader sr = new StreamReader(FileLocation.INPUT_FILE); int counter = 0; while (!sr.EndOfStream) { counter++; sr.ReadLine(); } sr.Close(); return counter; } public static string[] ReadLines() { int totalItems = GetLineCount(); string[] itemDetails = new string[totalItems]; StreamReader sr = new StreamReader(FileLocation.INPUT_FILE); string itemDetail; int counter = 0; while (!sr.EndOfStream){ itemDetail = sr.ReadLine(); if (itemDetail.Trim() != "") itemDetails[counter++] = itemDetail; } sr.Close(); return itemDetails; } } } :((

    A 1 Reply Last reply
    0
    • D d87c

      I ran into problem for my school assignment and it is to display whatever in a meal.csv file, here is a code we are starting with. The array string of itemDetails is where the data is store then passed into array ReadLines, then in Main, it is passed to mealContent, but when I tried Console.WriteLine(mealContent); it only prompted System.String[]. So how do I at least display it out in the command prompt. Thanks for the help guys~ Source code is also attached Code/meal.csv lunch,bento box b - sashimi,box combo,$9.59 dinner,vegetable sushi,6 rolls,$3.50 dinner,tuna roll,3 rolls,$4.50 dinner,roe, 2 rolls,$3.95 lunch,bento box a - chicken teriyaki,box combo,$8.59 lunch,bento box b - sashimi,box combo,$9.59 dinner,vegetable sushi,6 rolls,$3.50 dinner,tuna roll,3 rolls,$4.50 dinner,roe, 2 rolls,$3.95 lunch,bento box a - chicken teriyaki,box combo,$8.59 FileIOManager/FileLocation.cs using System; namespace FileIOManager { static class FileLocation { public const string INPUT_FILE = "../../Data/meals.csv"; } } using System; namespace FileIOManager { static class FileLocation { public const string INPUT_FILE = "../../Data/meals.csv"; } } FileIOManager/FileReader.cs using System; using System.IO; namespace FileIOManager { static public class FileReader { private static int GetLineCount() { StreamReader sr = new StreamReader(FileLocation.INPUT_FILE); int counter = 0; while (!sr.EndOfStream) { counter++; sr.ReadLine(); } sr.Close(); return counter; } public static string[] ReadLines() { int totalItems = GetLineCount(); string[] itemDetails = new string[totalItems]; StreamReader sr = new StreamReader(FileLocation.INPUT_FILE); string itemDetail; int counter = 0; while (!sr.EndOfStream){ itemDetail = sr.ReadLine(); if (itemDetail.Trim() != "") itemDetails[counter++] = itemDetail; } sr.Close(); return itemDetails; } } } :((

      A Offline
      A Offline
      AspDotNetDev
      wrote on last edited by
      #2
      1. Use PRE tags with the lang attribute set to "C#" to format your code better so we have an easier time reading it and helping you. 2) There is no need for those two functions. Just use System.IO.File.ReadAllLines(), which returns an array of strings. 3) To output each item in the array of strings, use a for loop and either Console.WriteLine() or Console.Write(). 4) Instead of using the first function to scan the file to determine the number of lines, just use a List instead of an array. Lists dynamically resize based on how many elements you put in them. Of course, this is assuming that you don't want to use System.IO.File.ReadAllLines() for some reason.

      [Forum Guidelines]

      D 1 Reply Last reply
      0
      • A AspDotNetDev
        1. Use PRE tags with the lang attribute set to "C#" to format your code better so we have an easier time reading it and helping you. 2) There is no need for those two functions. Just use System.IO.File.ReadAllLines(), which returns an array of strings. 3) To output each item in the array of strings, use a for loop and either Console.WriteLine() or Console.Write(). 4) Instead of using the first function to scan the file to determine the number of lines, just use a List instead of an array. Lists dynamically resize based on how many elements you put in them. Of course, this is assuming that you don't want to use System.IO.File.ReadAllLines() for some reason.

        [Forum Guidelines]

        D Offline
        D Offline
        d87c
        wrote on last edited by
        #3

        I figure it out, just iterate over the array and write out each one individually. But im actually stuck on the next step lol i hope you can help out too. static void Main(string[] args) { string[] meals = System.IO.File.ReadAllLines("meals.csv"); foreach (string meal in meals) Console.WriteLine(meal); Console.ReadKey(); } the price actually needed to times by 1.8 so Price = Cost * 1.8 also i'll be dealing with bubble sort.. which I have never learn to knew how to coz we have ot make it look like this in the end * Lunch Items * $15.46 bento box a - chicken teriyaki, box combo $17.26 bento box b – sashimi, box combo * Dinner Items * $7.11 roe, 2 rolls $8.10 tuna roll, 3 rolls $6.30 vegetable sushi, 6 rolls

        A 1 Reply Last reply
        0
        • D d87c

          I figure it out, just iterate over the array and write out each one individually. But im actually stuck on the next step lol i hope you can help out too. static void Main(string[] args) { string[] meals = System.IO.File.ReadAllLines("meals.csv"); foreach (string meal in meals) Console.WriteLine(meal); Console.ReadKey(); } the price actually needed to times by 1.8 so Price = Cost * 1.8 also i'll be dealing with bubble sort.. which I have never learn to knew how to coz we have ot make it look like this in the end * Lunch Items * $15.46 bento box a - chicken teriyaki, box combo $17.26 bento box b – sashimi, box combo * Dinner Items * $7.11 roe, 2 rolls $8.10 tuna roll, 3 rolls $6.30 vegetable sushi, 6 rolls

          A Offline
          A Offline
          AspDotNetDev
          wrote on last edited by
          #4

          d87c wrote:

          i hope you can help

          I might have, but I will not because you didn't take my advice to use PRE tags. Ignoring my advice clearly shows you aren't considerate of others and would rather have them wade through your poorly formatted code than spend a little time learning how to properly format it. Why should I spend time trying to teach somebody who doesn't want to learn? Also, a bit of advice, use the real words you mean rather than the txt speak versions (e.g., "coz", "ot"). We here appreciate people who realize they have a full keyboard to type with and use that ability. Also, avoiding txt speak will help in the workplace, as using txt speak makes you appear childish and less professional.

          [Forum Guidelines]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups