A
allpervasive
On Mon, 16 Jul 2007 00:03:08 -0700, (e-mail address removed) wrote:
snip 150+ lines of obsolete code
PLEASE trim your posts.
sorry for confusing you, i did a small mistake. now, here i attach the
exact code which i used.
No it's not.
hope now you can follow the code. input data and the desired output is
also shown below.
thankscode:
Where are your includes?
void main()
int main(void)
{
FILE *in1,*file2;char file1[1000], file3[1000];
int num;
float value;
char in_region;printf("\n\n Input File 1: "); /* Ask for File names */
gets(file1);
Don't use gets.
printf("\n\n Append-to File 3: ");
gets(file3);
When asking for free help, it is customary to make things as easy as
possible for the people responding. One very simple thing is to make
your code readable by INDENTING CONSISTENTLY.
if((in1 = fopen(file1,"rb")) == NULL)
Why binary?
{
printf("Can't open input file %s",file1);
exit(1); /* Exit Program */
Use EXIT_FAILURE, not 1.
if((file2 = fopen(file3,"a+b")) == NULL)
How many times have you tested your code? Do you remember to delete
your output file each time? Since you always append data to the file,
it is entirely possible that what you consider bad output is residual
from previous tests.
{
printf("Can't open input file %s",file3);
This error message should reflect that file3 is the output file.
exit(1); /* Exit Program */
}while (fgets(file1, sizeof file1, in1) != NULL) {if (in_region && sscanf(file1, "%d %f", &num, &value) == 2) {
in_region was never initialized with or assigned a value. This
statement invokes undefined behavior. Many compilers will generate a
diagnostic for this condition. Did yours?
printf("%d %1.1f\n", num, -value);
fprintf(file2,"%d\n %1.1f\n", num, -value);
continue;
}
if (file1[2] == 'P')
in_region = 1;else if(file1[2] == 'G')
in_region = 0;fputs(file1, stdout);
fputs(file1, file2);
On those occasions when in_region is not zero and sscanf returns two,
you not only printf and fprintf your numeric values but you repeat the
input line. You do this to both stdout and to file2. The output you
show us below does not have this repeated data. That brings us back
to the original question - Where is your real code and where is your
real output?
}
fclose(in1);
fclose(file2);
}original data:*** MEAS(T,L) DATA ***
/07/07
.
.
119.6329
-113.2538
74.8409
2.0000
1 P V T 1 15
20 -6.2
21 -4.0
22 0.7
.
.
2 G TT
1 1.2488
3 1.2598
ENDpresent output data:*** MEAS(T,L) DATA ***
/07/07
.
.
119.6329
-113.2538
74.8409
2.0000
1 P V T 1 15
20 6.221 4.022 -0.7
.
.
2 G TT
1 1.2488
3 1.2598
ENDdesired output:*** MEAS(T,L) DATA ***
/07/07
.
.
119.6329
-113.2538
74.8409
2.0000
1 P V T 1 15
20 6.2
21 4.0
22 -0.7
.
.
2 G TT
1 1.2488
3 1.2598
END
Remove del for email- Hide quoted text -
- Show quoted text -
----------------------
Using this code i get the desired output, If still there exists any
mistake in the code ,please do guide me.
As you all suggested I dont want to use gets to input the filename,
instead i want to input the filename during run time by giving the
path of the file, how can I do that.
Thanks one and all for your kind help.
The present code that iam using:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
void main(void)
{
FILE *in1,*in2;
char file1[1000], file2[1000];
int num;
float value;
char in_region=0;
printf("\n\n Input File 1: ");
gets(file1);
printf("\n\n Output File3: ");
gets(file2);
if((in1 = fopen(file1,"r")) == NULL)
{
printf("Can't open input file %s",file1);
exit(1); /* Exit Program */
}
if((in2 = fopen(file2,"a")) == NULL)
{
printf("Can't open input file %s",file2);
exit(1); /* Exit Program */
}
while (fgets(file1, sizeof file1, in1) != NULL) {
if (in_region && sscanf(file1, "%d %f", &num, &value) == 2) {
printf("%d %1.1f\n", num, -value);
fprintf(in2,"%d %1.1f\n", num, -value);
continue;
}
if (file1[2] == 'P')
in_region = 1;
else if(file1[2] == 'G')
in_region = 0;
fputs(file1, stdout);
fputs(file1, in2);
}
fclose(in1);
fclose(in2);
}
original data:
*** MEAS(T,L) DATA ***
/07/07
..
..
119.6329
-113.2538
74.8409
2.0000
1 P V T 1 15
20 -6.2
21 -4.0
22 0.7
..
..
2 G TT
1 1.2488
3 1.2598
END
modified data:
*** MEAS(T,L) DATA ***
/07/07
..
..
119.6329
-113.2538
74.8409
2.0000
1 P V T 1 15
20 6.2
21 4.0
22 -0.7
..
..
2 G TT
1 1.2488
3 1.2598
END