1

I want to read a file from a specified location. I have set a file pointer to the particular location (say 400). Now I am running a while loop till end of the file, and I am reading it line by line using fgets. But fgets reads the file from starting, and not from the specified location.

fsetpos(src,&pp);
while(!feof(src)) {
     fgets(cpy_buff,sizeof(cpy_buff),src);
     buffsize=strlen(cpy_buff);
     fwrite(cpy_buff, 1,buffsize,dst);
     memset(cpy_buff, 0, sizeof(cpy_buff));
}
peterh
  • 11,875
  • 18
  • 85
  • 108
RaVi DuDi
  • 15
  • 7
  • 2
    Read this: [**Why is “while (!feof(file))” always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Andrew Henle Jun 19 '19 at 07:31
  • 2
    Start by checking the `fsetpos` return value. Is it zero? – Support Ukraine Jun 19 '19 at 07:33
  • While the refered links are mainly correct, their arguments are in an *"abstract, high-level perspective"*. In fact this is a valid operation and you can use it without any problem in playground code. What is in `pp` before and after the call? Simply print it with a `printf`, or use a debugger. – peterh Jun 19 '19 at 07:34
  • How is `pp` initialized? By a `fgetpos` call? – Support Ukraine Jun 19 '19 at 07:37
  • Maybe you want to check out `fseek` - see for instance https://stackoverflow.com/questions/20510718/moving-file-pointer-back-to-string-it-already-read I'm pretty sure that `fsetpos` is **not** what you want – Support Ukraine Jun 19 '19 at 07:38
  • 3
    *I have set a file pointer to the particular location (say 400).* You can't "set" a position for `fsetpos()` in that way. You can only use it to return to a location and state obtained from `fgetpos()`. Per [the C standard](https://port70.net/~nsz/c/c11/n1570.html#7.21.9.3): "The `fsetpos` function sets the mbstate_t object (if any) and file position indicator for the stream pointed to by stream according to the value of the object pointed to by `pos`, **which shall be a value obtained from an earlier successful call to the fgetpos function on a stream associated with the same file**." – Andrew Henle Jun 19 '19 at 07:39
  • @peterh *What is in pp before and after the call? Simply print it with a printf, or use a debugger.* Read this answer: https://stackoverflow.com/a/39587990/4756299 The platform isn't specified. You can't in general `printf()` an `fpos_t` value. – Andrew Henle Jun 19 '19 at 07:41
  • @AndrewHenle Yes. We need to suspect as a *work hyphothesis* that it is an integer or similar. It has a quite high chance. If you have some language lawyer affinity, then I would say: the question indirectly states that it is an integer what can contain 400. – peterh Jun 19 '19 at 07:44
  • @AndrewHenle Oops, I've read your comment just now! You are right. I think it could be a nice answer! – peterh Jun 19 '19 at 08:33
  • 1
    Welcome to SO. Please provide a [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) that reproduces the problem. Your code is way too little to know what is going on. – Gerhardh Jun 19 '19 at 08:33
  • @Gerhardh Meanwhile, Andrew Henle has answered it in a comment. This problem does not require MCVE, because it is not a debug problem (even if it looks so), instead it is a C standard api question. – peterh Jun 19 '19 at 08:34
  • 2
    @peterh While it is very likely that Andrew Henle indeed is right, that comment is based on assumptions about what content might be used in `fsetpos`. A MCVE would show it. – Gerhardh Jun 19 '19 at 08:41

0 Answers0