Solved

Page count

Posted on 2006-06-16
8
231 Views
Last Modified: 2010-04-17
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(_filePath);
      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;
}
0
Comment
Question by:LAppling7775
  • 3
  • 3
  • 2
8 Comments
 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
Is this a typo?

 if (nextChar == 12 || lineCount == 68)

Shouldn't it be:

 if (nextChar == 112 || lineCount == 68)
0
 

Author Comment

by:LAppling7775
Comment Utility
How would that help.  I am interrupting char 12 to be new line. what would Char 112 do?
0
 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
oops - just a cursory glance at the text confused me: you said 112 in the text but, of course, that's not 0x0C.
0
 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
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.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:LAppling7775
Comment Utility
Thanks - will look at the int32 and test.  
0
 
LVL 18

Expert Comment

by:JoseParrot
Comment Utility
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
0
 

Author Comment

by:LAppling7775
Comment Utility
Thanks jose - will look at your code and let you know.  Thanks
0
 
LVL 18

Accepted Solution

by:
JoseParrot earned 500 total points
Comment Utility
Hum... I only have count lines, not pages...

Better:
    int public static CountPages()
    {
        int count=0;
        int pages=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++;
                    if (count==MAXLINES or sr.Peek()==LF)    // check LF without advance reading
                    {
                        pages++;
                        count=0;
                        if (sr.Peek()==LF) sr.Read();                // advance reading
                    }
                }
            }
        }
        return pages;
    }

Jose
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now