DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Need help on my assignment !!! - about file streams :(( (http://www.daniweb.com/forums/thread124352.html)

ohhmygod May 15th, 2008 7:56 am
Need help on my assignment !!! - about file streams :((
 
I got this code here, i wonder why it doesnt put in the file "in.dat" in the same folder.


#include <stdio.h>
#include <stdlib.h>
#define N 20 //Number of trials

void main(void)
{
FILE * pFile;
float data[N][2];
int i,j ;
char overwrite;

for(i=0;i <= N ; i++)                                                              /* Initinalize the array */
{
        for (j=0; j <= 1; j++)
    data[i][j]= 0;
}


for(i=0; i<=N ; ++i)
{
        printf("%d %5f %5f\n", i, data[i][0], data[i][1]);
}

data[0][1] = -0.545 ;
data[1][1] = 0 ;
data[2][1] = 0 ;
data[3][1] = 0 ;
data[4][1] = 0 ;
data[5][1] = 0 ;
data[6][1] = 0 ;
data[7][1] = 0 ;
data[8][1] = 0 ;
data[9][1] = 0 ;
data[10][1] = 0 ;
data[11][1] = 0 ;
data[12][1] = 0 ;
data[13][1] = 0 ;
data[14][1] = 0 ;
data[15][1] = 0 ;
data[16][1] = 0 ;
data[17][1] = 0 ;
data[18][1] = 0 ;
data[19][1] = 0 ;
data[20][1] = 0 ;

pFile= fopen("C:\\Documents and Settings\\<snipped name>\\My Documents\\Uni Stuff\\Eng Computing\\Sourcecodes\\in.dat","rb");

for(i=0; i<=N; i++)
{
//value[i] = ftell(pFile);
data[i][0] = 5*i;

printf("%f %f\n", data[i][0], data [i][1]);
fprintf(pFile,"%f %f\n", data[i][0], data [i][1]);
}
fclose(pFile);


scanf( "%c", &overwrite);
}

2. Im trying to do this(as said in the assignment):
- Type in the experimental data in a text file and save the file as “in.dat”.

- In the data entry part, use the fscanf() function to read the data from “ïn.dat" file. It is very important that you store the data in a suitable array ( data[N][2] for example). Make sure that all the array's elements are set to zero before you use the array.

This is what i need to put in the file:
0   -0.545
5  0.462
10  2.163
15  4.446
20  7.304
25  10.781
30  14.962
35  19.613
40  25.034
45  30.882
50  37.558
55  44.505
60  52.241
65  60.568
70  69.435
75  78.947
80  89.156
85  99.876
90  111.294
95  123.2
100 135.618

Thanks for helps guys :( I really need it ><

Ancient Dragon May 15th, 2008 8:01 am
Re: Need help on my assignment !!! - about file streams :((
 
you opened the file for reading, not writeing.

ohhmygod May 15th, 2008 8:12 am
Re: Need help on my assignment !!! - about file streams :((
 
Thanx i realized that. The reason im doing that to input in the file is to get the pointer position so that i can read from the file and get the data in to my arrays. Is there any easier way to do that ?

Clockowl May 15th, 2008 8:51 am
Re: Need help on my assignment !!! - about file streams :((
 
Open it for reading and writing?

ohhmygod May 15th, 2008 8:56 am
Re: Need help on my assignment !!! - about file streams :((
 
Wat i did is i write the data in first, then open it for reading the data in the file to the array. ;( Trying to figure out, any help ? ><

Clockowl May 15th, 2008 9:07 am
Re: Need help on my assignment !!! - about file streams :((
 
Just open it for reading and writing. Write to it, read from it.

What's the problem? You can find how fopen works for reading and writing on numerous C reference sites.

ohhmygod May 15th, 2008 9:36 am
Re: Need help on my assignment !!! - about file streams :((
 
Nah thats alright dw about it i fixed the writing reading problem. This is another approach. Index.txt is the file that has the data above.


#include <stdio.h>

#define M 21
#define N 2

void main ()
{
        float a[M][M], x, y;
  FILE *ifp, ;
  int i, j;

  for(i=0; i<M; i++)
  {
          for(j=0;j<N;j++)
      {
              a[i][j]=0;
      }
  }

  ifp=fopen("input.txt", "r");


  for(i=0; i<M; i++)
  {
          for(j=0;j<N;j++)
      {
              fscanf(ifp," %f", &a[i][j]);
        printf("%f\t", a[i][j]);
      }
      printf("\n");
  }




  fclose(ifp);


  getchar();
}

With this code, if M = N then it works (providing the data is in square matrix, which is not suitable for me.

My goal: Input the data from index.txt to the array a[M][N] with M is row and N is collumn.
index.txt
0.000000 -0.545000
5.000000 0.000000
10.000000 0.000000
15.000000 0.000000
20.000000 0.000000
25.000000 0.000000
30.000000 0.000000
35.000000 0.000000
40.000000 0.000000
45.000000 0.000000
50.000000 0.000000
55.000000 0.000000
60.000000 0.000000
65.000000 0.000000
70.000000 0.000000
75.000000 0.000000
80.000000 0.000000
85.000000 0.000000
90.000000 0.000000
95.000000 0.000000
100.000000 0.000000

Ancient Dragon May 15th, 2008 9:52 am
Re: Need help on my assignment !!! - about file streams :((
 
Instead of using the loops to initialize the array, you can also do it when the array is declared, like this:
float a[M][M] = {0};

The above will initialize all elements of the array to 0.

ohhmygod May 15th, 2008 10:01 am
Re: Need help on my assignment !!! - about file streams :((
 
I still dont understand how my code doesnt work for 21*2 Matrix. , it doesnt read from the file and put in the matrix. Thx for the short way of initializing arrays :p

niek_e May 15th, 2008 10:09 am
Re: Need help on my assignment !!! - about file streams :((
 
Two things:
This is a typo:
FILE *ifp, ;
Remove the comma:
FILE *ifp ;

And you might want to read this about void main

The rest of the code looks fine to me. What doesn't work?


All times are GMT -4. The time now is 10:24 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC