Click here to Skip to main content
15,909,747 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: "The ultimate grid" - adding new rows to a grid Pin
followait5-Jul-08 14:49
followait5-Jul-08 14:49 
QuestionRe: "The ultimate grid" - adding new rows to a grid Pin
Sternocera6-Jul-08 0:05
Sternocera6-Jul-08 0:05 
AnswerRe: "The ultimate grid" - adding new rows to a grid Pin
followait6-Jul-08 2:04
followait6-Jul-08 2:04 
AnswerRe: "The ultimate grid" - adding new rows to a grid Pin
Graham Shanks6-Jul-08 10:44
Graham Shanks6-Jul-08 10:44 
QuestionHow could this be? Pin
followait5-Jul-08 4:16
followait5-Jul-08 4:16 
AnswerRe: How could this be? Pin
David Crow5-Jul-08 4:48
David Crow5-Jul-08 4:48 
Answerthe thread could be finished [modified] Pin
followait5-Jul-08 13:57
followait5-Jul-08 13:57 
QuestionLarge File problem Pin
Ubik K5-Jul-08 2:45
Ubik K5-Jul-08 2:45 
Dear All

I'm having a spot of trouble - I'm hoping someone out there with a bigger brain than me might be able to help Big Grin | :-D

I am trying to read a file and filter the contents into an output file. The input file is about 1.5Gb.

My code chugs away neatly doing it's job until it hits about 12% completion, then simply hangs or crashes! I have changed from the fstream.h to the windows.h to avoid the larger file issues associated there, but it still appears to give me problems. I was expecting problems over 4Gb, but the source text file is way smaller than that.

Anyway, here's the code for you - Again, any help is really apreciated.

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

char * getEmail(char * str)
{

        char * email;
        int atpos = 0;
        int spos = 0;
        int epos = 0;

        int length = strlen(str);

        email = new char[length];

        strcpy(email, "");

        // find @

        int x = 0;
        while(x < length)
        {
            char ch = str[x];

            if(ch == '@')
            {
                atpos = x;
            }

            x++;
        }

        if (atpos > 0)
        {
            // find spos
            bool found = false;

            spos = 0;
            x = atpos;
            while(x > 0 && !found)
            {
                char ch = str[x];

                if(ch == ',' || ch == '"')
                {
                    spos = x;
                    found = true;
                }

                x--;
            }

            // find epos
            found = false;
            epos = length;
            x = atpos;
            while(x < length && !found)
            {
                char ch = str[x];

                if(ch == ',' || ch == '"')
                {
                    epos = x;
                    found = true;
                }

                x++;
            }

            x = spos + 1;
            int i = 0;
            while(x < epos)
            {
                char ch = str[x];

                email[i] = ch;
                email[i + 1] = '\0';

                x++;
                i++;
            }
        }

        return email;
}

