Incorrect length output in File read in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prads
    New Member
    • Oct 2007
    • 25

    Incorrect length output in File read in C++

    Hello,
    The pgm below opens a file, reads it and displays the contents and its length.
    However it displays a length of (length+2) and also the samplesread as (samplesread +2), where samplesread is the length obtaines from gcount(), and length from tellg(). Pls tell me how i should get the correct length and samplesread??. Also if i enter the data in my file in the next line, it considers "\n" towards the length count. Pls correct my code.
    Thanks,
    Prads
    [code=cpp]

    #include <iostream>
    #include <fstream>
    using namespace std;

    int main ()
    {
    int length,samplesr ead,blksize=30;
    char *rawsignal;

    ifstream acq_data; //acq_data is the fid
    acq_data.open ("C:\\t1\\p.bin ", ios::binary);
    acq_data.seekg (0, ios::end);
    length = acq_data.tellg( ); // check this out
    acq_data.seekg (0, ios::beg);
    rawsignal= new char [length];
    acq_data.read (rawsignal,leng th);
    samplesread=acq _data.gcount(); // check this out
    cout<<"the length is"<<length<<en dl;
    cout<<"the number of samples read is"<<samplesrea d<<endl;
    acq_data.close( );
    cout.write (rawsignal,leng th);
    delete[] rawsignal;
    getchar();
    return 0;
    }

    [/code]
    Last edited by sicarie; Nov 7 '07, 02:13 PM. Reason: Non-formatted code annoys me
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I don't believe tellg() and gcount() work with binary.

    Comment

    • prads
      New Member
      • Oct 2007
      • 25

      #3
      Originally posted by weaknessforcats
      I don't believe tellg() and gcount() work with binary.
      oh!...so is there any way out......i.e any other cmnd tat works well with .bin files.

      Comment

      • PuzzleC
        New Member
        • Nov 2007
        • 9

        #4
        In binary mode symbol '\n' to ignore, I m working to binary file a little and no problem.

        in this mode another way of looking than text mode
        end of line as '\n' not be effective, it is necessary to sizeof method :)

        Comment

        • prads
          New Member
          • Oct 2007
          • 25

          #5
          im sorry but didnt understand a word!!! :)

          Comment

          • PuzzleC
            New Member
            • Nov 2007
            • 9

            #6
            :)
            to show example?

            Comment

            Working...