473,770 Members | 5,977 Online
Bytes | Software Development & Data Engineering Community
+ 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",fil e1[j]);
fprintf(file2," %c",file1[j]);
if(file1[j]=='\n')break;}

}

} while (!feof(in1));

return EXIT_SUCCESS;

Jul 15 '07
24 2972
On Jul 17, 10:43 am, Barry Schwarz <schwa...@doezl .netwrote:
On Mon, 16 Jul 2007 00:03:08 -0700, allpervas...@gm ail.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...@gm ail.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**********@gm ail.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...@gm ail.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**********@gm ail.com wrote:
On Jul 17, 10:43 am, Barry Schwarz <schwa...@doezl .netwrote:
On Mon, 16 Jul 2007 00:03:08 -0700, allpervas...@gm ail.com wrote:

snip 150+ lines of obsolete code

PLEASE trim your posts.
Why did you ignore this?


Brian


Jul 17 '07 #24
allpervas...@gm ail.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
3047
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 for each item. Where is the problem? Is it in the sscanf? Whay can't I abandon tmp_name and just use p_name instead in that sscanf? Second, the order of the output (with a file of 3 lines) is 1, 2, 0, why? And why is the item 0 empty? I'm new to...
6
5608
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 the fgets() before the scanf(). However, this one is not always suitable. Do you know of any other ways of avoiding this problem? -- - Fordi det rotar til måten folk vanlegvis les tekst på.
4
2162
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 statement to cause an error, but it causes an error for everything input. How do I make an error occur when anything other than numbers are put in? #include <stdio.h> #define MAXLINE 20 int main(void) { char line; int error,n; do{...
6
3502
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 written. /* Book name : File name : E:\programs\cpp\iti01\ch10\ex09_5p1.cpp Program discription: Adding name,Address to customer_record SETUP PROGRAM
3
1349
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" individulely? and How?
3
2301
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
4288
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 However the next line has far more words in it This has the least I'm trying to read each whole line, then read each word from the line, as follows: while (fgets(line, MAX_LINE_LEN, inputfile) != NULL)
2
3090
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. ____________________________________________________________________________________ 1) The program should expect 2 decimal integers per line. Let's call these n1 and n2. 2) For each such input line it should output the value of n1 + 2 * n2 (followed by a newline...
2
3985
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 ); originalString contents are something like: char originalString="1,my text here 1,my text here 2";
0
9592
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.