943,937 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 6047
  • C# RSS
Nov 14th, 2006
0

Trying to obtain text from a file.

Expand Post »
Hi Guys

I am new to C# programming and I wandered whether anyone could help me regarding this error message below:

C:\MyFirstCOMTemplate\COM\PushTemplateC\PushTemplateC.cs(86): 'PushTemplateC.PushTemplateC.PblReadFile()': not all code paths return a value

I am simply trying to get the text below:

My name is
Neil Rastogi
I am aged
25

to be output in a listbox. I have created a simple data2.txt file using streamwriter class below within a c# component:

public void PblAddFile()
{
//int PrvFile = FreeFile();
try
{
// Create a new file to work with
FileStream fsOut = File.Create("C:\\data2.txt");
StreamWriter sw = new StreamWriter (fsOut);
sw.WriteLine("My name is");
sw.WriteLine("Neil Rastogi");
sw.WriteLine("I am aged");
sw.WriteLine("25");
sw.Flush();
sw.Close();
fsOut.Close();
}
catch (Exception)
{
Console.WriteLine("Error raised writing to the file");
}
}
The method above works fine. The second method below is what is causing the error message below:

public string PblReadFile()
{
FileStream fsIn = File.OpenRead("C:\\data2.txt");
StreamReader sr = new StreamReader (fsIn);
//And read the data
while (sr.Peek() > -1)
{
lbLines.Items.Add(sr.ReadLine());
//PblReadFile() = sr.ReadLine();
return sr.ReadLine(); 
}
sr.Close();
fsIn.Close();
}
I am not sure really what is wrong can anyone suggest any ideas? Thanks.
Last edited by tgreer; Nov 15th, 2006 at 11:32 am. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bullet102 is offline Offline
2 posts
since Nov 2006
Nov 14th, 2006
0

Re: Trying to obtain text from a file.

Hi, your problem is not related to the text file that you want to read. The real problem is because you create a method that has a return value of type string, but inside the method's logic, there's a program flow that will end up with not giving any return value.

I don't know what is exactly do you want with the second method. Since inside the loop, there's a return statement, meaning if you invoke this method, it will quit directly after reading the first line of your text file, and give the second line of your text file as a return value. Is this really what you want?

Ok, back to the problem, so let's say there's nothing inside your text file. So the flow of the program never goes inside the loop. The flow jump into the next line after the while(...) { .... }, closing the sr and fsIn, then... then what? here's the things who makes the problem, since after closing the sr and fsIn, the flow is end, without any return value given.

Hope that's help.

regards,
Lok
Last edited by lok_tan; Nov 14th, 2006 at 11:56 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lok_tan is offline Offline
8 posts
since Nov 2006
Nov 15th, 2006
0

Re: Trying to obtain text from a file.

Hi

Thanks for your help so far but I am not sure exactly what the solution is? Are you sugesting the while loop code should be omitted? I did this below:

public string PblReadFile()
{
FileStream fsIn = File.OpenRead("C:\\data2.txt");
StreamReader sr = new StreamReader (fsIn);
//And read the data
//while (sr.Peek() > -1)
//{
//lbLines.Items.Add(sr.ReadLine());
//PblReadFile() = sr.ReadLine();
//}
return sr.ReadLine();
sr.Close();
fsIn.Close();
}

This compiles fine but does not do anything. Can you provide a bit more clarification please.
Last edited by tgreer; Nov 15th, 2006 at 11:33 am. Reason: Removed unnecessary quote block, added code tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bullet102 is offline Offline
2 posts
since Nov 2006
Nov 15th, 2006
0

Re: Trying to obtain text from a file.

http://www.csharp-station.com/HowTo/...eTextFile.aspx

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.IO;
  3.  
  4.  
  5. class TextFileReader
  6. {
  7. static void Main(string[] args)
  8. {
  9. // create reader & open file
  10. TextReader tr = new StreamReader("C:\\data2.txt");
  11.  
  12. // read a line of text
  13. Console.WriteLine(tr.ReadLine());
  14.  
  15. // close the stream
  16. tr.Close();
  17. }
  18. }

swap the Console.WriteLine for lbLines.Items.Add I guess
Last edited by iamthwee; Nov 15th, 2006 at 3:21 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 15th, 2006
0

Re: Trying to obtain text from a file.

Click to Expand / Collapse  Quote originally posted by bullet102 ...
Hi

Thanks for your help so far but I am not sure exactly what the solution is? Are you sugesting the while loop code should be omitted? I did this below:

public string PblReadFile()
{
FileStream fsIn = File.OpenRead("C:\\data2.txt");
StreamReader sr = new StreamReader (fsIn);
//And read the data
//while (sr.Peek() > -1)
//{
//lbLines.Items.Add(sr.ReadLine());
//PblReadFile() = sr.ReadLine();
//}
return sr.ReadLine();
sr.Close();
fsIn.Close();
}
This compiles fine but does not do anything. Can you provide a bit more clarification please.
No, the while loop is an essential part to make you possible to read all the lines of the text files using StreamReader.ReadLine().

Before I continue, can you explain first, what should method PblReadFile return? In your program, it return one/single string, but actually the data on your text file is multi-line, so what do you want? do you want to return the whole text file inside one string? or actually you want a multi string (one string for each lines)?

My best guess is you better return an array of string, so it'is also more easier for you to loop the array, and put each string item into a listbox.

if not, use StreamReader.ReadToEnd(), return it as single string, then split it using String.Split("\n\r"), and put it into listbox one by one.

Regards,

Lok
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lok_tan is offline Offline
8 posts
since Nov 2006
Nov 24th, 2006
0

Re: Trying to obtain text from a file.

I think your problem is the way you were trying ot use the StreamReader object
Please see below, for some pointers

Hope this helps,

C# Syntax (Toggle Plain Text)
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. using( System.IO.FileStream fsOut = System.IO.File.Create("c:\\TestText.txt"))
  6. using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fsOut))
  7. {
  8. string[] sStuff = {"Some Text" , "Test", "Bravo"};
  9. for (int i = sStuff.GetLowerBound(0); i <= sStuff.GetUpperBound(0); i++)
  10. {
  11. sw.WriteLine(sStuff[i]);
  12. }
  13. sw.Flush();
  14.  
  15. }
  16.  
  17. using (System.IO.StreamReader sr = new System.IO.StreamReader(new System.IO.FileStream("c:\\TestText.txt", System.IO.FileMode.Open)))
  18.  
  19. //using( System.IO.StreamReader sr = new System.IO.StreamReader(fsRead))
  20. {
  21. while (sr.Peek() > -1)
  22. {
  23. Console.WriteLine( sr.ReadLine());
  24. }
  25. }
  26.  
  27. Console.ReadLine();
  28.  
  29.  
  30. }
  31. }
Last edited by cykophysh; Nov 24th, 2006 at 7:49 pm. Reason: removed some irrelevant text
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cykophysh is offline Offline
1 posts
since Mar 2004
Nov 25th, 2006
0

Re: Trying to obtain text from a file.

You mentioned in your original post that you wanted to just post this text data to a listbox.

I took your public string PblReadFile() and changed it to public void PblReadFile() then commented out your
return sr.ReadLine();
and it populated the listbox just fine.

Jerry
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: SQL DataReader
Next Thread in C# Forum Timeline: Project Ideas Required For Msc dissertation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC