Solved

Code Optimization

Posted on 2003-11-03
5
269 Views
Last Modified: 2010-04-17
Hi,

I am trying to insert a LineFeed at the end of every 100th character in my file.
I am using the following code for this.
It takes a long time if the file has some 100,000 characters. So can u suggest me a idea to do it faster.


Code:

char *file;
char *file1;

unsigned long offset = 0;
char   buf[102] = {0};
HANDLE hFile, hFile1 ;
DWORD  dnbytes = 0;
DWORD  dwact = 0;
DWORD  lowsize = 0;
DWORD  hisize = 0;
int    nStartIndex = 0;


if ((hFile = CreateFile(file,
      GENERIC_READ,
      0,
      NULL,
      OPEN_EXISTING,
      FILE_FLAG_RANDOM_ACCESS,
      NULL) )
== INVALID_HANDLE_VALUE)
return 1;

lowsize = GetFileSize(hFile, &hisize);
if (lowsize == 0xffffffff)
{
CloseHandle(hFile);
return FAILURE;
}

if ((hFile1 = CreateFile(file1,
       GENERIC_WRITE,
       0,
       NULL,
       OPEN_EXISTING,
       FILE_FLAG_RANDOM_ACCESS,
       NULL) )
== INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
return FAILURE;      
}


while (1)
{
dnbytes = 100;        

if (!ReadFile(hFile,buf,dnbytes,&dwact,0))
{
    CloseHandle(hFile);
    CloseHandle(hFile1);
    return SUCCESS;
}

// insert 0x0d at the end
if (dnbytes != dwact)
{
    dnbytes = dwact;
    if (dwact != 0)
 buf[dwact]=0x0d;
    CloseHandle(hFile);
    CloseHandle(hFile1);
    return SUCCESS;
}
else
{
    buf[dnbytes]=0x0d;
}

dnbytes++;

if (!WriteFile(hFile1, &buf[nStartIndex], dnbytes, &dwact, NULL))
{
    CloseHandle(hFile);  
    CloseHandle(hFile1);  
    return FAILURE;  
}

if (dnbytes != dwact)
{
    CloseHandle(hFile);  
    CloseHandle(hFile1);  
    return FAILURE;  
}
Sleep(1);      
}  
return 0;

Thanks in Advance,
Varadha
0
Comment
Question by:Varadha2k
5 Comments
 
LVL 6

Accepted Solution

by:
stsanz earned 20 total points
ID: 9670691
Use parameter FILE_FLAG_SEQUENTIAL_SCAN in both CreateFile calls instead of FILE_FLAG_RANDOM_ACCESS.
This may improve file buffering.

0
 
LVL 5

Expert Comment

by:drnick
ID: 9670743
also, you could speed a little up when reading and writing larger blocks.
make your buffer n*100+n bytes large,
read n*100 bytes,
use memmove or RtlMoveMemory to move the buffer content like

RtlMoveMemory(&buf[n*100-1], &buf[(n-1)*100], 100)
RtlMoveMemory(&bu[(n-1)*100-2], & buf[(n-2)*100, 100)
maybe in a loop, regarding how may bytes actually read
and then write the block again
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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…
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

920 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