By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,964 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,964 IT Pros & Developers. It's quick & easy.

How to transfer a large file into an array whose size is unknown

P: 2
I am new to C programming and would appreciate if anyone could provide assistance. I have a large text file which i want to store in an array. But the problem is I do not know the size of the text file and subsequently the size of the array. I have tried using dynamic memory allocation but it doesnt seem to work.
Oct 10 '10 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 9,197
Open the file and do a seek to the end. Then do a tell.

The result of the tell will be the number of bytes in the file.

If you know the format of the file and you know the number of bytes in the file, then you know the number of array elements. From there you can allocate an array of the correct size.
Oct 10 '10 #2

P: 2
Hey thanks a lot... i hav another question. This txt file has four columns of data which i would like to tranfer to four different arrays. Can i use fgets to read one line at a time and cut and append those four columns?
Oct 10 '10 #3

Expert Mod 5K+
P: 9,197
There should be no reason you can't do that.

I might suggest you use a struct as your array element and fill the struct members rather than use seprarate arrays.
Oct 10 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.