•
•
•
•
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
![]() |
| |
•
•
Join Date: Apr 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
I got this code here, i wonder why it doesnt put in the file "in.dat" in the same folder.
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:
Thanks for helps guys
I really need it ><
#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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,189
Reputation:
Rep Power: 38
Solved Threads: 931
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Apr 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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
#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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,189
Reputation:
Rep Power: 38
Solved Threads: 931
Instead of using the loops to initialize the array, you can also do it when the array is declared, like this:
The above will initialize all elements of the array to 0.
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation:
Rep Power: 11
Solved Threads: 185
Two things:
This is a typo:
And you might want to read this about void main
The rest of the code looks fine to me. What doesn't work?
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
do NOT pm me for help, in the best case, you'll get ignored
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- project plz who can help me (C++)
- Cable Bill Program (C++)
- what does assignment statement return (C)
- help with streams .... (C++)
- who can help me in c++ project (C++)
- I need help on STRUCTURES and on input file (C)
- Input Output File Streams - Sorting Help With Array (C++)
- overloading Operator << to accept endl (C)
- transfer input.txt to output.txt help prz (C++)
Other Threads in the C Forum
- Previous Thread: Pass a string by reference
- Next Thread: random number



Hybrid Mode