Archived

This topic is now archived and is closed to further replies.

XaOs

File reading error.........*grin*

Recommended Posts

XaOs    122
Hi!! This is probably the most stupid post i will ever do, and the sollution is probably so easy that i can't see it... But here we go.... FILE *file = NULL; (...) file = fopen ("blabla.txt", "r"); for (x=0; x<10; x++) { fscanf (file, "%f,", &xpos); } fclose (file) ...(or close(file)...don't remeber right now))!! This gives me an error! It won't compile!! What am i doing wrong here...????? This used to work for me before...but it won't do it now! Any help would be appreciated! (BTW : The file contains floats.... i.e : 1.0, 1.0, 1.0, ...10 times...)(forgot to add) Thanks! Take Care! - -- ---[XaOs]--- -- - [ project fy ] Edited by - XaOs on October 16, 2001 6:20:33 AM

Share this post


Link to post
Share on other sites
omega_001    122
Did you remember to include the header file for that command?

#include

If it hasn''t compiled there is nothing wrong with the file its to do with your code.

Make sure xpos is a float.

If that doesn''t help tell us the error code.

________________________________________________

"Compare yourself to me then kill yourself"

Share this post


Link to post
Share on other sites
baldurk    1028
it could be fscanf() giving you the problem. I noticed you are ignoring the 2nd and 3rd floats. The proper command should be:

fscanf (file, "%f %*f %*f", &xpos);

assuming the lines are just:

1.0 1.0 1.0

the * tells it to read a float but ignore it.

Share this post


Link to post
Share on other sites
XaOs    122
Jupp!!! I got the #include right....


Okey....so presuming i got a file something like this :

1.0f, 1.0f, 1.0f, 1.0f
1.0f, 1.0f, 1.0f, 1.0f
1.0f, 1.0f, 1.0f, 1.0f

then i will do like this :

for (x=0; x<4; x++) {
for (y=0; y<3; y++) {
fscanf (file, "%f,%f,%f,%f", &x1, &x2, &x3, &x4);
}}

This is what you mean?
But what if i got like 30 floats in both x and y?
Then doing it like this would be a little wrong....

But..thanx anyway...i'll give it a try when i get home from school!!!!!

Take Care!

- -- ---[XaOs]--- -- -

[ project fy ]

Edited by - XaOs on October 16, 2001 8:48:47 AM

Share this post


Link to post
Share on other sites
XaOs    122
quote:
Original post by XaOs
Jupp!!! I got the #include right....


Okey....so presuming i got a file something like this :

1.0f, 1.0f, 1.0f, 1.0f
1.0f, 1.0f, 1.0f, 1.0f
1.0f, 1.0f, 1.0f, 1.0f

then i will do like this :

for (x=0; x<4; x++) {
for (y=0; y<3; y++) {
fscanf (file, "%f,%f,%f,%f", &x1, &x2, &x3, &x4);
}}

This is what you mean?
But what if i got like 30 floats in both x and y?
Then doing it like this would be a little wrong....

But..thanx anyway...i''ll give it a try when i get home from school!!!!!

Take Care!

- -- ---[XaOs]--- -- -

[ project fy ]

Ohhhh...damn......i forgot something i my first post!!
he he....my xpos is really a xpos[10]!!!!!!

so reading would be something like this...

fscanf (file, "%f,", &xpos[x]);

might be a little more correct now......


Edited by - XaOs on October 16, 2001 8:48:47 AM


Share this post


Link to post
Share on other sites
XaOs    122
Holy cow....what did i do to that post?
Most have pressed the "quote" button instead of the "edit" button....

Cool post though.... *LOL*!!

Take Care!

- -- ---[XaOs]--- -- -

[ project fy ]

Share this post


Link to post
Share on other sites
pacrugby    122
here is what I use:

  
char ty[100];
ifstream InputFile2;
InputFile2.open("example6.res", ios::in , filebuf::binary);
InputFile2.getline(ty,100,''\n'');
sscanf(ty,"%i %f %f %f %f %f %f %f %f %f %f %f %f",&a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8,&a9,&a10,&a11,&a12,&a13);


I read one line at a time.
I think its quicker???

Share this post


Link to post
Share on other sites
TheMummy    122
But what happens when the line is larger than 100 chars ?? How can you know that there are still characters waiting in this line ?
Why not using C++ ???
instead of these old style scanf methods?

istream file("myfile.txt",ios:open);
for(every float I am waiting for)
{
if(file.eof())
handle_this_error();

file >> nextfloat;
}

I always try to avoid these f(const char *, ...); methods , but sometimes they are realy powerful.

Share this post


Link to post
Share on other sites
pacrugby    122
I do it this way because it works.
I know it isnt pretty.
if you are reading data you should have an idea of the
ammount of info per line. All I need is 100char if you need more change it.
the '\n' reads till the end of line.
I think reading in a line and then using the string is
the fastest way to do things.
If it isnt Id love to know a faster way.
I deal with 9meg text files for my modeling.


Edited by - pacrugby on October 16, 2001 12:22:19 PM

Share this post


Link to post
Share on other sites