nice reading simple model file with stdio

Started by
8 comments, last by fir 10 years, 4 months ago

this is a basic question but
i am curious for some nice answer

i need to read my own simple format
file which in general is containing
words interleaved with floats

something 1 2 3
other 2 3
else 8 9 99 9 1 2

i want to load in by using only c
standard library functions

when i need read only floats I can
use fscanf "%f" in loop this is
very easy and nicely skips whitespaces
also is able to say when there is an
end of data - how is easy way
of doing the above?

Advertisement

If you always have a string followed by a varying number of floats, you could use vscanf. If you have floats and strings intertwined then you should read one line at a time (or the whole file if it's small to improve performance) and then perform a parsing of the memory buffer you've read the file and split it into tokens (separated by whitespaces). Then you can try to convert them to floats and see which ones are floats and which ones are not.

If you always have a string followed by a varying number of floats, you could use vscanf. If you have floats and strings intertwined then you should read one line at a time (or the whole file if it's small to improve performance) and then perform a parsing of the memory buffer you've read the file and split it into tokens (separated by whitespaces). Then you can try to convert them to floats and see which ones are floats and which ones are not.

using

fscanf("%s", buf);

and then checking if buf[0] is digit ( or +/- ) would be quite goode

xcept that when word in input file would be larger than buffer it

would be an unavoidable program crash - this is hell

isnt there in c lib a safe function what would be reading

the strings contained between the whitespaces?

Sorry for the late answer. Why don't you get file size first, allocate a big enough buffer to contain the file, read all file at once as binary data, then using strtok split the file into tokens based on white spaces and end of line characters and then process each token as you get it and use it based on the reading state. You can use something like a state machine to know what to do with the current token.

Sorry for the late answer. Why don't you get file size first, allocate a big enough buffer to contain the file, read all file at once as binary data, then using strtok split the file into tokens based on white spaces and end of line characters and then process each token as you get it and use it based on the reading state. You can use something like a state machine to know what to do with the current token.

I found that i can use scanf "%20s" this will read the word

but if more then 20 letters will split on amaller chunks - this

could be used

But tell me - this is a think I didnt ever know: is it posible

to check quickty filesize without reading it in c-lib and winapi? I never knew that..//lay

There is this winapi function; http://msdn.microsoft.com/en-us/library/windows/desktop/aa364955(v=vs.85).aspx

allright, tnx for info, good to know, hehe, never used that yet,

see inih and author's home blog, perhaps this will help for you

On a side note, you don't need to insert so many line-breaks in your posts. Just type normally with line-breaks between paragraphs and the forum software will sort out wrapping your text to the proper width of the screen for you.

It's much harder

to read what

you've typed when

it's broken into

short lines

rather than being properly line-wrapped as you get from regular typing without inserting extra line-breaks. smile.png

- Jason Astle-Adams

On a side note, you don't need to insert so many line-breaks in your posts. Just type normally with line-breaks between paragraphs and the forum software will sort out wrapping your text to the proper width of the screen for you.

It's much harder

to read what

you've typed when

it's broken into

short lines

rather than being properly line-wrapped as you get from regular typing without inserting extra line-breaks. smile.png

I know :) but this is somewhat counter intuitive for me,

I also make big amount of typos and got weak english

When i see the unbreaked text in this edit window it is

psychical pain for me. I like this forum that no people attacks

me fot typos and such chaotic form, so IMo the best would be to allow that :) I will be tring to improve maybe in some extent

This topic is closed to new replies.

Advertisement