[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

how to determine the length of a file

Posted on 2003-02-25
9
Medium Priority
?
184 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
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

607 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