Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16943

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
help with opening a file and printer the last 128 bytes of it Posted by vovfisk on 13 Nov 2008 at 11:21 PM
I been trying to write a program that open the file C:\mp3.mp3 and prints the last 128 bytes of the file which would be the id3 tag v1. So far it's been an epic fail =(
Report
Re: help with opening a file and printer the last 128 bytes of it Posted by XLoom on 14 Nov 2008 at 6:18 AM
: I been trying to write a program that open the file C:\mp3.mp3 and
: prints the last 128 bytes of the file which would be the id3 tag v1.
: So far it's been an epic fail =(
:

Would something like this do the trick? (NB! I didn't write any error checking, do it yourself)
#include <stdio.h>
#include <string.h>

int main() {
  int bufLength=129;
  FILE* f=fopen("C:\\mp3.mp3", "rb");
  fseek(f, 0, SEEK_END);
  long fileLength=ftell(f);
  fseek(f, fileLength-bufLength+1, SEEK_SET);

  char buffer[bufLength];
  memset(buffer, 0, bufLength);
  fread(buffer, 1, bufLength-1, f);
  for(int i=0; i<bufLength; i++) {
    printf("%c", buffer[i]);
  }
  printf("\n");
  fclose(f);
  return 0;
}

Report
Re: help with opening a file and printer the last 128 bytes of it Posted by vovfisk on 15 Nov 2008 at 7:40 PM
: : I been trying to write a program that open the file C:\mp3.mp3 and
: : prints the last 128 bytes of the file which would be the id3 tag v1.
: : So far it's been an epic fail =(
: :
:
: Would something like this do the trick? (NB! I didn't write any
: error checking, do it yourself)
:
: 
: #include <stdio.h>
: #include <string.h>
: 
: int main() {
:   int bufLength=129;
:   FILE* f=fopen("C:\\mp3.mp3", "rb");
:   fseek(f, 0, SEEK_END);
:   long fileLength=ftell(f);
:   fseek(f, fileLength-bufLength+1, SEEK_SET);
: 
:   char buffer[bufLength];
:   memset(buffer, 0, bufLength);
:   fread(buffer, 1, bufLength-1, f);
:   for(int i=0; i<bufLength; i++) {
:     printf("%c", buffer[i]);
:   }
:   printf("\n");
:   fclose(f);
:   return 0;
: }
: 
:
:
thank you, appreciate the help



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.