Вопрос

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!

Это было полезно?

Решение

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.

Другие советы

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).
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top