Need Help Reading a file

not reading complete file

Page 1 of 1

5 Replies - 626 Views - Last Post: 06 March 2009 - 08:31 PM Rate Topic: -----

#1 nice_mak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-March 09

Need Help Reading a file

Post icon  Posted 06 March 2009 - 05:15 AM

Assalm-0-Alaikum and Hi,
i m new to C# world...learning c# through Video tutorials... i learned how to input a file by giving path of the file...but my program is not completely showing complete file means to say that whatever a .txt file contanis is not showing...this is my code
using System;
using System.IO;

class FileInput
{
	public static void Main()
	{
		string Path = null;
		

		Console.WriteLine("Please Enter The Path of the TXT File You Want See : ");
		Path = Console.ReadLine();

		StreamReader r = File.OpenText(Path);
		
		for (int i = 0; i < 20; i++)
		{
			Console.WriteLine(r.ReadLine());
			if (r.ReadLine() != null)
			{
				i = i;
			}
			else
			{
				i = 19;
			}
		}
	}

}
please help me to learn c# Thanks...

Is This A Good Question/Topic? 0
  • +

Replies To: Need Help Reading a file

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5678
  • View blog
  • Posts: 22,540
  • Joined: 23-August 08

Re: Need Help Reading a file

Posted 06 March 2009 - 05:35 AM

Are you saying it's not showing anything?
Was This Post Helpful? 0
  • +
  • -

#3 p0rkjell0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-March 09

Re: Need Help Reading a file

Posted 06 March 2009 - 06:17 PM

Um no, he said the following: "my program is not completely showing complete file...".

He is reading "some" lines -- not "all" lines...

From what I can see in the code, he is setting a maximum iteration count of 21 in his loop (0 to 20 = 21 iterations). If his text file has more than 21 lines, he will never get all lines from the file.

He could create a function that reads the lines from the file and returns a List<> of Strings that represent the lines in the file.

Example:

public List<string> ReadFileLines(string fileName)
{
   try
   {
	  List<string> allLines = new List<string>();

	  using (StreamReader reader = new StreamReader(fileName))
	  {
		 string line;
		 while ((line = reader.ReadLine()) != null)
		 {
			//
			// "line" is a line in the file. Add it to our List.
			//
			allLines.Add(line);
		 }
	  }
				
	  return allLines;
   }
   catch (IOException)
   {
	  // Bad things just happened...
	  return null;
   }
}


Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5678
  • View blog
  • Posts: 22,540
  • Joined: 23-August 08

Re: Need Help Reading a file

Posted 06 March 2009 - 07:15 PM

He's also skipping every other line:
Console.WriteLine(r.ReadLine());
if (r.ReadLine() != null)

Was This Post Helpful? 0
  • +
  • -

#5 p0rkjell0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-March 09

Re: Need Help Reading a file

Posted 06 March 2009 - 07:47 PM

Sorry.. i didn't get that far down in the code... lol
Was This Post Helpful? 0
  • +
  • -

#6 p0rkjell0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-March 09

Re: Need Help Reading a file

Posted 06 March 2009 - 08:31 PM

Sorry I didn't explain. I didn't get that far not because I couldn't... because I wouldn't...

i'm sure you understand...

yawn!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1