| | |
Trying to obtain text from a file.
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 2
Reputation:
Solved Threads: 0
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:
The method above works fine. The second method below is what is causing the error message below:
I am not sure really what is wrong can anyone suggest any ideas? Thanks.
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"); } }
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(); }
Last edited by tgreer; Nov 15th, 2006 at 11:32 am. Reason: added code tags
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
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.
•
•
Join Date: Nov 2006
Posts: 2
Reputation:
Solved Threads: 0
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:
This compiles fine but does not do anything. Can you provide a bit more clarification please.
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.
http://www.csharp-station.com/HowTo/...eTextFile.aspx
swap the Console.WriteLine for lbLines.Items.Add I guess
C# Syntax (Toggle Plain Text)
using System; using System.IO; class TextFileReader { static void Main(string[] args) { // create reader & open file TextReader tr = new StreamReader("C:\\data2.txt"); // read a line of text Console.WriteLine(tr.ReadLine()); // close the stream tr.Close(); } }
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*
•
•
•
•
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:
This compiles fine but does not do anything. Can you provide a bit more clarification please.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(); }
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
•
•
Join Date: Mar 2004
Posts: 1
Reputation:
Solved Threads: 0
I think your problem is the way you were trying ot use the StreamReader object
Please see below, for some pointers
Hope this helps,
Please see below, for some pointers
Hope this helps,
C# Syntax (Toggle Plain Text)
class Program { static void Main(string[] args) { using( System.IO.FileStream fsOut = System.IO.File.Create("c:\\TestText.txt")) using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fsOut)) { string[] sStuff = {"Some Text" , "Test", "Bravo"}; for (int i = sStuff.GetLowerBound(0); i <= sStuff.GetUpperBound(0); i++) { sw.WriteLine(sStuff[i]); } sw.Flush(); } using (System.IO.StreamReader sr = new System.IO.StreamReader(new System.IO.FileStream("c:\\TestText.txt", System.IO.FileMode.Open))) //using( System.IO.StreamReader sr = new System.IO.StreamReader(fsRead)) { while (sr.Peek() > -1) { Console.WriteLine( sr.ReadLine()); } } Console.ReadLine(); } }
Last edited by cykophysh; Nov 24th, 2006 at 7:49 pm. Reason: removed some irrelevant text
![]() |
Similar Threads
- Writing text from textbox to file... (C++)
- Need Help in Reading characters from a text file (C++)
- Output to text (notepad) file (HTML and CSS)
Other Threads in the C# Forum
- Previous Thread: SQL DataReader
- Next Thread: Project Ideas Required For Msc dissertation
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox concurrency control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ globalization httpwebrequest image index input install java label list listbox listener mandelbrot math microsoftc#visualexpress mouseclick mysql networking operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






