LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2005, 09:32 PM   #1
rmartine
Member
 
Registered: Dec 2002
Location: San Luis Obispo, CA
Distribution: Fedora Core 3
Posts: 618

Rep: Reputation: 30
Dumb File I/O Question


Hello all.

Can anyone here tell me what I'm doing wrong. I'm reading 30 bytes into a buffer, modifying it, then putting the modified 30 bytes back into the file.

My first modification is getting written over and over again.

Example:

Code:
while(current_frame < end_frame) {
      // Read the current frame into the buffer
      fread(frame_buf, FRAME_SIZE, 1, fp);

      // Do something to it
      crc9(frame_buf)

      // Write the modified frame to the file
      fwrite(frame_buf, FRAME_SIZE, 1, fp);

      // Update the frame address for the next frame
      frame_address += FRAME_SIZE;

      // Move the file position to the next frame
      fseek(fp, frame_address, SEEK_SET);

      // Update the current frame loop index
      current_frame++;
}
The loop is incrementing fine, I'm moving around the file just fine. The first 30 bytes I read is being written to the file over and over again. If I comment the fwrite() out and display the contents of the buffer it looks good. It's like the first call to fread() isn't doing anything.

Thanks!
 
Old 06-18-2005, 04:19 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
fread() increments the file pointer with FRAME_SIZE, so the fwrite() starts writing in the second block ("frame").

Then, after fwrite(), you set the file pointer (with fseek()) to the start of the second block. The the loop starts again reading the second block. This second block was just written to by fwrite() with the changed data from the first block.

You need the fseek() after fread(), but before the fwrite().
Code:
while(current_frame < end_frame) {
      // Read the current frame into the buffer
      fread(frame_buf, FRAME_SIZE, 1, fp);

      // Do something to it
      crc9(frame_buf)

      // Reset the file position to the frame just read
      // to overwrite it.
      fseek(fp, frame_address, SEEK_SET);

      // Write the modified frame to the file
      fwrite(frame_buf, FRAME_SIZE, 1, fp);

      // Update the frame address for the next frame
      frame_address += FRAME_SIZE;

      // Update the current frame loop index
      current_frame++;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Just a dumb question melinda_sayang General 11 04-27-2004 06:09 AM
dumb a*s question Jetta-GT- Linux - Newbie 3 01-08-2004 05:58 AM
very dumb question about a .pl file mercutio Linux - Newbie 7 09-05-2003 11:01 PM
Really dumb question but how do I edit a config file ? gavinc Linux - Newbie 2 08-19-2003 01:14 PM
A really dumb question jkcunningham Linux - General 6 07-20-2002 11:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration