Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi all

I am having 30 lines in a text file.
Now i want to print top 15 line first from the file.after printing it will show a message box and after clicking ok button it will print the remaining 15 lines.

Below is my printing function

C#
public void Print_Trans(string filePath)
       {
           UInt32 len = 0;
           StringBuilder buffer = new StringBuilder();
           string[] data = new string[100];
           buffer = buffer.Append(File.ReadAllText(filePath));
           buffer = buffer.Append("\r");
           len = Convert.ToUInt32((buffer.Length));
           data = File.ReadAllLines(filePath);
           noOfLines = data.Length;
           PrtSetFont(10002);
           System.IO.StreamReader file =
               new System.IO.StreamReader(@"C:\Display.txt");
           string[] liness = File.ReadAllLines(@"C:\Display.txt");
           var firstValue = "";
           var SValue = "";
           var TValue = "";
           var FValue = "";
           var FiValue = "";
           var SiValue = "";
           var SeValue = "";
           status = PrtWrite("", len);
           string line111 = "   Date                    Particulars                              ChequeNo     Withdrawals          Deposite          Balance ";
           status = PrtWrite(line111, len);
           PrtPosRelV(1);
           string line222 = "   ---------------------   ---------------------------------      -----------    ---------------         -----------         -----------";
           status = PrtWrite(line222, len);
           PrtPosRelV(1);



           foreach (var lines in liness)
           {
                    if (lines == null || lines.Length < 1)
                    {
                   PrtPosRelV(1);
                    }
                    else
                    {

                   firstValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[0];
                   if (firstValue == null)
                   {
                       firstValue = "";
                   }
                   SValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[1];
                   if (SValue == null)
                   {
                       SValue = "";
                   }
                   TValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[2];
                   if (TValue == null)
                   {
                       TValue = "";
                   }
                   FValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[3];
                   if (FValue == null || FValue == "")
                   {
                       FValue = "0";
                   }
                   FiValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[4];
                   if (FiValue == null)
                   {
                       FiValue = "";
                   }
                   SiValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[5];
                   if (SiValue == null)
                   {
                       FiValue = "";
                   }
                   SeValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[6];
                   if (SeValue == null)
                   {
                       SeValue = "";
                   }
                   int leng = 0;
                   int sp = 0;
                   PrtPosAbsH(5);
                   status = PrtWrite(firstValue, len);
                   PrtPosAbsH(10);
                   status = PrtWrite(SValue, len);
                   PrtPosAbsH(30);
                   status = PrtWrite(TValue, len);
                   PrtPosAbsH(75);
                   status = PrtWrite(FValue, len);
                   leng = FiValue.Length;
                   if (leng > 3)
                   {
                       sp = app_sp(leng);
                       int tot = 99;
                       int rem = tot - (92 + sp);
                       PrtPosAbsH(92 + sp);
                   }
                   status = PrtWrite(FiValue, len);
                   leng = SiValue.Length;
                   if (leng > 3)
                   {
                       sp = app_sp(leng);
                       int tot = 120;
                       int rem = tot - (112 + sp);
                       PrtPosAbsH(112 + sp);
                   }
                   status = PrtWrite(SiValue, len);
                   leng = SeValue.Length;
                   if (leng > 3)
                   {
                       sp = app_sp(leng);
                       int tot = 134;
                       int rem = tot - (127 + sp);
                       PrtPosAbsH(135 + sp);
                   }
                   status = PrtWrite(SeValue, len);
                   PrtPosRelV(1);
               }
           }
       }


Please tell me how to do this.

Thank you
Posted
Updated 9-Nov-14 16:40pm
v2

you can use File.ReadLines Method [^]
C#
var firstlineSet = File.ReadLines(fileName)
            .Take(15)   // limit to first 15
            .ToArray(); 

C#
var secondlineSet = File.ReadLines(fileName)
            .Skip(15)   // skip first 15 and take the rest
            .ToArray(); 


UPDATE:
C#
Print_Trans(firstlineSet);
//show your message
Print_Trans(secondlineSet);

you need to change the print method to accept string array to print
C#
public void Print_Trans(string[] lines)
{
   // use above lines to print inside your method 
}
 
Share this answer
 
v4
Comments
Black_Rose 9-Nov-14 22:41pm    
I have updated my question with my printing function.please tell me where to use this.
DamithSL 9-Nov-14 22:48pm    
I have given outline for the work, try yourself.
I would approach this problem by writing code I could re-use. My goal would be:

1. read the file only once

2. produce a useful collection of "pieces" of the file based on the number of lines to read into each piece, and the number of pieces to be produced.

a. 'chunk: a List of string whose size is the number of lines the user specifies per chunk.

b. 'lineChunks: a collection of chunks.

3. deal in some way with the file being too "short" to read even one chunk, or the number of lines in the file not being integrally divisible by the chunk size.
C#
private string filePath = @"C:\Users\YourUserName\Desktop\thirty.txt";

private List<list><string>> getLines(string fPath, int nChunks, int linesPerChunk)
{
    var Lines = File.ReadLines(fPath);

    var lineChunks = new List<list><string>>();

    for (int i = 0; i < nChunks; i++)
    {
        var chunk = Lines.Skip(i * linesPerChunk).Take(linesPerChunk).ToList();
        
        if(! (chunk.Count == 0)) lineChunks.Add(chunk);
    }

    return lineChunks;
}
</string></list></string></list>
To test this I created a Text file named 'thirty.txt' that had 30 lines; each line's content was a number from 1~30.
C#
// tests

var chunkCollection1 = getLines(filePath, 2, 15);
// returns a List with two elements: 
// chunkCollection1[0] has lines 1-15
// chunkCollection1[1] has lines 16-30

var chunkCollection2 = getLines(filePath, 2, 30);
// returns a List with one element: 
// chunkCollection2[0] has lines 1-30

var chunkCollection3 = getLines(filePath, 2, 20);
// returns a List with two elements: 
// chunkCollection3[0] has lines 1-20
// chunkCollection3[1] has lines 21-30

var chunkCollection4 = getLines(filePath, 2, 59);
// returns a List with one element: 
// chunkCollection4[0] has lines 1-30

var chunkCollection5 = getLines(filePath, 2, 0);
// returns an empty List
 
Share this answer
 
Hello friend,
This is an interesting question. To be honest I have never used this, but had bookmarked some tickets for this.
SO LINK[^]
SO LINK[^]
Hope these help you...
Thanks..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900