Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem opening multiple files using StreamReader

Posted on 2004-04-14
9
Medium Priority
?
969 Views
Last Modified: 2011-10-03
I'm trying to open several separate text files and populate some label controls with them.  My system works fine when I only try to do it for a single file, but when I try to do more than one file, I get the following error:

>Compiler Error Message: CS1023: Embedded statement cannot be a declaration or labeled statement
>
>Source Error:
>
>Line 48:       //lblStrVariable.Text = sOutput;
>Line 49:
>Line 50:       StreamReader objReader0 = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt");
>Line 51:       string sLine0="";
>Line 52:       ArrayList arrText0 = new ArrayList();      

My code is as follows:
--------------------------------------
private void Page_Load(object sender, System.EventArgs e)
{


      StreamReader objReader = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window_left_combined.txt");
      string sLine="";
      ArrayList arrText = new ArrayList();      
      while (sLine != null)
                  {
                        sLine = objReader.ReadLine();
                        if (sLine != null)
                              arrText.Add(sLine);
                  }
                        objReader.Close();
      foreach (string sOutput in arrText)


      StreamReader objReader0 = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt");
      string sLine0="";
      ArrayList arrText0 = new ArrayList();      
      while (sLine0 != null)
                  {
                        sLine0 = objReader0.ReadLine();
                        if (sLine0 != null)
                              arrText0.Add(sLine0);
                  }
                        objReader0.Close();
-------------------------------------------------------------------

What am I supposed to do?

Thanks.

Leprechaun



 

0
Comment
Question by:Leprechaun
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10830331
The problem is this code...

foreach (string sOutput in arrText)

     StreamReader objReader0 = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt");

You are opening the same file in a single line loop without closing it which would be a runtime problem.  The error is that you are declaring the StreamReader in a single line loop, which is not allowed.
 
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10830866
Replace your code with this (and check the logic there, I'm not sure if you wanted to do this)

StreamReader objReader = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window_left_combined.txt");
string sLine="";
ArrayList arrText = new ArrayList();    
while (sLine != null)
{
      sLine = objReader.ReadLine();
      if (sLine != null)
            arrText.Add(sLine);
}
objReader.Close();
foreach (string sOutput in arrText)
{
      StreamReader objReader0 = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt");
      string sLine0="";
      ArrayList arrText0 = new ArrayList();    
      while (sLine0 != null)
      {
            sLine0 = objReader0.ReadLine();
            if (sLine0 != null)
                  arrText0.Add(sLine0);
      }
      objReader0.Close();
}
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10833161


does this really work?  with the slashes pointing the wrong way??
"f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt"


I would have assumed you'd get an error, and your code shoul look more like:

@"f:\inetpub\wwwroot\islam-democracy\content\window0_content.txt"


Also, I don't think that you can create a new variable in a loop like that

  foreach (string sOutput in arrText)
     StreamReader objReader0 = new StreamReader(...);

I think you'll need to do something like this:


  StreamReader objReader0;
  foreach (string sOutput in arrText)
      objReader0 = new StreamReader(...);
0
Industry Leaders: 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!

 
LVL 12

Expert Comment

by:dfiala13
ID: 10833551
>>does this really work?  with the slashes pointing the wrong way??
>>"f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt"
Sure, those are valid directory delimiters and one way around escaping or using @.

the problem is the declaration in the single-line loop
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10833670
TAD, if you try what you type:

  StreamReader objReader0;
  foreach (string sOutput in arrText)
      objReader0 = new StreamReader(...);

you will just create n StreamReader() assigned to objReader0 and will use the last created one.. I don't think he wants to do that..
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 1000 total points
ID: 10835786


If he doesn't want to re-declare the same object, but instead create many, many objects with a similar name then he needs to create an array of objects


  StreamReader[] objReader0 = new StreamReader[100] ;
  int i = 0;
  foreach (string sOutput in arrText)
      objReader0[i++] = new StreamReader(...);
   
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10836275
Yeah, that may be the point
0
 

Author Comment

by:Leprechaun
ID: 10837452
Thanks, folks.  Will test suggestions later today.

Leprechaun
0
 

Author Comment

by:Leprechaun
ID: 10856388
The array solved the problem.  

I also removed the foreach logic by using ReadToEnd, which simplified the code.  

The code is now:

> StreamReader[] objReaderArray = new StreamReader[5] ;
>      
> objReaderArray[0] = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt");
> string sLeftTop = objReaderArray[0].ReadToEnd();
>
> objReaderArray[1] = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window1_content.txt");
> string sRightTop = objReaderArray[1].ReadToEnd();

Thanks.

Leprechaun

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question