?
Solved

Reading and Writing not working??

Posted on 2005-03-22
5
Medium Priority
?
295 Views
Last Modified: 2010-04-15
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");
}

0
Comment
Question by:agtmulder17
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 5

Expert Comment

by:beryl666
ID: 13603834
f1 = fopen ("east_point.txt", "r");
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 40 total points
ID: 13604086
AS a general rule,if every third or fourth line in your program isnt something like:

if( somevar == Error_Code ) bomb("function xxx failed")

... then you're not doing enough error checking.

In your particlar case, fopen(), fgets(), might be returning interesting error codes.  Even printf() and fclose(0 sometimes return VERY interesting and unexpected errors.

0
 
LVL 5

Accepted Solution

by:
beryl666 earned 60 total points
ID: 13604088
in > east_point.txt
77643
2368803.69186
6944484.47132   >> please press enter here before save

---------------------------------------
coding:

#include <stdio.h>
#include <conio.h>
void a2a(char *name1)
{
char line[81];
char name2[80];
int i=0;
float x1, x2;
FILE *f1, *f2;

f1 = fopen ("east_point.txt", "r");

while(!feof(f1)) {
 fgets(line, 80, f1);
   if (sscanf(line,"%d %f %f", &i, &x1, &x2) == 2) {
       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");
   getch();
}

Try this... i can get the 2 crated text files.
hope this help.
0
 

Author Comment

by:agtmulder17
ID: 13604776
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?

0
 
LVL 5

Expert Comment

by:beryl666
ID: 13607866
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?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question