Link to home
Start Free TrialLog in
Avatar of agtmulder17
agtmulder17

asked on

Reading and Writing not working??

Hi, I can't seem to get my program to do what I need it to. This program is supposed to open and read my file east_points, check for the line configuration, create a new file and put info into the file. No errors when I complie or execute, except no new *.txt files are created. Not sure what is happing. I have used this code in previous research efforts, but it isn't working for me today???Example of each line of east_points is:    

77643,2368803.69186,6944484.47132     Below is my code, any help would be appreciated! -Jason

#include <stdio.h>

void a2a(char *name1)
{
char line[81];
char name2[80];
int i;
float x1, x2;
FILE *f1, *f2;

f1 = fopen ("east_points", "r");

while(!feof(f1)) {
 fgets(line, 80, f1);
   if (sscanf(line,"%d %e %e", &i, &x1, &x2) == 3) {
       sprintf(name2,"east%d.txt", i);
       f2 = fopen(name2, "w");
       fprintf(f2, "%d,%e,%e\nend\n", i, x1, x2);
       fclose(f2);
    }
  }
  fclose(f1);
}


main()
{
   a2a("AA.txt");
}

Avatar of beryl666
beryl666
Flag of United States of America image

f1 = fopen ("east_point.txt", "r");
SOLUTION
Avatar of grg99
grg99

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of agtmulder17
agtmulder17

ASKER

beryl666,

I couldn't get your code to work, but it looks like the problem is with my file east_points. The file is comma delimited, but the program reads it as tab delimited. I changed the code and it produces my files, thus executes correct, with just one hitch.

I can't seem to get the precision correct. For the first line of east_points:
46232,2372708.00682,6939554.26065

my newly created file east46232.txt gives,
46232,2.372708e+06,6.939554e+06

which obviously loses my precision. I have tried %13.5e in the code to try and control for this but it doesn't execute correctly. Any thoughts on controling the precison of my output?

First of all i would like to know why you put  "AA.txt" while you never use?
secondly, may i know what is the exact input and and what is the exact output that you want.

when i set (1 file generate)
if (sscanf(line,"%d %f %f", &i, &x1, &x2) == 1)
"east46232.txt" is generated and output is the file is
46232,1.208627e+001,2.598459e+020
end

when i set (2 file generate)
if (sscanf(line,"%d %f %f", &i, &x1, &x2) == 2)
"east2372708.txt" is generated output
2372708,6.820000e-003,2.598459e+020
end
"east6939554.txt" is generated output
6939554,2.606500e-001,2.598459e+020
end

when i ser
if (sscanf(line,"%d %f %f", &i, &x1, &x2) == 3)
nothing generated

is this what you want?