not all file read.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Murphy

    not all file read.

    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("FAILU RE: Opening file %s",filename.c_ str());
    }
    ostringstream buffer;
    buffer << file.rdbuf();
    string dataFromFile(bu ffer.str());

    if(file.bad()) {
    logError("Faile d to read file %s, state=%d",filen ame,file.rdstat e());
    }

    printf("%s",dat aFromFile.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_buf fer));
    file.read( l_buffer, 10240 );
    prinf("%s",l_bu ffer);

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


    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
  • Ron Natalie

    #2
    Re: not all file read.


    "Tony Murphy" <tony_murphy@ya hoo.com> wrote in message news:aa9724e3.0 311060911.37125 d61@posting.goo gle.com...
    [color=blue]
    > printf("%s",dat aFromFile.c_str ());[/color]

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

    How about:
    cout.write(data FromFile.data() , dataFromFile.si ze());
    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.


    Comment

    • Tony Murphy

      #3
      Re: not all file read.

      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" <ron@sensor.com > wrote in message news:<3faa828f$ 0$75672$9a6e19e a@news.newshost ing.com>...[color=blue]
      > "Tony Murphy" <tony_murphy@ya hoo.com> wrote in message news:aa9724e3.0 311060911.37125 d61@posting.goo gle.com...
      >[color=green]
      > > printf("%s",dat aFromFile.c_str ());[/color]
      >
      > Does the file by any chance include a null byte? That will cause the printf
      > to stop there.
      >
      > How about:
      > cout.write(data FromFile.data() , dataFromFile.si ze());
      > 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.[/color]

      Comment

      • Tony Murphy

        #4
        Re: not all file read.

        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

        Comment

        Working...