int main(void){

    HANDLE infileH;
    HANDLE outfileH;
    BOOL rsuccess;
    BOOL wsuccess;
    char s[10];
    DWORD numRead;
    DWORD numWriten;
    char infile[MAX_PATH] = {"datafile.txt"};
    char outfile[MAX_PATH] = {"output.txt"};

    char * email;
    char str[1024];
    int x;
    int i;
    int atcount;
    double goodemails = 0;
    double bademails = 0;

    DWORD pc = 0;
    DWORD c = 0;
    DWORD t = 0;

    DWORD sizeLo, sizeHi;
    
    cout << "Starting email extraction...\n\n";

    infileH = CreateFile(infile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
    if(infileH == INVALID_HANDLE_VALUE)
    {
        cout << "Error #" << GetLastError() << " occured on file open." << "\n";
    }

    sizeLo = GetFileSize(infileH, &sizeHi);

    //cout << "Size (low 32 bits) = " << sizeLo << "\n";
    //cout << "Size (high 32 bits) = " << sizeHi << "\n";

    t = sizeLo;

    outfileH = CreateFile(outfile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
    if(outfileH == INVALID_HANDLE_VALUE)
    {
        cout << "Error #" << GetLastError() << " occured on file open." << "\n";
    }

    x = 0;
    do{
        rsuccess = ReadFile(infileH, s, 1, &numRead, 0);
        
        s[numRead] = 0;

        
        str[x] = s[0];
        str[x + 1] = '\0';
        

        if(str[x] == '\n')
        {
            email = new char[1024];
            email = getEmail((char*)str);
            strcat(email, "\r\n");

            // validate @ count
            i = 0;
            atcount = 0;
            while(i < (int)strlen(email))
            {
                if(email[i] == '@') atcount++;
                i++;
            }
            
            if(atcount == 1)
            {
                wsuccess = WriteFile(outfileH, email, strlen(email), &numWriten, 0);

                c = c + strlen(str);

                pc = (DWORD)(((float)c / (float)t) * 100.0);

                cout << pc << "% complete: " << email;

                goodemails++;
            }else{
                c = c + strlen(str);

                pc = (DWORD)(((float)c / (float)t) * 100.0);

                cout << pc << "% complete: " << email;

                bademails++;
            }

            x = 0;
        }
        x++;
        

    }while(numRead > 0 && rsuccess && wsuccess);

    CloseHandle(outfileH);

    CloseHandle(infileH);
    
 
    cout << "\n\n100% email extraction complete.\n\n";
    cout << "Number of extracted emails: " << goodemails << "\n";
    cout << "Number of bad emails removed: " << bademails << "\n";

    return 0;
}
</iostream.h></windows.h>

QuestionRe: Large File problem Pin
David Crow5-Jul-08 4:55
David Crow5-Jul-08 4:55 
AnswerRe: Large File problem Pin
Joe Woodbury5-Jul-08 11:24
professionalJoe Woodbury5-Jul-08 11:24 
GeneralRe: Large File problem Pin
Ubik K5-Jul-08 15:44
Ubik K5-Jul-08 15:44 
GeneralRe: Large File problem Pin
David Crow5-Jul-08 16:11
David Crow5-Jul-08 16:11 
GeneralRe: Large File problem Pin
Ubik K6-Jul-08 3:10
Ubik K6-Jul-08 3:10 
Questionmodeless child dialog would not close after its parent is destroy Pin
followait5-Jul-08 2:32
followait5-Jul-08 2:32 
AnswerRe: modeless child dialog would not close after its parent is destroy Pin
User 24844375-Jul-08 8:44
User 24844375-Jul-08 8:44 
GeneralRe: modeless child dialog would not close after its parent is destroy Pin
followait5-Jul-08 13:16
followait5-Jul-08 13:16 
GeneralRe: modeless child dialog would not close after its parent is destroy Pin
User 24844376-Jul-08 0:41
User 24844376-Jul-08 0:41 
GeneralRe: modeless child dialog would not close after its parent is destroy Pin
followait6-Jul-08 1:58
followait6-Jul-08 1:58 
QuestionChanging name of exe Pin
VCProgrammer5-Jul-08 1:50
VCProgrammer5-Jul-08 1:50 
AnswerRe: Changing name of exe Pin
Gary R. Wheeler5-Jul-08 4:36
Gary R. Wheeler5-Jul-08 4:36 
QuestionHow to catch out of memory allocation in C? Pin
Gofur Halmurat5-Jul-08 1:27
Gofur Halmurat5-Jul-08 1:27 
AnswerRe: How to catch out of memory allocation in C? Pin
Saurabh.Garg5-Jul-08 4:07
Saurabh.Garg5-Jul-08 4:07 
AnswerRe: How to catch out of memory allocation in C? Pin
Mark Salsbery5-Jul-08 9:08
Mark Salsbery5-Jul-08 9:08 
GeneralOnly if you've been a good dog. Pin
CPallini5-Jul-08 10:16
mveCPallini5-Jul-08 10:16 
GeneralRe: Only if you've been a good dog. Pin
Mark Salsbery5-Jul-08 10:21
Mark Salsbery5-Jul-08 10:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.