Trying to obtain text from a file.

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Nov 2006
Posts: 2
Reputation: bullet102 is an unknown quantity at this point 
Solved Threads: 0
bullet102 bullet102 is offline Offline
Newbie Poster

Trying to obtain text from a file.

 
0
  #1
Nov 14th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 8
Reputation: lok_tan is an unknown quantity at this point 
Solved Threads: 0
lok_tan's Avatar
lok_tan lok_tan is offline Offline
Newbie Poster

Re: Trying to obtain text from a file.

 
0
  #2
Nov 14th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 2
Reputation: bullet102 is an unknown quantity at this point 
Solved Threads: 0
bullet102 bullet102 is offline Offline
Newbie Poster

Re: Trying to obtain text from a file.

 
0
  #3
Nov 15th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Trying to obtain text from a file.

 
0
  #4
Nov 15th, 2006
http://www.csharp-station.com/HowTo/...eTextFile.aspx

  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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 8
Reputation: lok_tan is an unknown quantity at this point 
Solved Threads: 0
lok_tan's Avatar
lok_tan lok_tan is offline Offline
Newbie Poster

Re: Trying to obtain text from a file.

 
0
  #5
Nov 15th, 2006
Originally Posted by bullet102 View Post
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1
Reputation: cykophysh is an unknown quantity at this point 
Solved Threads: 0
cykophysh cykophysh is offline Offline
Newbie Poster

Re: Trying to obtain text from a file.

 
0
  #6
Nov 24th, 2006
I think your problem is the way you were trying ot use the StreamReader object
Please see below, for some pointers

Hope this helps,

  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
Kind Regards,
Gary Woodfine
My Website || My Blog
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Trying to obtain text from a file.

 
0
  #7
Nov 25th, 2006
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC