problems using fgets() and sscanf() while modifying file contents

A

allpervasive

hi all, this is reddy, a beginner to c lang,,here i have some problems
in reading and modifying the contents of a file,, hope you can help to
solve this problem. Here i attach the file to be modified and the
program code.
In the attached file below i just want to change the value of
data(only float value) after the line 1 P V T 1 15 till 2 G TT,
from positive to negative and vice versa, and wire the date in other
file. can someone help me to solve this.
thanks.


*** MEAS(T,L) DATA ***
/07/07

T
8
PINION
3
2
2
119.6329
-113.2538
74.8409
2.0000
1 P V T 1 15
20 -6.2
21 -4.0
22 0.7
1 P V L 1 5
23 3.9
24 -5.1
25 5.2
26 -5.7
1 P V T 3 15
27 5.9
28 -5.2
2 G TT
1 1.2488
3 1.2598
END


program code:


do {

if (c=fgets(file1, sizeof file1, in1) !='\n'
&& sscanf(file1,"%d %f",&num,&value) == 2)
{
value= (value- (2*value));
printf("%d %1.1f\n", num, value);
fprintf(append3,"%d %1.1f\n", num, value);
}
else
{
for(j=0;;j++){
printf("%c",file1[j]);
fprintf(file2,"%c",file1[j]);
if(file1[j]=='\n')break;}

}

} while (!feof(in1));

return EXIT_SUCCESS;
 
S

santosh

(e-mail address removed) wrote:

[ ... ]
In the attached file below i just want to change the value of
data(only float value) after the line 1 P V T 1 15 till 2 G TT,
from positive to negative and vice versa, and wire the date in other
file. can someone help me to solve this.
thanks.

*** MEAS(T,L) DATA ***
/07/07

T
8
PINION
3
2
2
119.6329
-113.2538
74.8409
2.0000
1 P V T 1 15
20 -6.2
21 -4.0
22 0.7
1 P V L 1 5
23 3.9
24 -5.1
25 5.2
26 -5.7
1 P V T 3 15
27 5.9
28 -5.2
2 G TT
1 1.2488
3 1.2598
END


program code:

This is not a program. This is just a code snippet.
do {

if (c=fgets(file1, sizeof file1, in1) !='\n'

fgets returns a null pointer, if end-of-file or an error occurs,
otherwise it returns file1. Here you're checking it's return value
against the newline character. The result of this test, either 1 or 0
for true or false gets assigned to c. ITYM something else.
&& sscanf(file1,"%d %f",&num,&value) == 2)
{
value= (value- (2*value));
printf("%d %1.1f\n", num, value);
fprintf(append3,"%d %1.1f\n", num, value);
}
else
{
for(j=0;;j++){
printf("%c",file1[j]);
fprintf(file2,"%c",file1[j]);
if(file1[j]=='\n')break;}

}

} while (!feof(in1));

You need to check the fgets call for a null pointer return. That could
be because of both end-of-file, (true if feof() returns true), or
other error, (true if ferror() returns true). So here you should check
for both outcomes.

<snip>
 
A

allpervasive

ya i checked the fgets using null pointer also,, but the there is no
change,,
actually iam able to change the data(float value) from +ve to -ve and
vice-versa but the problem is that even the data before the line
"1 P V T 1 15" and after the line "2 G TT" gets changed as shown
below, so i need a better approach to solve that bug. thanks

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 -0.6
-113 -0.3
74 -0.8
2 0.0
1 P V T 1 15
20 6.2
21 4.0
22 -0.7
..
..
2 G TT
1 -1.2
3 -1.3
END
END
 
S

santosh

ya i checked the fgets using null pointer also,, but the there is no
change,,
actually iam able to change the data(float value) from +ve to -ve and
vice-versa but the problem is that even the data before the line
"1 P V T 1 15" and after the line "2 G TT" gets changed as shown
below, so i need a better approach to solve that bug. thanks

<snip>

I'll suggest a general algorithm:

