473,466 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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;

Jul 15 '07 #1
24 2888
allpervas...@gmail.com 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>

Jul 15 '07 #2
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

Jul 15 '07 #3
allpervas...@gmail.com wrote:
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.

Jul 15 '07 #4
al**********@gmail.com writes:
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.</OT>
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.

--
Ben.
Jul 15 '07 #5

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

Jul 15 '07 #6
On Sun, 15 Jul 2007 07:54:09 -0000, al**********@gmail.com wrote:
>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
Jul 15 '07 #7
al**********@gmail.com writes:
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.

--
Ben.
Jul 15 '07 #8
al**********@gmail.com wrote:
>
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

Jul 15 '07 #9
On Sun, 15 Jul 2007 13:58:57 -0000, al**********@gmail.com wrote:
>
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
Jul 15 '07 #10
On Jul 16, 2:45 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
allpervas...@gmail.com writes:
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.

--
Ben.


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


Jul 16 '07 #11
On Sun, 15 Jul 2007 18:12:14 -0700, al**********@gmail.com wrote:
>On Jul 16, 2:45 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>allpervas...@gmail.com writes:
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.

--
Ben.

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
Jul 16 '07 #12
On Jul 16, 1:04 pm, Barry Schwarz <schwa...@doezl.netwrote:
On Sun, 15 Jul 2007 18:12:14 -0700, allpervas...@gmail.com wrote:
On Jul 16, 2:45 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
allpervas...@gmail.com writes:
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.
--
Ben.
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

Jul 16 '07 #13
allpervas...@gmail.com wrote:
On Sun, 15 Jul 2007 18:12:14 -0700, allpervas...@gmail.com wrote:
<snip>
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.
<snip>
Remove del for email- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
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.

Jul 16 '07 #14
santosh wrote:
allpervas...@gmail.com wrote:
<snip>
printf("\n\n Append-to File 3: ");
gets(file3);
<snip>
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.
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.
Jul 16 '07 #15
al**********@gmail.com writes:
[...]
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, <http://www.c-faq.com/>.
{
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]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 16 '07 #16
al**********@gmail.com wrote, On 16/07/07 08:03:

<snip>
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.
--
Flash Gordon
Jul 16 '07 #17
santosh <sa*********@gmail.comwrites:
allpervas...@gmail.com 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) { ... }

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 16 '07 #18
Keith Thompson wrote:
santosh <sa*********@gmail.comwrites:
allpervas...@gmail.com 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.

Jul 16 '07 #19
On Mon, 16 Jul 2007 00:03:08 -0700, al**********@gmail.com 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
Jul 17 '07 #20
On Jul 17, 10:43 am, Barry Schwarz <schwa...@doezl.netwrote:
On Mon, 16 Jul 2007 00:03:08 -0700, allpervas...@gmail.com 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- 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

Jul 17 '07 #21
On Jul 17, 10:43 am, Barry Schwarz <schwa...@doezl.netwrote:
On Mon, 16 Jul 2007 00:03:08 -0700, allpervas...@gmail.com 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- 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

Jul 17 '07 #22
al**********@gmail.com wrote, On 17/07/07 14:54:
On Jul 17, 10:43 am, Barry Schwarz <schwa...@doezl.netwrote:
>On Mon, 16 Jul 2007 00:03:08 -0700, allpervas...@gmail.com wrote:

snip 150+ lines of obsolete code

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

<snip>
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.
--
Flash Gordon
Jul 17 '07 #23
al**********@gmail.com wrote:
On Jul 17, 10:43 am, Barry Schwarz <schwa...@doezl.netwrote:
On Mon, 16 Jul 2007 00:03:08 -0700, allpervas...@gmail.com wrote:

snip 150+ lines of obsolete code

PLEASE trim your posts.
Why did you ignore this?


Brian


Jul 17 '07 #24
allpervas...@gmail.com wrote:

<snip ~170 lines>
Remove del for email- Hide quoted text -

- Show quoted text -
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.

Jul 18 '07 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrej Hocevar | last post by:
Hello, below I've attached a test program reading data from a file and storing that information in a linked list. First problem is that current->p_name will print only the last item in the file...
6
by: Eirik | last post by:
Hey, all groovy C programmers, I've read in the FAQ(question 12.18) about how applications skip calls to (f)gets() after scanf() has been used. How can I avoid this? I know that I can by putting...
4
by: interpim | last post by:
ok the function is supposed to accept only non-negative integers... but it still accepts letters input through the function. Such as '23e' still passes through. I tried to use isalpha in the or...
6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
3
by: Tcc | last post by:
Hi all, Assume there are some data in a file "a.txt": abc def 11<---------------------data in a.txt is it possible for me to use "fgets" function to get the string "abc", "def" and "11"...
3
by: kimimaro | last post by:
hi below is my save function that is used to placed data from the C program to a text file for future usage. void save() { FILE *save; int i = 0; save=fopen("employeerecord.txt", "a+");
1
by: anonymous | last post by:
I'm having a LOT of trouble figuring out how to read input from a file into my program. This file contains lines with an arbitrary number of words in them. For example: This is one line in my file...
2
by: jou00jou | last post by:
Hi, I have trouble using sscanf and fgets to check for overflow. I will post the assignment specification so I could help whoever would kindly like to offer his/her help. ...
2
by: jorba101 | last post by:
I'm having following trouble: I'm reading contents of a string retrieved from a CSV-like file with sscanf, in following way: sscanf( originalString, "%d,%s,%s", &myInteger, myString1, myString2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.