Solved

Problem opening multiple files using StreamReader

Posted on 2004-04-14
9
958 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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 video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 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