Microsoft Interview Question
Software Engineer / DevelopersYeah I mentioned fseek to him, when you read the entire file, it s something like
f1 = fopen(file)
readone()
{
while(!feof(f1))
f1 = fscanf("%d",something);
}
But about reading one like everytime, readone() is called, I stil dont get!
#include <stdio.h>
static char *readone(char *path) {
static long offset;
static char line[8192];
FILE *f = fopen(path, "r");
char *p;
fseek(f, offset, SEEK_SET);
p = fgets(line, sizeof line, f);
offset = ftell(f);
fclose(f);
return p;
}
int main(int argc, char **argv) {
char *line;
while((line = readone(argv[1])) != NULL) {
fputs(line, stdout);
}
}
@hary. Everything is missing here.
It gives incorrect answer! It has buffer overflow. The first time readone is called, it could seek to a random offset in the file.
In short, it is crap.
If i wrong or missing something please correct me
I agree buffer overflow would be there. But since the question says readoe would read one line at every call, then i am assuming they there is a restriction on the size of each line, because you need to read each line and store in internal buffer. Any API one uses, fscanf, fgets will make use of a buffer string.
As far as the first call i sconcerned it will not seek to a random offset because offset is static and is initialized to 0, so fseek will move you nowhere w.r.t to the begining of the file.
If you still fell its wrong suggest ways to improve this.
it happens, its all abt keeping ur nerves. Q is simple and instead of algo, I guess intent was to see the design/architect skills.
I hv very little idea of C/C++ lib for readfile
In C# I would use static counter to keep the count of number of lines read
Then some additional checks like EOF case/ExceptionHandling/Empty lines etc.
Keep trying, everyones knows its hard to get thru, but once u get it, its an accomplishment.
Gud Luck
I think what he wanted to see was what is your knowledge for fscanf.
So you need to give it a scanset of not to stop till '\n'
#include <stdio.h>
void readone();
int main(int argn, char **argv)
{
int i;
for (i=0; i<=2; i++){
readone();
}
}
void readone()
{
static int ctr = 0;
FILE *f = fopen("test.txt", "r");
char str[100];
int i;
for (i=0; i<=ctr; i++){
fscanf(f, "%[^\n]\n", str);
}
puts(str);
fclose(f);
ctr++;
}
doesnt fgets function read a line from a file..
i mean read is terminated as soon as it encounters a /n
I am so sorry to write an incorrect answer.
Answer to your question is yes
Here is the definition from man pages
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.
What i intended was that you anyhow would be restricted by SIZE chars and so cap on the buffer used must always be there unless we think of a way to avoid this.
Guys, Sincerely, the solution to this question can be from very simple C++ to very complex C. I think it depend on the interviewer's preferences. Below is a simple C++ solution using fstream library. You don't need fseek here as fstream keep tracking it for you. In fact, it always does and we often overlook this as we often use Yan's approach all the times.
fstream file;
void readOne()
{
string str;
getline(file,str);
cout << str << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
file.open("Your file path here",std::ios_base::in);
readOne();
readOne();
readOne();
file.close();
string str;
cin >> str;
}
FILE *fp; // fp is file pointer
void read (void)
{
if(!feof)
{
fgets(str,len,fp);//it reads file until new line character is read or len-1 char's
//are read ( so keep len very large number ),
//after this str contains first line of file .
// now file pointer points to end of the first line.
fseek(fp,1,SEEK_CUR);// now file pointer points to next line beginning.
// so when ever file is called it will keep moving to next line
cout<<str;
}
}
The C standard library specifies calls ftell() and fseek() that allow you to save and restore the position in the file. (Note: Microsoft probably has their own bastardized equivalents.) Just store the offset in a static variable.
- Anonymous March 02, 2010