?
Solved

Problem opening multiple files using StreamReader

Posted on 2004-04-14
9
Medium Priority
?
963 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

800 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