read and write from a sector

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aditigupta
    New Member
    • Jul 2007
    • 6

    read and write from a sector

    hi

    actually i m using absread to read a particular sector but when i write it i m getting some special characters instead of actual data.......

    i m writing the data by this command

    for (i=0; i<512; i++)
    {
    ch_out = buf[i];
    fprintf(fnew1," %c",ch_out);
    }

    so can anybody tell me wat can be the problem........ ..

    is it the format s[pecifier that has to be different...... ..

    plz help
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    fprintf() formats your data according to your format string.

    Probably you need to use fwrite() instead of fprintf() to avoid the formatting.

    Comment

    • aditigupta
      New Member
      • Jul 2007
      • 6

      #3
      ya but by using fwrite will i get the data that was stored initially in that file i.e. in the same format and not binary........

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by aditigupta
        (i=0; i<512; i++)
        {
        ch_out = buf[i];
        fprintf(fnew1," %c",ch_out);
        }
        I assume ch_out is a char?? and that buf is an array of char??

        Otherwise the above loop will not work.

        Originally posted by aditigupta
        ya but by using fwrite will i get the data that was stored initially in that file i.e. in the same format and not binary........
        That's only true if you opened the file in text mode. Text mode does the same thing as your fprintf() loop: It converts everytjhing to a char. Worse, it assumes you know the value will fit in the char. Otherwise, you get rollover and a garbage result.

        I say again, use fwrite() and open the file in binary mode. That bypasses the conversion to char and lets you write a swatch of memory directly to the file without formatting.

        Comment

        Working...