开发者

fwrite function not working

I am trying to write a simple program that opens the content of a binary file into a buffer and the searches that buffer for the occurrence of string which is '+' characters. It seems like to does find these characters even though my algorithm is a little flaky. Once it finds them it changes the character to some other character. Then it writes the same buffer back to file. It does not work as I tried opening the file in a hex editor and could not find the new string with the new characters. The file that gets modified prints out the string that I am trying to modify using fread and fwrite.

Here is the code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

using namespace std;

char XyZ[] = "++++++++++++++++++++++++++";    //26 count

int main()
{
    int error = 0;
    FILE * pFile;
    unsigned int lSize;
    char * buffer;
    size_t result;
    int i = 0, j = 0;
    bool bfound = false;
    int count = 0;
    int startLocation = 0;

    pFile = fopen ( "E:\\DEVS\\Projects\\JustTesting\\FOpen\\Release\\Test.exe",   "rb+" );
    if (pFile==NULL) 
    {
        cout << "Unable to open Test.exe" << endl;   
        cout << GetLastError() << endl;      
    }
    else
    {
        cout << "Successfully opened the file" << endl;

        // obtain file size:
        fseek (pFile , 0 , SEEK_END);
        lSize = ftell (pFile);
     开发者_如何转开发   rewind (pFile);

        cout << "Number of file size is " << lSize << endl;

        // allocate memory to contain the whole file:
        buffer = (char*) malloc (sizeof(char)*lSize);
        if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

        // copy the file into the buffer:
        result = fread (buffer,1,lSize,pFile);

        cout << "Total bytes read into our buffer is " << result << endl;
        if (result != lSize) 
        {
            cout << "Error in reading the file" << endl;
        }
        else
        {
            for(i = 0; i < lSize; i++)
            {
                if(buffer[i] == '+')    // '+'
                {
                    //cout << "Found first one" << endl;

                    startLocation = i;
                    //j = i + 1;
                    //cout << "Found the next one" << endl;
                    while(buffer[++i] == '+')
                    {
                        buffer[i] = '@';

                        cout << "Found the next one" << endl;
                        count++;
                    }
                }               
            }
            cout << "Found our string with count " << count << endl;

        }
        //for(int k = startLocation; k < startLocation + 5; k++)
        //  buffer[k] = '@';

        int value = fwrite (buffer , 1 , lSize , pFile );
        cout << "bytes written to file is :" << value << endl;
        fclose (pFile);
        free (buffer);
    }
    return 0;
}


The first problem is your while:

while(buffer[++i] == '+')

So you've found your +, but in the while you first increase the position and then test whether the value is still +. That fails if you only have one + (and if you have several, the first is not overwritten). Instead, replace it with:

for ( ; (buffer[i] == '+') && (i < lSize) ; i++)

The next problem is that after fread, the file position is at the end. Your call to fwrite will thus append the modified buffer. To actually overwrite the file you first need to call rewind(pFile); again.

A final note on fread/fwrite: you read and write lSize items, each 1 byte in size. That's pretty slow (try it with a one megabyte file). Instead you likely want the opposite:

result = fread(buffer, lSize, 1, pFile);

Note that result will then merely have 1 on success. The same applies for fwrite.


Note that you would be trying to write the modified contents at the end of the file. Besides, switching between read and write operations need certain function calls in between to switch the operation mode. Try adding

fseek( pFile, 0, SEEK_SET );

right before doing fwrite. Note that there may still be something wrong


You open the file in rb (for reading), then write to it... I am not sure it will do what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