0

Ive been using the above functions in my code and now found out that we are not allowed to. Perhaps someone can help in implementation of alternative function? Im allowed to use only Fgets,fgetc,fputc,fprintf,sprintf,fscanf,sscanf,rewind,stdin,stdout....

About fseek i have a general idea about getting the chars i need to skip over into an internal string Is it a correct thinking?

Ill be glad if someone can help. Thanks a lot!

Adiel
  • 1,203
  • 3
  • 18
  • 31
  • 2
    Who says you're not allowed to use `fseek()`? There isn't a sensible complete alternative unless you're using the file descriptor functions instead of the file stream functions. You seldom need to use `feof()` — see [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) for more information on that. Where do you need to seek to? If it is back to the start, then `rewind()` works. If it is to somewhere you've read up to, then `fgetpos()` and `fsetpos()` can be used. – Jonathan Leffler Apr 30 '14 at 07:04
  • Seems odd to bar `fseek()` yet allow `rewind()`. – chux - Reinstate Monica Apr 30 '14 at 12:32
  • My course lecturer \ proffesor is not allowing me to use it :-) neither fgetpos or fsetpos.. thanks to all of you, i've figured it out. check my comment to Timothy Jones :) – Adiel Apr 30 '14 at 13:13

2 Answers2

1

Apparently you are only allowed to read from stdin, which does not support seeking but does support end-of-file.

  • To skip characters, you can simply read them into a buffer using fread, and ignore the results in the buffer.
  • To detect end-of-file, check the return value of fread. If fread returns 0, there is nothing more to read (or an error occurred).
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • Thanks, but i'm now allowed to use fread :| thanks to all, i've figured it out. check my comment to Timothy Jones :) – Adiel Apr 30 '14 at 13:11
1

I assume these functions are forbidden because of some assignment restriction, rather than technical? It depends what you want to do:

  • feof() is for determining whether a stream is at the end of the file
  • fseek() is for moving to a particular position in that file.

You can determine whether a stream is at the end of the file at read time. For example, from the man page for fscanf:

The value EOF is returned if an input failure occurs before any conversion such as an end-of-file occurs.

For moving around within a file without using fseek(), you can use rewind() from your allowed list. From the man page for rewind():

The rewind() function sets the file position indicator for the stream pointed to by stream to the beginning of the file. It is equivalent to:

       (void)fseek(stream, 0L, SEEK_SET)

From there, you'll have to read up to the point you wanted to seek() to.

Timothy Jones
  • 21,495
  • 6
  • 60
  • 90
  • Hey, im not allowed due to assignment restrictions. – Adiel Apr 30 '14 at 07:10
  • Ok. Did my suggestions help, or are you still stuck? – Timothy Jones Apr 30 '14 at 07:11
  • i'm sorry for the short comment, was posting from my phone. as mentioned i'm not allowed to use the functions due to assignment restrictions. according to the comments of all you, and a bit thinking i managed a workaround. feof replace, i used an int flag - end_of_file = fscan(...) and checked the flag afterwards regarding fseek, i needed to go forward in the stream and not back so i've used fscanf(file, "%*[^\n]%*c") and its working perfect and moving the position indicator to the start of the next line, without "collecting" the unneeded text. thank you all and have a great week\weekend :) – Adiel Apr 30 '14 at 13:10