not all file read.

Discussion in 'C++' started by Tony Murphy, Nov 6, 2003.

  1. Tony Murphy

    Tony Murphy Guest

    Strange problem, I read a file into a string using ifstream,
    ostringstream and string and end part of the file is missing (file
    size ~9.5kb, ~9k read). its a html file. using windows nt 4

    ------------- Not working right
    ----------------------------------------
    ifstream file(filename.c_str());
    if(!file) {
    logError("FAILURE: Opening file %s",filename.c_str());
    }
    ostringstream buffer;
    buffer << file.rdbuf();
    string dataFromFile(buffer.str());

    if(file.bad()) {
    logError("Failed to read file %s, state=%d",filename,file.rdstate());
    }

    printf("%s",dataFromFile.c_str());

    ------------------------------------------------------------------------

    then i print out the contents of file.rdbuf and the whole file is
    printed to screen.

    file.clear();
    file.seekg(0, ios::beg);
    cout << file.rdbuf() << endl;

    -------------------------------------------------------------------------

    this code only reads a portion of the file as well

    file.clear();
    file.seekg(0, ios::beg);
    char l_buffer[10240];
    memset(l_buffer,0,sizeof(l_buffer));
    file.read( l_buffer, 10240 );
    prinf("%s",l_buffer);

    -------------------------------------------------------------------------


    anyone know whats going on. if the file is smaller (~3kb), then i've
    no problems. I tried c stdlib function fread(,,,) as well, and had
    similar problem.

    anyone point me to c++ faq and a good website. i have the c++
    programming language by bjarne stroustrup, which is good, but need a
    book with good examples & more of a tutorial rather than a reference.

    thanks
    Tony Murphy, Nov 6, 2003
    #1
    1. Advertising

  2. Tony Murphy

    Ron Natalie Guest

    "Tony Murphy" <> wrote in message news:...

    > printf("%s",dataFromFile.c_str());


    Does the file by any chance include a null byte? That will cause the printf
    to stop there.

    How about:
    cout.write(dataFromFile.data(), dataFromFile.size());
    as a test.

    DId you look at the size of the string? It may n ot be the reading of the file
    that is broken but your printing it out again.
    Ron Natalie, Nov 6, 2003
    #2
    1. Advertising

  3. Tony Murphy

    Tony Murphy Guest

    you are right ron, there is a null byte in the file. now must go and
    find out how it got there, how to find it and remove it. i reckon the
    difference in file size is related to carriage return/linefeed

    "Ron Natalie" <> wrote in message news:<3faa828f$0$75672$>...
    > "Tony Murphy" <> wrote in message news:...
    >
    > > printf("%s",dataFromFile.c_str());

    >
    > Does the file by any chance include a null byte? That will cause the printf
    > to stop there.
    >
    > How about:
    > cout.write(dataFromFile.data(), dataFromFile.size());
    > as a test.
    >
    > DId you look at the size of the string? It may n ot be the reading of the file
    > that is broken but your printing it out again.
    Tony Murphy, Nov 7, 2003
    #3
  4. Tony Murphy

    Tony Murphy Guest

    problem seems to be caused by mixing const char * and string and
    assignment, haven't go to the bottom of it yet.

    is
    string x(const char*) a valid constructor?

    file is been read correctly into the string, the problem surfaces when
    i use string.c_str(), string.data() is ok. i now don't think there is
    a null terminator anywhere in the file
    Tony Murphy, Nov 7, 2003
    #4
    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. keithb
    Replies:
    2
    Views:
    7,990
    keithb
    Jun 7, 2006
  2. Holger
    Replies:
    1
    Views:
    338
    Holger
    Nov 7, 2008
  3. Sreejith K
    Replies:
    24
    Views:
    990
    Sreejith K
    Mar 24, 2009
  4. Alex Dowad
    Replies:
    4
    Views:
    258
    Michel Demazure
    May 1, 2010
  5. john.swilting
    Replies:
    0
    Views:
    93
    john.swilting
    Mar 21, 2007
Loading...

Share This Page