I am using dque to push some elements that I read from a file to the back of the deque but when I print them from the front I only get the last element
fgets(line,100,file);
qu.push_back(line);
fgets(line,100,file);
qu.push_back(line);
fgets(line,100,file);
qu.push_back(line);
fgets(line,100,file);
qu.push_back(line);
while(!qu.empty())
{
puts(qu.front());
qu.pop_front();
}
sample input
a
b
c
d
output
d
d
d
d
Thank you
You haven’t provided the declarations for
lineandqu, but I can guess they areThis means
qustores pointers to an external (toqu) buffer with characters in it. In your program, you have just a single buffer (line), whose address gets pushed repeatedly intoqu, but whose contents get overwritten with eachfgetscall.While you are learning C++, try to stay away from pointers as much as you can. So, instead of using
char*for strings, usestd::string: