Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


nice reading simple model file with stdio


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 29 November 2013 - 03:08 AM

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?



Sponsor:

#2 meeshoo   Members   -  Reputation: 508

Like
1Likes
Like

Posted 29 November 2013 - 03:28 AM

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.



#3 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 29 November 2013 - 06:43 AM

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?



#4 meeshoo   Members   -  Reputation: 508

Like
1Likes
Like

Posted 02 December 2013 - 11:44 AM

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.



#5 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 02 December 2013 - 12:32 PM

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



#6 meeshoo   Members   -  Reputation: 508

Like
1Likes
Like

Posted 05 December 2013 - 11:41 AM

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



#7 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 05 December 2013 - 11:58 AM

 

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



#8 pyirrlicht   Members   -  Reputation: 618

Like
0Likes
Like

Posted 09 December 2013 - 02:45 AM

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



#9 jbadams   Senior Staff   -  Reputation: 19324

Like
0Likes
Like

Posted 10 December 2013 - 04:27 AM

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



#10 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 10 December 2013 - 04:34 AM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS