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]);

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 ><

you opened the file for reading, not writeing.

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 ?

Open it for reading and writing?

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 ? ><

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.

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++)

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

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



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.

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

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.

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

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?

I've fixed the silly problems b4, now the codes can be compiled, but it doesn't hold the windows as expected. So i guess it doesnt scan in properly (i need to keep windows opening, as its part of my assignment :p )

You can try to compile it , it will close straight away.

you have to put a line at the end of main() to keep the window from closeing. Add a getchar() will do it.

I do have getchar() at the end of my code :( ><

That's very strange... AD is right a getchar() should do the trick.
Try replacing the getchar() with: while(getchar()!='q'){} Now you'll have to enter a 'q' and 'enter' to quit

I just compiled and ran your program in Dev-C++ and the window didn't close on me. I also ran it from command prompt and it worked there too.

Another thought comes to mind:
Where is your input.txt? Is it in the same folder as the executable? If not: crash

@ niek: i tried ur method, doesnt work either.
@ AD: can u please post the source code and the file u compiled on here ? I use dev-c++ too and doesnt work ><

---- EDITED -----

oh wait. I got it working for some reasons. I need to see if the thing really works :p BRB
oh and I finally realized that when u compiled with dev C++ and press run in the code, it wont run, have to go to the actual folder to run it.

Thank you guys for helping me. I finally figured everything out. Now im working on the rest of my assignment . Again, thanks a lot !!!!!! This is a hell long assignment and its due monday hehe. :p

(and i'll fix the void main(void) too =] "The lecturer said its OK" ! :p i'll just do the standard one)

>>@ AD: can u please post the source code and the file u compiled on here ? I use dev-c++ too and doesnt work ><
I used exactly the same source code that appears in post #7 in this thread, except I fixed the void main. I didn't use a data file so it didn't actually read anything. But the program didn't crash as niek_e suspected.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.