Read each line of data file
If line matches desired pattern, (in this case, "1 P VT 1 15")
If next line _does not_ match closing pattern, (i.e., "2 G
TT")
Convert the line into appropriate numerical values
Change the value you want to change
Write out the modified values
Repeat from step three above

Basically you need to enter your main modification loop only after you
detect the opening pattern and exit it after you detect the closing
pattern. Before the opening pattern and after the closing pattern you
should write out the file unmodified. I suggest encapsulating the
modification code into a separate function. The patterns can be
detected with the standard string functions in string.h.
 
B

Ben Bacarisse

hi all, this is reddy, a beginner to c lang,,here i have some problems
in reading and modifying the contents of a file,, hope you can help to
solve this problem. Here i attach the file to be modified and the
program code.
In the attached file below i just want to change the value of
data(only float value) after the line 1 P V T 1 15 till 2 G TT,
from positive to negative and vice versa, and wire the date in other
file. can someone help me to solve this.
thanks.

I know this is comp.lang.c but there are cases where it is worth
suggesting another route: <OT>Perl and AWK can this kind of thing with
ease. If you are fluent in C, so can C, but the time spent learning
one of these other tools will pay off big time over a lifetime. said:
program code:

A whole program is more useful.
do {
if (c=fgets(file1, sizeof file1, in1) !='\n'
&& sscanf(file1,"%d %f",&num,&value) == 2)
{
value= (value- (2*value));
printf("%d %1.1f\n", num, value);
fprintf(append3,"%d %1.1f\n", num, value);
}
else
{
for(j=0;;j++){
printf("%c",file1[j]);
fprintf(file2,"%c",file1[j]);
if(file1[j]=='\n')break;}
}

} while (!feof(in1));

Using "do" is almost always wrong -- it tends to twist program logic.
Using feof to end an input loop is also almost always wrong (see the
FAQ about that).

You want to do something for every line read so say that:

while (fgets(file1, sizeof file1, in1) != NULL) {

(You will have problems if there are lines longer than will fit in
your buffer, but let's leave that for now.)

You will need to know if you are in the region of interest, so you will
have a variable to record that fact.

if (in_region && sscanf(file1, "%d %f", &num, &value) == 2) {
printf("%d %1.1f\n", num, -value);
fprintf(append3,"%d %1.1f\n", num, -value);
continue;
}

I put a "continue" in there because I know we have done all we need to
do with such a line[1].

You need to note coming into and leaving the region of interest:

if (file1[2] == 'P')
in_region = 1;
else if (file1[2] == 'G')
in_region = 0;

And, lastly, you need to print any data not yet dealt with. You don't
need a loop to that, fputs is there to help:

fputs(file1, stdout);
fputs(file1, file2);
}

I am not sure what "file2" and "append3" are, but I assumed those
parts are correct.

[1] I am not a fan of continue, but it is handy in some explanations.
 
A

allpervasive

thanks a lot for ur suggestions,, now i could get the desired output..
but i still have a small question,,how can i print a new line using
fprintf

using- fprintf(append3,"%d %1.1f\n", num, -value); iam unable to
get the output in a new line, instead i get
it in a single line. could u please help me how to do that. I also
tried to use fprintf(append3, "\n"); but still i couldnt get it.

thanks
 
B

Barry Schwarz

hi all, this is reddy, a beginner to c lang,,here i have some problems
in reading and modifying the contents of a file,, hope you can help to
solve this problem. Here i attach the file to be modified and the
program code.

You obviously know that sentences end with a period and how to find
the shift key on your keyboard. Is there,,some,,reason,,you
deliberately chose to make your text difficult to read?
In the attached file below i just want to change the value of
data(only float value) after the line 1 P V T 1 15 till 2 G TT,
from positive to negative and vice versa, and wire the date in other
file. can someone help me to solve this.
thanks.

What problem are you having? You don't show the code that checks for
the two lines of interest. You don't tell us what values you see in
your output file. Your code violates the rules of the language. If
you want help, you need to give us enough information so we don't have
to guess.

snip input data

I have reformatted your code to make it readable. Please indent
consistently. Also, for code you post, use spaces, not tabs.
program code:


do {

if (c=fgets(file1, sizeof file1, in1) !='\n'
&& sscanf(file1,"%d %f",&num,&value) == 2)

You are missing parentheses in this expression. As a result, it is
not doing what you intend. The operators with the highest precedence
in this expression are != and ==. That causes your expression to be
evaluated as
c = (fgets(...) != '\n') && (sscanf(...) == 2)
The next operator with the highest precedence is && which results in
c = ((fgets(...) != '\n') && (sscanf(...) == 2))
Note that this assigns to c the result of a boolean expression, which
will always be 0 or 1.

Furthermore fgets returns a pointer. '\n' is an int. You cannot
compare a pointer to an int without an explicit conversion, which you
would not want to do anyway. Your compiler should have issued a
diagnostic here. Did it? Why did you ignore it? Since you don't use
c in the posted code, I cannot tell if you wanted to compare the
return from fgets to NULL or if you really wanted to use fgetc to
extract a single character. Since you do use file1 in the expression
after the &&, I assume the former and your code should look something
like
if ((fgets(...) != NULL) && (sscanf(...) == 2))
Technically, the two sets of internal parentheses surrounding the two
operands of && are superfluous but until you become more familiar with
precedence they are cheap insurance.
{
value= (value- (2*value));

This is "equivalent" to
value = -value;
which matches your stated intent and has the additional virtue of
avoiding "rounding errors".
printf("%d %1.1f\n", num, value);
fprintf(append3,"%d %1.1f\n", num, value);
}
else
{
for(j=0;;j++){
printf("%c",file1[j]);
fprintf(file2,"%c",file1[j]);

putc and fputc would be simpler here.
if(file1[j]=='\n')break;}

You have a logic error here. fgets stores a \n in the input buffer
only if the input "line" is shorter than the buffer. You also need to
terminate the for loop if j exceeds the size of the buffer. The
easiest way to do that is to populate the second clause of the for
statement with something like j < sizeof file1 - 1. (Think about why
you never want to process file1[sizeof file1 - 1] in this loop.)
}

} while (!feof(in1));

Your description said you wanted to stop this conversion at 2 G TT.
The loop you show us has no code to check for this line.

There is another logic error here. fgets can return NULL for reasons
other than end of file. If you experience an I/O error, this code
will continue the loop and try to read more data from a broken stream.
return EXIT_SUCCESS;


Remove del for email
 
B

Ben Bacarisse

thanks a lot for ur suggestions,, now i could get the desired output..
but i still have a small question,,how can i print a new line using
fprintf

using- fprintf(append3,"%d %1.1f\n", num, -value); iam unable to
get the output in a new line, instead i get
it in a single line. could u please help me how to do that. I also
tried to use fprintf(append3, "\n"); but still i couldnt get it.

Sounds odd. Please post a short program (maybe just main with a file
open and an fprintf call?) that exhibits this problem. Here,

#include <stdio.h>

int main(void)
{
FILE *f = fopen("out", "w");
if (f) {
fprintf(f, "two\nlines\n");
fclose(f);
}
return 0;
}

does exactly what I'd expect it to.
 
D

Default User

thanks a lot for ur suggestions,, now i could get the desired output..
but i still have a small question,,how can i print a new line using
fprintf

Please quote a representative sample of the previous message to give
context to your reply. Google Groups does that automatically for you.
When you do, trim the quotes to leave the necessary part and interleave
your replies.

See the vast majority of other posts in the newsgroup.




Brian
 
B

Barry Schwarz

thanks a lot for ur suggestions,, now i could get the desired output..
but i still have a small question,,how can i print a new line using
fprintf

using- fprintf(append3,"%d %1.1f\n", num, -value); iam unable to
get the output in a new line, instead i get
it in a single line. could u please help me how to do that. I also
tried to use fprintf(append3, "\n"); but still i couldnt get it.
What do you want on a new line?

Is the text from this fprintf showing up on the same line as a
previous fprintf? If so, that fprintf probably doesn't end with a \n.
You could add a \n at the beginning of this fprintf but fixing the
previous one is usually a better idea.

Do you want the two values from this frpintf on different
lines? If so, then insert a \n between the two format conversions.

If you want help with a problem, please provide enough detail:
what is the current result.
what is the desired result.
if it is not immediately very obvious, what is the difference
between the two.
cut and paste the code which is causing the problem,
preferably as a ready to compile and run complete program.

And unless you want to be Ignored,,as,,delIberately,,obnoxIous, you
wIll make some effort to use readable EnglIsh


Remove del for email
 
A

allpervasive

Sounds odd. Please post a short program (maybe just main with a file
open and an fprintf call?) that exhibits this problem. Here,

#include <stdio.h>

int main(void)
{
FILE *f = fopen("out", "w");
if (f) {
fprintf(f, "two\nlines\n");
fclose(f);
}
return 0;

}

does exactly what I'd expect it to.



inspite of using \n in fprintf(append3,"%d %1.1f\n", num, -value); i
dont get the output in a new line when the loop continues.
i get the desired output using printf, but by using fprintf \n doesnt
work.
details of code and output are shown below. thanks.



code:
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(append3,"%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, 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
END


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
END


desired 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
 
B

Barry Schwarz

inspite of using \n in fprintf(append3,"%d %1.1f\n", num, -value); i
dont get the output in a new line when the loop continues.
i get the desired output using printf, but by using fprintf \n doesnt
work.
details of code and output are shown below. thanks.



code:
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(append3,"%d %1.1f\n", num, -value);

According to this code, you only write to append3 if in_region is
non-zero and sscanf returns two. I have no idea what the original
state of in_region is but it apparently must be non-zero since you
claim append3 has a copy of the first two input lines. However,
sscanf cannot parse either of these two lines with the format
conversions you provide. It must return zero for line 1 since * is
neither white space nor an allowable character in an int or float. It
must return zero for line 2 since / is equally unsuitable.

Either this is not the code you are executing or you are not showing
us append3.

It gets worse. In your calls to printf and fprintf, there is a space
between the %d and %f. This space must be present in the output. When
sscanf processes the four lines before the P, the %d will stop at the
decimal point and the %f will pick up from there. So in the first of
these four, num would be set to 119 and value to .6329 The output
from printf and fprintf must be a line that looks like
119 -0.6329
Since your output doesn't look anything like this, the question
remains: What are you showing us?
continue;
}

if (file1[2] == 'P')
in_region = 1;
else if (file1[2] == 'G')
in_region = 0;

fputs(file1, stdout);
fputs(file1, file2);

So file1 one is a buffer (probably char[]) while file2 is a FILE*.
With naming conventions like this, it is no wonder you are confused.
}



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


output data:

What tool are you using to show us append3? How is it opened in your
C code?

We really need to see the complete function.
*** 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
END


Remove del for email
 
A

allpervasive

inspite of using \n in fprintf(append3,"%d %1.1f\n", num, -value); i
dont get the output in a new line when the loop continues.
i get the desired output using printf, but by using fprintf \n doesnt
work.
details of code and output are shown below. thanks.
code:
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(append3,"%d %1.1f\n", num, -value);

According to this code, you only write to append3 if in_region is
non-zero and sscanf returns two. I have no idea what the original
state of in_region is but it apparently must be non-zero since you
claim append3 has a copy of the first two input lines. However,
sscanf cannot parse either of these two lines with the format
conversions you provide. It must return zero for line 1 since * is
neither white space nor an allowable character in an int or float. It
must return zero for line 2 since / is equally unsuitable.

Either this is not the code you are executing or you are not showing
us append3.

It gets worse. In your calls to printf and fprintf, there is a space
between the %d and %f. This space must be present in the output. When
sscanf processes the four lines before the P, the %d will stop at the
decimal point and the %f will pick up from there. So in the first of
these four, num would be set to 119 and value to .6329 The output
from printf and fprintf must be a line that looks like
119 -0.6329
Since your output doesn't look anything like this, the question
remains: What are you showing us?
continue;
}
if (file1[2] == 'P')
in_region = 1;
else if (file1[2] == 'G')
in_region = 0;
fputs(file1, stdout);
fputs(file1, file2);

