LAppling7775
asked on
Page count
Below is code I am using to get page counts of txt files. The line char count can only be 132 char, the total lines per page is 68, there is a char 112 for form feed and a char 10 and could be char 13.
Using this code - I do not get an acurate count of pages. And something else that is happening is that I will see different counts on different PC's. Which does not make sense because the code is the same.
Is there a way to get an accurate count of pages????
public int GetPageCount()
{
int nextChar;
int charCount = 0;
// These counts are initially 1
int lineCount = 1;
int pageCount = 1;
System.IO.StreamReader streamReader = new System.IO.StreamReader(_fi lePath);
while (streamReader.Peek() > -1)
{
nextChar = streamReader.Read();
charCount ++;
// Check for form feed (12) or 68 lines
if (nextChar == 12 || lineCount == 68)
{
pageCount++;
lineCount = 0;
charCount = 0;
continue;
}
// Check for a newLine (10) or 132 characters
else if(nextChar == 10 || charCount == 132)
{
lineCount++;
charCount = 0;
continue;
}
}
// Close StreamReader
streamReader.Close();
// Increment pageCount by one before returning it's value
return pageCount;
}
Using this code - I do not get an acurate count of pages. And something else that is happening is that I will see different counts on different PC's. Which does not make sense because the code is the same.
Is there a way to get an accurate count of pages????
public int GetPageCount()
{
int nextChar;
int charCount = 0;
// These counts are initially 1
int lineCount = 1;
int pageCount = 1;
System.IO.StreamReader streamReader = new System.IO.StreamReader(_fi
while (streamReader.Peek() > -1)
{
nextChar = streamReader.Read();
charCount ++;
// Check for form feed (12) or 68 lines
if (nextChar == 12 || lineCount == 68)
{
pageCount++;
lineCount = 0;
charCount = 0;
continue;
}
// Check for a newLine (10) or 132 characters
else if(nextChar == 10 || charCount == 132)
{
lineCount++;
charCount = 0;
continue;
}
}
// Close StreamReader
streamReader.Close();
// Increment pageCount by one before returning it's value
return pageCount;
}
ASKER
How would that help. I am interrupting char 12 to be new line. what would Char 112 do?
oops - just a cursory glance at the text confused me: you said 112 in the text but, of course, that's not 0x0C.
OK - I looked dmore closely, and it looks fine. FYI, you don't need the continue statements - the if/else manages that. The only thing that strikes me as a possible source of the problem is the use of int - perhaps using Int32 would be better since this more correctly reflects the returned values, and the different machines might interpret this differently.
ASKER
Thanks - will look at the int32 and test.
Hi,
Did you try
someStrLine = myStreamReader.ReadLine() instead of search for LF?
Example:
using System;
using System.IO;
int public static CountLines()
{
int count=0;
try
{
using (StreamReader sr = new StreamReader("TestFile.txt "))
{
String line;
// Read and count lines from the file until EOF is reached
while ((line = sr.ReadLine()) != null)
{
count++;
}
}
}
return count;
}
Are the .NET framework the same version in all PCs? The different results can be from different versions.
Jose
Did you try
someStrLine = myStreamReader.ReadLine() instead of search for LF?
Example:
using System;
using System.IO;
int public static CountLines()
{
int count=0;
try
{
using (StreamReader sr = new StreamReader("TestFile.txt
{
String line;
// Read and count lines from the file until EOF is reached
while ((line = sr.ReadLine()) != null)
{
count++;
}
}
}
return count;
}
Are the .NET framework the same version in all PCs? The different results can be from different versions.
Jose
ASKER
Thanks jose - will look at your code and let you know. Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if (nextChar == 12 || lineCount == 68)
Shouldn't it be:
if (nextChar == 112 || lineCount == 68)