When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.
31 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.
C++ Trying to use fscanf to read a text file and store into struct
is this C or C++?
do you know that std::ifstream exists in C++?
wait a minute, this is your first week in C++, and you are writing this?
yep, here you are writing in C, this has nothing in common with C++, so where is the problem?
which words are not read correctly?
in other words you do not correctly detect the empty line used as a separator?
fscanf is meant to read non-whitespace characters, it completely skips over \n
you probably want to consider using getline to read the file first, then parse the contents later
#include
#include
#include
#include
using namespace std;
ve
this would be the "C++ way" to do this
he hasn't used the STL ever, he knows C only
or you could try to parse inline on demand, during read
I'm able to read and follow the conversation, I'm not unaware of the situation
I gave an example, is that a problem for you?
really it doesn't matter if it's a problem for you, it matters if it helps meowstars
have you used malloc?
if you have, create a cusom global function called readLine, by using the function fread and reading 1 byte at a time, from there extract the individual words with sscanf
when an empty line is detected, you will know that this is the separator
why do things the old shit C way if this is a C++ course
unless the professor mandates it
which is quite plausible, but it doesn't seem this has been yet stipulated as a requirement
yes, fgetc is the same as calling fread with 1 byte as the 3rd parameter
also, you might need ungetc to teleport back to th previous character
https://pubs.opengroup.org/onlinepubs/007908799/xsh/ungetc.html
since in C you can't read 1 character without advancing the file pointer one character forward
or we could encourage using C++ which also has a function to get a single character at a time from a stream
I was talking about peek()