Question:
Can you please answer this C Programming language question?
TheInternetGuy
2010-06-29 04:02:41 UTC
inbclude
main()
{

FILE *fp;
fp=fopen("c:\\example.txt", "r");
char str[5];


fgets(str, 5, fp);
fgets(str, 5, fp);
fseek(fp, 10, 1);
}


after execution of the above program what will be the offset of the file pointer after giving the fseek statement?

is it ?

a: 11
b: 20
c: 21
d: 10

Please help me as it is very urgent and need to be answered by today evening 9:PM
Four answers:
Shadow Wolf
2010-06-29 16:48:29 UTC
The correct answer seems to be "unknown" or "not enough information". Since you don't know how it is defined, you can't give an answer. If you knew how it was defined for the compiler in question then you can figure out the answer. So you also need a copy of stdio.h to answer the question correctly.



However, if it is an interview question or one of those interview tests that some employers like to give, arguing that the question is invalid is more likely to cause you to lose the chance at the job. I've had this kind of thing happen to me on a real interview with a badly written test question. End result is you lose and probably don't get the job because you know more than the test writer.



Onward... Open up stdio.h and see what "1" means so you then know what fseek() actually does in the program. In the case of one compiler I have the following:



/* Constants to be used as 3rd argument for "fseek" function

*/

#define SEEK_CUR 1

#define SEEK_END 2

#define SEEK_SET 0



Adding the above information, your answer should be 20 if we use the defines given above.



Please note we are assuming a great deal to get this answer and most if not all programmers would probably use the labels for the defines because it would make understanding the program easier and not know what value they referenced.



If this isn't going to cost you a job, I'd suggest questioning the validity of the question.



Shadow Wolf
JoelKatz
2010-06-29 04:10:58 UTC
It would take you like one minute to compile this code, add an 'ftell', and run it. You'd have your answer in no time.



In any event, you can't answer it without knowing the platform, since there is no guarantee that the '1' in the 'fseek' won't be SEEK_SET on one platform but SEEK_CUR on another.



Also, it will depend on the contents of the 'example.txt' file. If the 'example.txt' file is, say, zero bytes long, then the offset of the file pointer will be zero no matter what you do with 'fseek'. Similarly, if the first line in the 'example.txt' file contains only a single byte, then the 'fgets' will get only a single byte. Whereas if it's a longer line, 'fgets' will get more bytes.



My bet would be that whoever asked you this question didn't want you to pick a, b, c, or d, but to point out the issues with the code.
?
2016-09-11 09:38:27 UTC
All of the "c languages" are exceptional. C and C++ are fairly comparable (within the experience that you'll software in C in C++), however that is approximately so far as that is going. To be sincere, embedded apps (Anything that runs from a computer, on that computer, like video games, workplace, iTunes, and so on) are commencing to emerge as a factor of the beyond. Learning Java or C# could be your first-class wager for retaining velocity with the marketplace. There will continually be a necessity for embedded apps even though. So dont believe that it's fully vain! It's simply that plenty of enbedded apps are fitting to be had as on-line equipment (like google workplace merchandise) If you're particularly fascinated with not anything however embedded apps, then you are going to wish to stay with a entirely compiled language. In that case, decide upon c++. capn. Those instructed coding matters all suck. Why no longer bypass the blabber and transfer directly directly to Eclipse? Eclipse is loose, pass platform and has a plethora of plugins all designed to do ALOT to accelerate growth and make it simpler.
2010-06-29 04:15:29 UTC
This page



http://www.cplusplus.com/reference/clibrary/cstdio/fseek/



tells you how fseek works. Now all you need to do is find our which constant (SEEK_SET, SEEK_CUR or SEEK_END) equates to the 1 that is the third parameter of the above fseek, and all that's left is common sense.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...