Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.17 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C: how to print content of file after seek
  2. #include <stdio.h>
  3.  
  4. int main(int argc, char *argv[]){
  5.   FILE *fr;
  6.  
  7.   if (fr = fopen (argv[1], "r")){
  8.     fseek(fr, 100, SEEK_CUR);
  9.  
  10.     char c[1];
  11.     while (fread(c, 1, sizeof(c),fr) > 0)
  12.         printf("%s", c);
  13.  
  14.     fclose(fr);
  15.   }
  16.   else{
  17.     perror("File does not exist");
  18.   }
  19.  
  20. }
  21.        
  22. #include <stdio.h>
  23. #include <sys/stat.h>
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27.     FILE *fr;
  28.     char c[1];
  29.     struct stat sb;
  30.  
  31.     // obtains information about the file
  32.     if (stat(argv[1], &sb) == -1)
  33.     {
  34.         perror("stat()");
  35.         return(1);
  36.     };
  37.  
  38.     // verifies the file is over 100 bytes in size
  39.     if (sb.st_size < 101)
  40.     {
  41.        fprintf(stderr, "%s: file is less than 100 bytesn", argv[1]);
  42.        return(1);
  43.     };
  44.  
  45.     // opens the file, or prints the error and exists
  46.     if (!(fr = fopen (argv[1], "r")))
  47.     {
  48.         perror("fopen():");
  49.         return(1);
  50.     };
  51.  
  52.     fseek(fr, 100, SEEK_CUR);
  53.  
  54.     while (fread(c, sizeof(c), 1, fr) > 0)
  55.         printf("%c", c[0]);
  56.  
  57.     fclose(fr);
  58.  
  59.     return(0);
  60. }
  61.        
  62. while (fread(c, sizeof(char), 1023, fr) > 0)
  63.     {
  64.         c[1023] = '';
  65.         printf("%s", c);
  66.     };
  67.        
  68. printf("%c",*c);