problem reading text file with getlin()

Discussion in 'C++' started by fabrice, Sep 15, 2003.

  1. fabrice

    fabrice Guest

    Hello,

    I've got trouble reading a text file (event viewer dump) by using the
    getline() function...

    After 200 - 300 lines that are read correctly, it suddenly stops
    reading the rest of the file...

    Thank you to all of you who can help me with this one...


    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string>

    int main () {

    int MAX_LENGTH = 10240*10240;
    char buffer[1000];
    char content[1000];
    int result;
    int newresult;
    int newresult_bis;
    int newresult2;
    int getpart;
    int check;

    ifstream notes2 ("c://Data//Utility//Share//Auditing_notes_services//sec_dump.txt");
    ofstream putdata("c://Data//Utility//Share//Auditing_notes_services//result_notes.txt",
    ios::app);

    // lire le fichier sec_dump.txt ligne par ligne - THIS IS THE DUMP
    FILE WHERE THE PROGRAM STOPS READING IN THE MIDDLE


    while( ! notes2.eof() ) {

    char *content2;
    content2 = new char[MAX_LENGTH];


    notes2.getline (content2,MAX_LENGTH);
    _strlwr(content2);

    string input2 = content2;

    newresult_bis = input2.find("nt authorit");
    if(newresult_bis != string::npos) {
    continue;
    }

    ifstream notes ("c://Data//Utility//Share//Auditing_notes_services//source_rep.txt");

    // Lire les nom de fichier dans le repertoir ligne par ligne

    while (! notes.eof() ) {


    notes.getline (buffer,1000);
    strcpy(content, buffer);
    _strlwr(content);
    string input = content;

    result = input2.find(input);

    newresult = input2.find("object");
    string first = input2.substr(0, newresult);

    newresult2 = input2.find("dom206");
    string first2 = first.substr(newresult2, 14);

    // check pour ne pas ecrire de doublons

    if(result != string::npos) {

    FILE * pFile;
    long lSize;
    char * buffer4;

    pFile = fopen (
    "c://Data//Utility//Share//Auditing_notes_services//result_notes.txt"
    , "rb" );
    if (pFile==NULL) exit (1);

    // obtain file size.
    fseek (pFile , 0 , SEEK_END);
    lSize = ftell (pFile);
    rewind (pFile);

    // allocate memory to contain the whole file.
    buffer4 = (char*) malloc (lSize);
    if (buffer4 == NULL) exit (2);

    // copy the file into the buffer.
    fread (buffer4,1,lSize,pFile);
    string temporary = buffer4;

    /*** the whole file is loaded in the buffer. ***/

    string whole = first2 + ";" + input;

    check = temporary.find(whole);

    if((check == -1) && (first2 != "dom206\\adminis")) {

    putdata << first << input << endl;

    }

    }



    }

    }


    }
    fabrice, Sep 15, 2003
    #1
    1. Advertising

  2. fabrice wrote:

    > Hello,
    >
    > I've got trouble reading a text file (event viewer dump) by using the
    > getline() function...
    >
    > After 200 - 300 lines that are read correctly, it suddenly stops
    > reading the rest of the file...
    >
    > Thank you to all of you who can help me with this one...


    Please note that this group is for discussion of the *standard C++
    language*. If you want to use old, ARM-style C++ you might have to go
    somewhere else for help. But first you should consider updating to
    modern C++.

    >
    >
    > #include <iostream.h>
    > #include <fstream.h>


    Neither of these exist in standard C++. Standard C++ uses <iostream> and
    <fstream>

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


    These are deprecated in standard C++. The replacements are <cstdlib> and
    <cstdio>. That said, I still use these versions because my compiler
    incorrectly places names from the <c*> headers in the global namespace,
    therefore I have to use the deprecated headers if I want my code to be
    standard and still compile. :/

    Mixing C & C++ I/O might be a bad idea, also.

    > #include <string>


    Mixing old & new C++ headers could lead to problems.

    >
    > int main () {
    >
    > int MAX_LENGTH = 10240*10240;


    Did you mean:

    const int MAX_LENGTH = 10240*10240;

    ?

    The result of that multiplication (104,857,600) cannot portably fit in
    an int object (int may only be able to store values up to 32,767).
    Better change it to

    long MAX_LENGTH = 10240L * 10240L;

    Personally, I would advise against the use of all-uppercase here. That
    is traditionally used for macros (which in turn were traditionally used
    for constants). This served as a warning to the person using the
    identifier, since macros can be dangerous. const objects do not suffer
    from the problems associated with macros, therefore this warning is
    unnecessary.

    > char buffer[1000];
    > char content[1000];


    I'd strongly recommend replacing these with std::strings.

    > int result;
    > int newresult;
    > int newresult_bis;
    > int newresult2;
    > int getpart;
    > int check;
    >
    > ifstream notes2 ("c://Data//Utility//Share//Auditing_notes_services//sec_dump.txt");
    > ofstream putdata("c://Data//Utility//Share//Auditing_notes_services//result_notes.txt",
    > ios::app);


    In standard C++, the names are std::ifstream and std::eek:fstream. If you
    decide to update you'll need to either use these fully-qualified names
    or add appropriate 'using' statements. For example, you could do this at
    the top of the file, immediately after the #includes:

    using namespace std;

    Also, the strings you are passing in to identify the files are highly
    suspect. (Note the doubled forward-slashes. If those were back-slashes
    instead, it would sort of make sense.) I don't know if those are legal
    pathnames on Windows, and you probably shouldn't use full pathnames
    anyway (if you expect the program to work on a computer other than yours).

    >
    > // lire le fichier sec_dump.txt ligne par ligne - THIS IS THE DUMP
    > FILE WHERE THE PROGRAM STOPS READING IN THE MIDDLE
    >
    >
    > while( ! notes2.eof() ) {


    Two serious problems here. First, you didn't bother to check whether the
    file opened correctly.

    Second, reading input this way in C++ is almost always a mistake. The
    eof flag will only be set AFTER a failed attempt to read PAST THE END of
    the file. Therefore, you may arrive at this test when your file has no
    data left, but the test will still succeed, and the loop will still
    execute, attempting to read and use data from a file that has no data.

    >
    > char *content2;
    > content2 = new char[MAX_LENGTH];


    Why are you using 'new' here?

    char contents2[MAX_LENGTH];

    should work just fine. Even better, use

    std::string contents2;

    >
    >
    > notes2.getline (content2,MAX_LENGTH);


    If you change to std::strings, you can do

    std::getline(notes2, contents2);

    here, which is much easier and automatically makes contents2 the right size.

    > _strlwr(content2);


    This is not a standard function. If it's one of yours, then it's
    probably invading the implementation's namespace. Identifiers beginning
    with '_' followed by a lowercase letter are reserved for the
    implementation in the global namespace.

    If this function is part of the implementation then it's fine, but not
    portable.

    >
    > string input2 = content2;


    It's called 'std::string'. You need either the fully-qualified name or
    an appropriate 'using' statement.


    That's about all I can be bothered to look at right now. General advise:

    * Use modern C++. Lose the char* and char[] strings, use std::strings
    instead.

    * Don't cram so much into a single function - it complicates code and
    breeds bugs. Shoot for functions that take up no more than one or two
    screens full of text. Try breaking this up into 2 or 3 functions.

    * Don't mix C and C++ I/O. It's messy and unnecessary.

    * Fix your pathnames (better yet, get rid of them).

    * When posting here, try to leave out all the non-standard stuff. We
    can't help you with it anyway.

    * When posting here, minimize the amount of code you post. Give us the
    smallest complete program that demonstrates the problem you are having.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Sep 15, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    669
    Kevin Spencer
    Nov 11, 2004
  2. Replies:
    0
    Views:
    778
  3. Replies:
    1
    Views:
    449
    Keith Thompson
    Apr 12, 2005
  4. Lionel
    Replies:
    22
    Views:
    645
    Steve Holden
    Feb 3, 2009
  5. Robin Wenger
    Replies:
    191
    Views:
    3,219
Loading...

Share This Page