
Untitled
By: a guest on
May 7th, 2012 | syntax:
None | size: 1.17 KB | hits: 13 | expires: Never
C: how to print content of file after seek
#include <stdio.h>
int main(int argc, char *argv[]){
FILE *fr;
if (fr = fopen (argv[1], "r")){
fseek(fr, 100, SEEK_CUR);
char c[1];
while (fread(c, 1, sizeof(c),fr) > 0)
printf("%s", c);
fclose(fr);
}
else{
perror("File does not exist");
}
}
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char *argv[])
{
FILE *fr;
char c[1];
struct stat sb;
// obtains information about the file
if (stat(argv[1], &sb) == -1)
{
perror("stat()");
return(1);
};
// verifies the file is over 100 bytes in size
if (sb.st_size < 101)
{
fprintf(stderr, "%s: file is less than 100 bytesn", argv[1]);
return(1);
};
// opens the file, or prints the error and exists
if (!(fr = fopen (argv[1], "r")))
{
perror("fopen():");
return(1);
};
fseek(fr, 100, SEEK_CUR);
while (fread(c, sizeof(c), 1, fr) > 0)
printf("%c", c[0]);
fclose(fr);
return(0);
}
while (fread(c, sizeof(char), 1023, fr) > 0)
{
c[1023] = ' ';
printf("%s", c);
};
printf("%c",*c);