Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C# streamreader and while loop
New Posts  All Forums:Forum Nav:

C# streamreader and while loop

post #1 of 7
Thread Starter 
Hey, how do i create a while loop that loops over a value in a text file until it equals something?

example:

streamreader sr = new streamreader("text.txt");

while (sr.readline < 1)
{

}

But how do i actually get it to work?
My Computer ...
(11 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom ii x4 955 @4ghz M4A88T-M/USB3 ASUS HD6870 DirectCU 4GB ValueRam 
Hard DriveHard DriveOSMonitor
External 500GB for storage Crucial M4 128GB Windows 7  32* LCD Screen 
KeyboardPowerCase
Logitech G110 CX600 Cooler Master 
  hide details  
Reply
My Computer ...
(11 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom ii x4 955 @4ghz M4A88T-M/USB3 ASUS HD6870 DirectCU 4GB ValueRam 
Hard DriveHard DriveOSMonitor
External 500GB for storage Crucial M4 128GB Windows 7  32* LCD Screen 
KeyboardPowerCase
Logitech G110 CX600 Cooler Master 
  hide details  
Reply
post #2 of 7
Readline, reads the next line of the file and returns a string.

Havent worked with streams in awhile but afaik
Code:


using(StreamReader sr = new StreamReader("Yourfile"))
{

            String s;

            while (!String.IsNullOrEmpty(s = sr.ReadLine()))
            {
                if (s == "YourValueHere")
                {
                    
                }
            }
}

Edit: You should also make sure you use a using block so the stream is properly cleaned up and doesnt leave an open stream to your file which could cause problems.
Edited by mykah89 - 5/3/13 at 3:26pm
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #3 of 7
Instead of using a StreamReader, I would use the great File class. It uses StreamReader in the backend, but to do what you're wanting to do with File you can just do this:
Code:
foreach (string line in File.ReadAllLines("whicheverFileYouWant.txt"))
{
    //do whatever you want
}

Edited by 3930K - 5/5/13 at 2:13am
post #4 of 7
Quote:
Originally Posted by 3930K View Post

Instead of using a StreamReader, I would use the great File class. It uses StreamReader in the backend, but to do what you're wanting to do with File you can just do this:
Code:
foreach (string line in File.ReadAllLines("whicheverFileYouWant.txt"))
{
    //do whatever you want
}

Perfectos.

For anyone who cares, File.ReadAllLines looks like this:
Code:
public static string[] ReadAllLines(string path, Encoding encoding)
{
      ArrayList list = new ArrayList();
      using (StreamReader reader = new StreamReader(path, encoding))
      {
            string str;
            while ((str = reader.ReadLine()) != null)
            {
                  list.Add(str);
            }
      }
      return (string[]) list.ToArray(typeof(string));
}
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #5 of 7
ReadAllLines is great unless you have a large file, because it will always (surprise, surprise) read all lines, which can take a while and use a large amount of memory. In those cases you'd probably want to use ReadLines instead, because each lines is read as it is required, and therefore you can stop reading more lines once you've found what you're looking for. All you'd need to do is break out of the foreach loop.

For even larger files you need to work in batches, but I'm sure that's well outside of the scope of what you're doing tongue.gif
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
post #6 of 7
Quote:
Originally Posted by randomizer View Post

ReadAllLines is great unless you have a large file, because it will always (surprise, surprise) read all lines, which can take a while and use a large amount of memory. In those cases you'd probably want to use ReadLines instead, because each lines is read as it is required, and therefore you can stop reading more lines once you've found what you're looking for. All you'd need to do is break out of the foreach loop.

For even larger files you need to work in batches, but I'm sure that's well outside of the scope of what you're doing tongue.gif
This.
post #7 of 7
check this one C# loops and statements gives you better understading of loops and contitional statements.

Mark.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C# streamreader and while loop