473,320 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

to read a file in C and use the data

Hello everybody

I am quite new to C.I want to read a file and store the data in an array.And i also want to calculate the data_length of the file.

Please can anyone help

i have tried somethg like calculating number of char in the file but that doesnt give me the data_length of the file.

FILE *fp;
int noc=0;
char test[80];

char ch;
fp = fopen("/TC/MODPROJ/PR1.TXT","r");

while (1)
{
ch=fgetc(fp);

if (EOF==ch)
break;


noc++;


Can anyone please help me

thanks in advance

Nal
Jul 31 '07 #1
2 1462
JosAH
11,448 Expert 8TB
A quick and easy way is to read your file *twice*; the first time you read all your
data but don't store anything but when you've read everything you know how
many data items are in that file. Close the file, open it again and read everything
again, but now you have allocated an array of exactly the size you need so
you can store all your data.

kind regards,

Jos
Jul 31 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
You can also fseek() from 0 (the start of the file) to SEEK_END.

Then do a tell().

The tell returns the number of bytes from the beginning of your file.
Aug 1 '07 #3

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

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
5
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right...
14
by: Mark Broadbent | last post by:
Does anybody know what is (factual please -not just guess) the quickest method to read data from a file? I am not interested in the format of the data (i.e. blocks, bytes, string etc) just that the...
0
by: Peter | last post by:
I am having a problem reading an Excel file that is XML based. The directory I am reading contains Excel files that can be of two types. Either generic Microsoft based or XML based. I am reading...
2
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes echo...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
4
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.