?
Solved

how to determine the length of a file

Posted on 2003-02-25
9
Medium Priority
?
178 Views
Last Modified: 2011-04-14
Im opening a file which length I dont know, but want read all the datas in there, here my code:

const char* infile;
infile="test.dat";
vector<double> key;
int size=0;

FILE *fp1;

fp1=fopen(infile, "r");

//HERE I NEED THE SIZE OF THE FILE

while( !feof( stream ) ){size++;}
//this gives an unending loop!!


key.resize(size);

for (int k=0; k<size; k++){
     fscanf(fp1, "%lf\n", &key[k]);
}
 
fclose(fp1);

return key;




Any suggestions?

Thanks

Urs
0
Comment
Question by:Cryptos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 5

Accepted Solution

by:
Kimpan earned 120 total points
ID: 8016632
use fseek to get to the end of the file
and use ftell to get the position
0
 

Author Comment

by:Cryptos
ID: 8016639
of course stream in the while loop is not stream, its fp. But this gives still an infinite loop!

greets U
0
 

Author Comment

by:Cryptos
ID: 8016668
How would the code look with this fseek?? And how do I move back to the begining to start the scan again??

Thanks

U
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:jkr
ID: 8016679
>>//HERE I NEED THE SIZE OF THE FILE

struct _stat st;

fstat ( fp1, &st);

// 'size' can be found as st.st_size
0
 

Author Comment

by:Cryptos
ID: 8016715
jkr:

header??

Im working with MSVC6.

thanx

U
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8016833
[1] open the file
[2] use fseek() to move the pointer at the end of the file
[3] use ftell() to get the position, which is the length of the file
0
 

Author Comment

by:Cryptos
ID: 8016866
jkr:

header found. But the problem is still: It doesnt compile, at the _fstat(...) function takes an int as first argument and not my *iobuf.
So I use the oldcast _open() function? therefore I must know the number of bytes in the file. In fact there are doubles ints and so on...

mhhh

tanx
U
0
 
LVL 86

Expert Comment

by:jkr
ID: 8016887
#include <sys/stat.h>
#include <sys/types.h>

The VC++ docs on '_fstat()' are mentioning that :o)
0
 

Author Comment

by:Cryptos
ID: 8024553
The thing works fine.

Thanks

U
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question