Solved

Problem opening multiple files using StreamReader

Posted on 2004-04-14
9
939 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 250 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now