Link to home
Start Free TrialLog in
Avatar of Nikhilesh_K
Nikhilesh_K

asked on

how to read something written by fprintf

hi

how can i read something written in a file using the following

fprintf(file,"%f %f\n",(float)i*abc,xyz[i]);

how can i get those arrays back in my program.

nikhil
Avatar of Jose Parrot
Jose Parrot
Flag of Brazil image

Hi, Nikhil,

Good see you around.
When we write a file by using fprintf, we write absolutely the same as we write in the console, that is, what we see in the screen is what is in the file. So, we should read it as text.

In your case,
fprintf(file,"%f %f\n",(float)i*abc,xyz[i]);
would write something like
5.2541 982.734
for instance.

To read  such float numbers, we can simply
fscanf(file,"%f %f\n",&abc, &xyz[i]);
and the text will be read, turned into float numbers and writen in the rigth addresses.

The code

//---------------------------------------------------------------------------
#pragma hdrstop
#include <stdio.h>
void main(void)
{
    FILE *f;
    float abc;
    float xyz[10];
    float tmp1, tmp2;
    int i;

    f=fopen("c:\\test.txt","wt");
    abc=0.618; xyz[2]=3.14159; i=2;
    fprintf(f,"%f %f\n",(float)i*abc,xyz[i]);
    printf("%f %f\n",(float)i*abc,xyz[i]);
    fclose(f);

    f=fopen("c:\\test.txt","rt");
    fscanf(f,"%f %f",&tmp1, &tmp2);
    fclose(f);
    abc=tmp1/i;
    xyz[2]=tmp2;

    printf("%f %f\n",(float)i*abc,xyz[i]);
}
should print:

1.236000 3.141590
1.236000 3.141590


Jose
Avatar of Nikhilesh_K
Nikhilesh_K

ASKER

hi

say i have a huge array in the test.txt
for(i=0, i<400, i++)
{
fprintf(f,"%f %f\n",(float)i*abc,xyz[i]);
}

so how can i read all of it?
Hi,

Please note, there are two values inside each loop:
first: (float)i*abc
second: xyz[i]

The second one is simple to manage, as we can store each value inside each cell of a an array, in the same sequence as they appear in the file.
The fisrt one isn't declared as an array, so the suggestion I have is to store them too, experimentaly, in a array.

So, the reading can be:

FILE *f;
int i;
float tmp;
float abci[MAX], xyz[MAX]; // you must #define MAX 1000 previuosly, with any other valid number
f=fopen("MyFilename.txt","rt");
i=0;
while (!eof)
{
   i++;
   fscanf(f,"%f %f",&tmp, &xyz[i]);   // you must use & to denote "address of"
   abc[i]=tmp/i;
}
fclose(f);

Case you notice that abc array is merely a sequence of multiples of a number, you can store just the first value and ignore the remain entries. For example, if the abc values are
abc[1] = 10
abc[2] = 20
abc[3] = 30,

then, clearly, this is only a given constant, say A=10 and the abc array is just the product of such value by 1, 2, 3 , etc, as long the loop continues when recording the file. To store these values as an array is pure waste of memory room.
In this case, you can read abc only in the first loop and skip it in the next loops. Then, the code would be:

FILE *f;
int i;
float A,tmp;
float abci[MAX], xyz[MAX]; // you must #define MAX 1000 previuosly, or with any other valid number
f=fopen("MyFilename.txt","rt");
i=1;
fscanf(f,"%f %f",&A, &xyz[i]);
while (!eof)
{
   i++;
   fscanf(f,"%f %f",&tmp, &xyz[i]);
}
fclose(f);

By the end of this second code, we have the values of variable A and array xys[1..n], being n the number of readings from the file.

Jose
hi

here are some of the values that i am trying to read

0.000000 0.000000
62.499996 184.398804
124.999992 179.076813
187.499989 176.800430
249.999985 165.335587
312.499981 157.636505
374.999977 164.393326
437.499973 159.548981

they are sored in a file named "new.001"

 i tried this

//---------------------------------------------------------------------------

#include <vcl.h>
#include <stdio.h>
#include <io.h>
#pragma hdrstop

#include "Unit1.h"
#define MAX 5000
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SLScope"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
FILE *f;
int i;
float A,tmp;
float abci[MAX], xyz[MAX]; // you must #define MAX 1000 previuosly, or with any other valid number
f=fopen("new.001","rt");
i=1;
fscanf(f,"%f %f",&A, &xyz[i]);
while (!eof)
{
   i++;
   fscanf(f,"%f %f",&tmp, &xyz[i]);
abci[i]=tmp/i;
}
fclose(f);
Edit1->Text=xyz[4];

}

but in edit1 box i get zero always.

ASKER CERTIFIED SOLUTION
Avatar of Jose Parrot
Jose Parrot
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial