#C++ Trying to use fscanf to read a text file and store into struct

31 messages · Page 1 of 1 (latest)

old jayBOT
#

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.

crude wyvern
#

C++ Trying to use fscanf to read a text file and store into struct

spare bridge
#

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

vapid crane
#

you probably want to consider using getline to read the file first, then parse the contents later

#

this would be the "C++ way" to do this

spare bridge
vapid crane
#

or you could try to parse inline on demand, during read

vapid crane
#

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

spare bridge
#

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

vapid crane
#

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

spare bridge
#

yes, fgetc is the same as calling fread with 1 byte as the 3rd parameter

#

since in C you can't read 1 character without advancing the file pointer one character forward

vapid crane
#

or we could encourage using C++ which also has a function to get a single character at a time from a stream

spare bridge
old jayBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] C++ Trying to use fscanf to read a text file and store into struct