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

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

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

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- Hide quoted text -

- Show quote----------------------

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
 
F

Flash Gordon

Did you not read this? If so, why did you not trim your post as requested?

Using this code i get the desired output, If still there exists any
mistake in the code ,please do guide me.

Yes, there are lots of mistakes which have already been pointed out to
you in earlier versions.
As you all suggested I dont want to use gets to input the filename,

You have already been told a number of alternatives.
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.

<snip>

Rather than thanking people actually take note and act on the advice you
are given, including the advice to trim posts. Had you put some effort
in to follow the advice given I might have put in effort to point out
additional problems, but as you do not seem to have taken note of
several things you have been told I won't bother.
 
S

santosh

(e-mail address removed) wrote:


Please don't quote these silly artefacts of Google Groups. Also trim
your post to include only relevant material. Everyone here pays for
their news service and/or Internet access. Our free time is also
limited.
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>

You don't use any declarations from string.h and ctype.h below.
void main(void)

In Standard C main *must* return an int value.
{
FILE *in1,*in2;

char file1[1000], file2[1000];

I'd use symbolic constants instead of numeric literals. It's easier to
change; an edit at one place automatically propagates wherever it is
used.
int num;
float value;

Do you have any particular reason for using float instead of double.
On most machines the latter type is usually faster, and it's
unquestionably of better precision.
char in_region=0;

printf("\n\n Input File 1: ");
gets(file1);

Are you trying to become a troll?

Multiple posters have already told you about the severe risk and
inadvisability of using gets. The use of alternatives like fgets was
also illustrated. Just above you said you didn't want to use gets. Yet
here you're using it again. You do use fgets below, so it's not that
you don't know how replace these gets calls.

Also the string supplied to printf is not terminated with a newline. I
told you previously that that's needed if you want to immediately
flush your stdout stream.
printf("\n\n Output File3: ");
gets(file2);
Ditto.

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

I also informed you that the only portable program termination status
values were 0, EXIT_SUCCESS and EXIT_FAILURE. One is *not* a portable
value. Again you've failed to heed the advice.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top