So file1 one is a buffer (probably char[]) while file2 is a FILE*.
With naming conventions like this, it is no wonder you are confused.




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
output data:

What tool are you using to show us append3? How is it opened in your
C code?

We really need to see the complete function.






*** 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
END

Remove del for email- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -


--------------------------
sorry for confusing you, i did a small mistake. now, here i attach the
exact code which i used.
hope now you can follow the code. input data and the desired output is
also shown below.
thanks

code:

void main()
{
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);

printf("\n\n Append-to File 3: ");
gets(file3);

if((in1 = fopen(file1,"rb")) == NULL)
{
printf("Can't open input file %s",file1);
exit(1); /* Exit Program */
}


if((file2 = fopen(file3,"a+b")) == NULL)
{
printf("Can't open input file %s",file3);
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(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);
}
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
END


present 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
END


desired 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
 
F

Flash Gordon

(e-mail address removed) wrote, On 16/07/07 08:03:

sorry for confusing you, i did a small mistake. now, here i attach the
exact code which i used.
hope now you can follow the code. input data and the desired output is
also shown below.
thanks

code:

void main()

The standard does not define a return type of main, if you
implementation does not then it is undefined behaviour (anything can
happen, including it causing your bowels to void, if your implementation
does then using it is still not portable.

int main(void)

Note also be explicit about not taking parameters.
{
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 */

As you have not included stdio.h this invokes undefined behaviour.
Include it then see the next comment.

There is no guarantee that the prompt will be displayed immediately
since it does not any with a newline. Ti improve your chances flush stdout.
gets(file1);

DO NOT USE GETS!!!

Search the archives of the group, then use fgets or one of the many open
source alternatives written in standard C that you can include in your
program.
printf("\n\n Append-to File 3: ");
gets(file3);

if((in1 = fopen(file1,"rb")) == NULL)

Is it really a binary file? I thought you were dealing with text files.
If it is text get rid of the "b".
{
printf("Can't open input file %s",file1);
exit(1); /* Exit Program */

This is a non-portable return value and on some real systems it actually
means success.
exit(EXIT_FAILURE);
You will need to include stdlib.h
}


if((file2 = fopen(file3,"a+b")) == NULL)

Is it really a binary file? I thought you were dealing with text files.
If it is text get rid of the "b".
{
printf("Can't open input file %s",file3);
exit(1); /* Exit Program */
}

while (fgets(file1, sizeof file1, in1) != NULL) {

You don't check if a complete line was read.
if (in_region && sscanf(file1, "%d %f", &num, &value) == 2) {
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);
}
fclose(in1);
fclose(file2);
}

<snip>

Your indentation is inconsistent and misleading, and your variable names
are terrible.

Fix everything listed including your indentation and using sensible
variable names, then see where you are. I don't have the time to
reformat your code to make it readable.
 
S

santosh

Don't quote the above.
sorry for confusing you, i did a small mistake. now, here i attach the
exact code which i used. hope now you can follow the code. input data
and the desired output is also shown below.
thanks

code:

void main()

Unless you know what you're doing, you should probably use either one
of:

int main(void)

or

int main(int, char**)

The second form is necessary if you need to access arguments supplied
to the program from it's startup environment.
{
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 */

Be aware that unless you terminate the argument supplied to printf
with a newline character, output may not immediately appear on the
stdout stream.
gets(file1);

This function cannot be safely used and it is an extremely bad idea to
use it, for whatever purpose. fgets is equally simple to use and a
much safer. In this instance the replacement could be like:

if(fgets(file1, sizeof file1, stdin) == NULL)
deal_with_error();
else
continue_processing();

You should also check the buffer supplied to fgets for the presence of
a newline character. If it's not present, fgets was only able to read
in a part of the input, since, unlike gets, it respects buffer sizes.
The rest of the line can be read in with another call to fgets.
printf("\n\n Append-to File 3: ");
gets(file3);

Same as above.
if((in1 = fopen(file1,"rb")) == NULL)

Here you're passing your input to fopen without any checking.
{
printf("Can't open input file %s",file1);
exit(1); /* Exit Program */

One is not a portable value for exit or return from main. The portable
values are 0, EXIT_SUCCESS, (both indicate normal program
termination), and EXIT_FAILURE, to indicate abnormal program
termination. The two macros are defined in stdlib.h.
}

if((file2 = fopen(file3,"a+b")) == NULL)

As per your code, the file3 array's contents are indeterminate. By
using uninitialised objects here you're invoking undefined behaviour.
From this point on, your program is allowed to behave in any way possible.

{
printf("Can't open input file %s",file3);
exit(1); /* Exit Program */
}

while (fgets(file1, sizeof file1, in1) != NULL) {

if (in_region && sscanf(file1, "%d %f", &num, &value) == 2) {

Here in_region is also uninitialised.
printf("%d %1.1f\n", num, -value);
fprintf(file2,"%d\n %1.1f\n", num, -value);

The strings supplied to printf and fprintf are inconsistent.
Specifically, the one supplied to fprintf has an additional newline
character. Is this what you want?

Also you should check fprintf for error, since you're presumably,
writing to a disk file.
continue;
}
if (file1[2] == 'P')
in_region = 1;

else if(file1[2] == 'G')
in_region = 0;

This check should logically occur before the IF statement above it.
fputs(file1, stdout);
fputs(file1, file2);

You should check these for write errors.
Also this should probably be a part of the ELSE-IF construct above.
Your code formatting is atrocious. If you want people to volunteer to
help you, you should try to make things as simple as possible.
}
fclose(in1);
fclose(file2);

Check these for failure too.

<snip>

Well, implement the changes I and the others have suggested and
recompile and see.
 
S

santosh

santosh said:
(e-mail address removed) wrote:



As per your code, the file3 array's contents are indeterminate. By
using uninitialised objects here you're invoking undefined behaviour.
From this point on, your program is allowed to behave in any way
possible.

Very sorry about this mistatement. I failed to notice that you'd
populated file3 with the call to gets above.

The rest of my comments still stand.

{
printf("Can't open input file %s",file3);
exit(1); /* Exit Program */
}

while (fgets(file1, sizeof file1, in1) != NULL) {

if (in_region && sscanf(file1, "%d %f", &num, &value) == 2) {

Here in_region is also uninitialised.
printf("%d %1.1f\n", num, -value);
fprintf(file2,"%d\n %1.1f\n", num, -value);

The strings supplied to printf and fprintf are inconsistent.
Specifically, the one supplied to fprintf has an additional newline
character. Is this what you want?

Also you should check fprintf for error, since you're presumably,
writing to a disk file.
continue;
}
if (file1[2] == 'P')
in_region = 1;

else if(file1[2] == 'G')
in_region = 0;

This check should logically occur before the IF statement above it.
fputs(file1, stdout);
fputs(file1, file2);

You should check these for write errors.
Also this should probably be a part of the ELSE-IF construct above.
Your code formatting is atrocious. If you want people to volunteer to
help you, you should try to make things as simple as possible.
}
fclose(in1);
fclose(file2);

Check these for failure too.

<snip>

Well, implement the changes I and the others have suggested and
recompile and see.
 
K

Keith Thompson

sorry for confusing you, i did a small mistake. now, here i attach the
exact code which i used.
hope now you can follow the code. input data and the desired output is
also shown below.
thanks

code:

void main()

main returns int, not void. The correct declaration is
int main(void)

See questions 11.12a, 11.12b, 11.14a, 11.14b, and 11.15 in the
comp.lang.c FAQ said:
{
FILE *in1,*file2;

You haven't shown us your actual code. Without a '#include <stdio.h>',
the compiler doesn't know what a 'FILE' is; it would have rejected
your program with an error message, something like "Undeclared identifier".

You need to post your *exact* code, just as you fed it to the
compiler.
char file1[1000], file3[1000];
int num;
float value;
char in_region;

printf("\n\n Input File 1: "); /* Ask for File names */
gets(file1);

Never use gets(). See question 12.23 in the FAQ.

[snip]
 
K

Keith Thompson

santosh said:
(e-mail address removed) wrote: [...]
void main()

Unless you know what you're doing, you should probably use either one
of:

int main(void)

or

int main(int, char**)

The second form is necessary if you need to access arguments supplied
to the program from it's startup environment.
[...]

In a function definition, you need names for the parameters:

int main(int argc, char **argv) { ... }
 
S

santosh

Keith said:
santosh said:
(e-mail address removed) wrote: [...]
void main()

Unless you know what you're doing, you should probably use either one
of:

int main(void)

or

int main(int, char**)

The second form is necessary if you need to access arguments supplied
to the program from it's startup environment.
[...]

In a function definition, you need names for the parameters:

int main(int argc, char **argv) { ... }

Yes thanks, I know that. My example was meant as a generic
declaration. It'd would probably have been less confusing to write it
out as a definition, as you've done.
 
B

Barry Schwarz

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.
thanks

code:

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
END


present 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
END


desired 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top