Results 1 to 1 of 1
Hey everyone,
I'm brand new in programming with C. I need to read 3 columns from a file and do calculations on that. Here is the code which I tried ...
- 08-23-2011 #1Just Joined!
- Join Date
- Aug 2011
- Posts
- 1
reading columns from file
Hey everyone,
I'm brand new in programming with C. I need to read 3 columns from a file and do calculations on that. Here is the code which I tried to reach a result. I don't know whether I'm in the correct way or not.
I want to that just 3rd, 4th and 5th column in the input file. I also don't want the header line, and unfortunately I couldn't find a way how to get rid of that line.
Any help will be appreciated.
Input file:
1498 Water Cubic Box (24.662 Ang, 499 SPC 1 Cl)
1 O 9.499321 11.110239 -10.973059 1 2 3
2 H 8.921068 10.650441 -10.364419 2 1
3 H 9.780605 11.889890 -10.494253 2 1
4 O 11.888238 -5.534349 11.019181 1 5 6
5 H 12.657229 -5.480351 10.451756 2 4
6 H 12.216792 -5.312415 11.890405 2 4
7 O 9.826543 -4.179970 4.929116 1 8 9
8 H 8.993242 -4.005135 5.366465 2 7
9 H 10.009277 -3.382000 4.433049 2 7
10 O 4.357144 10.554913 8.930376 1 11 12
Code:
#include <stdio.h>
int main()
{
double a[2],c[20],d[20],e[20],f[2],g[2],h[2];
int i, n, x, z;
char b[2];
char line[50];
FILE *fp;
fp = fopen("input.txt", "r+");
i=0;
while ( fgets( line, sizeof line, "input.txt" ) != NULL ) {
{
if ( sscanf( line, "%f%s%f%f%f%f%f%f", &a[i], b[i], &c[i], &d[i], &f[i], &g[i], &h[i]) == 8 ) {
++i;
}
printf("%f\n%d\n%f\n%f\n%f\n%f\n%f\n%f\n",a,b,c,d, e,f,g,h);
fclose(fp);
return 0;
}Last edited by humank; 08-23-2011 at 07:37 PM.


Reply With Quote