User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 426,595 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,654 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 459 | Replies: 18 | Solved
Reply
Join Date: Apr 2008
Posts: 14
Reputation: ohhmygod is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ohhmygod ohhmygod is offline Offline
Newbie Poster

Need help on my assignment !!! - about file streams :((

  #1  
May 15th, 2008
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 ><
Last edited by Ancient Dragon : May 15th, 2008 at 8:02 am. Reason: snipped your real name from the path
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,189
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 931
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Need help on my assignment !!! - about file streams :((

  #2  
May 15th, 2008
you opened the file for reading, not writeing.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Apr 2008
Posts: 14
Reputation: ohhmygod is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ohhmygod ohhmygod is offline Offline
Newbie Poster

Re: Need help on my assignment !!! - about file streams :((

  #3  
May 15th, 2008
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 ?
Reply With Quote  
Join Date: May 2008
Posts: 159
Reputation: Clockowl is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 7
Clockowl Clockowl is offline Offline
Junior Poster

Re: Need help on my assignment !!! - about file streams :((

  #4  
May 15th, 2008
Open it for reading and writing?
Reply With Quote  
Join Date: Apr 2008
Posts: 14
Reputation: ohhmygod is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ohhmygod ohhmygod is offline Offline
Newbie Poster

Re: Need help on my assignment !!! - about file streams :((

  #5  
May 15th, 2008
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 ? ><
Reply With Quote  
Join Date: May 2008
Posts: 159
Reputation: Clockowl is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 7
Clockowl Clockowl is offline Offline
Junior Poster

Re: Need help on my assignment !!! - about file streams :((

  #6  
May 15th, 2008
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.
Reply With Quote  
Join Date: Apr 2008
Posts: 14
Reputation: ohhmygod is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ohhmygod ohhmygod is offline Offline
Newbie Poster

Re: Need help on my assignment !!! - about file streams :((

  #7  
May 15th, 2008
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
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,189
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 931
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Need help on my assignment !!! - about file streams :((

  #8  
May 15th, 2008
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.
Last edited by Ancient Dragon : May 15th, 2008 at 9:52 am.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Apr 2008
Posts: 14
Reputation: ohhmygod is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ohhmygod ohhmygod is offline Offline
Newbie Poster

Re: Need help on my assignment !!! - about file streams :((

  #9  
May 15th, 2008
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
Last edited by ohhmygod : May 15th, 2008 at 10:02 am.
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Need help on my assignment !!! - about file streams :((

  #10  
May 15th, 2008
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?
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 10:52 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC