I've problem with my file handling program. See this

R

Rajen

When I run this program.
#include<stdio.h>

int main()
{
FILE *fp;

int i;
float f;
char str[20];

if( ( fp=fopen("stuff", "w")) == NULL )
{
printf("Sorry! Unable to create/read stuff");
exit(-1);
}

for( i=0; i<=10; i++ )
fprintf(fp, "%d:%s:%.2f\n", i, "Hello", 12.3f);

for( i=0; i<=5; i++)
{
fscanf( fp, "%d:%s:%.2f\n" , &i, &str, &f);
printf("Hi %s %d %f\n", str, i, f);
}

fclose(fp);
return 0;
}

This is the output:
$./a.out
Hi |àÇù¿-F¦Yâ 0 0.000000
Hi |àÇù¿-F¦Yâ 1 0.000000
Hi |àÇù¿-F¦Yâ 2 0.000000
Hi |àÇù¿-F¦Yâ 3 0.000000
Hi |àÇù¿-F¦Yâ 4 0.000000
Hi |àÇù¿-F¦Yâ 5 0.000000

The contents of the file are:
$cat stuff
0:Hello:12.30
1:Hello:12.30
2:Hello:12.30
3:Hello:12.30
4:Hello:12.30
5:Hello:12.30
6:Hello:12.30
7:Hello:12.30
8:Hello:12.30
9:Hello:12.30
10:Hello:12.30

Why is this happening when I'm reading string and float values. Help me
where I'm wrong
Thanks in advance
 
M

Mahesh

Rajen said:
When I run this program.
#include<stdio.h>

int main()
{
FILE *fp;

int i;
float f;
char str[20];

if( ( fp=fopen("stuff", "w")) == NULL )

Should have been either 'fopen("stuff",,"rw")' or
'fopen("stuff",,"r+")'
{
printf("Sorry! Unable to create/read stuff");
exit(-1);
}

for( i=0; i<=10; i++ )
fprintf(fp, "%d:%s:%.2f\n", i, "Hello", 12.3f);

And here the file pointer has to be brought to the beginning of the
file, or to the position from which you wanted to read the contents of
the file. Something like, 'rewind(fp)' would do that.

for( i=0; i<=5; i++)
{
fscanf( fp, "%d:%s:%.2f\n" , &i, &str, &f);
printf("Hi %s %d %f\n", str, i, f);
}

fclose(fp);
return 0;
}

This is the output:
$./a.out
Hi |àÇù¿-F¦Yâ 0 0.000000
Hi |àÇù¿-F¦Yâ 1 0.000000
Hi |àÇù¿-F¦Yâ 2 0.000000
Hi |àÇù¿-F¦Yâ 3 0.000000
Hi |àÇù¿-F¦Yâ 4 0.000000
Hi |àÇù¿-F¦Yâ 5 0.000000

But I am not aware of what produced the above output.
 
W

Walter Roberson

When I run this program.

Please don't post in MIME encoding. I have to manually decode the
mime to answer your question.
#include<stdio.h>

int main()
{
FILE *fp;

int i;
float f;
char str[20];

if( ( fp=fopen("stuff", "w")) == NULL )
{
printf("Sorry! Unable to create/read stuff");
exit(-1);
}

for( i=0; i<=10; i++ )
fprintf(fp, "%d:%s:%.2f\n", i, "Hello", 12.3f);

for( i=0; i<=5; i++)
{
fscanf( fp, "%d:%s:%.2f\n" , &i, &str, &f);

You have several problems here.

- you are trying to read from the end of a file

- you have not flushed the output buffer

- you are trying to read from a file while it is in write mode; you
need to use rewind() or fseek() to switch to read mode

- you are printing out a ':' after the "Hello" and you are
expecting that when you fscanf() with %s format that that ':' will mark
the end of the "Hello" string. It will not, however: the %s format
reads to the next white-space. If you only want to read as far as
the next : then you need to use %[^:] as your format specifier.

- you have not protected yourself in case the input string read into
str is larger than your buffer (20 including the \0 you want to
terminate the string)

- int main() should be int main(void)
 
R

Rod Pemberton

Walter Roberson said:
Please don't post in MIME encoding. I have to manually decode the
mime to answer your question.

You didn't mention how you wanted him to post (ASCII text) so his next post
might be HTML, UUE, etc...

You didn't check to see if he was posting from Google...(i.e., the format
might not be his choice).

You should be able to adjust _your_ newsreader settings to display the
message _properly_.

If he's using MS Outlook Express, the new message will have a "Format" menu
and "Plain Text" option.


Rod Pemberton
 
K

Kenneth Brody

Walter said:
Please don't post in MIME encoding. I have to manually decode the
mime to answer your question.
[...]

According to my newsreader, his message has no "MIME encoding":

Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

Richard Heathfield

Kenneth Brody said:
Walter said:
Please don't post in MIME encoding. I have to manually decode the
mime to answer your question.
[...]

According to my newsreader, his message has no "MIME encoding":

Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

If you look at the original message source (typically via Message/View
Source or Properties/Original Message or something along those lines,
depending on your newsreader), I think you will see his point.

For example, if( ( fp=3Dfopen("stuff", "w")) =3D=3D NULL )

which is never going to compile. :)
 
W

Walter Roberson

You didn't mention how you wanted him to post (ASCII text) so his next post
might be HTML, UUE, etc...

And then you get into the arguments about how the 7 bit MIME encodings
*are* in ASCII text, and round and round it goes.

I didn't have time for an exposition on encoding formats, and such
would have been only marginally relevant to the newsgroup.

You didn't check to see if he was posting from Google...(i.e., the format
might not be his choice).

The format is a choice even with Google. It just might not be an obvious
choice.

You should be able to adjust _your_ newsreader settings to display the
message _properly_.

Well I can't. The mime decoding program I would need is the legal
property of a bankrupt corporation (#1) which had bought it from another
company (#2) which had bought it from yet another company (#3) which
discontinued officially discontinued it a decade ago after having
bought it from a spin off of another company that wrote it (#4). Several
people tried going through all the legal hoops with the first two
forks (#3 and #2) and were completely unsuccessful: the spin-off (#2)
made a deliberate policy decision to withdraw it from the market and
keep it off, and none of the successor companies was willing to go through
the internal mechanisms to sort things out.

You are assuming that my equipment supports the character set the
posting in question was written in; that's a poor assumption, and
switching to a GUI newsreader to -draw- the characters is not an
acceptable solution.

Alternate encodings are also not in accordance with Usenet standards
so I'll thank you not to go telling me that I need to adjust *my* end.

If he's using MS Outlook Express, the new message will have a "Format" menu
and "Plain Text" option.

I wouldn't know. That's a platform specific piece of information
for a platform that I don't use. As Outlook Express is not the
topic of this newsgroup, it is highly recommended that you do not
post technical information about it here, where Outlook Express experts
are not necessarily available to review your information and correct it
or give additional guideance if necessary.


I'm not sure why you felt the need to pick on me for keeping on
topic and not giving out platform-specific information that
is not the topic of the newsgroup and which I don't know.
 
W

Walter Roberson

Walter Roberson wrote:
According to my newsreader, his message has no "MIME encoding":
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Content-Type and Content-Transfer-Encoding *are* MIME headers.
quoted-printable encoding is defined by RFC 1521,
"MIME (Multipurpose Internet Mail Extensions) Part One"
 
D

Default User

Walter said:
Please don't post in MIME encoding. I have to manually decode the
mime to answer your question.

He's posting from Google, and I don't believe there's any control over
that sort of thing.





Brian
 
D

Default User

Richard said:
Kenneth Brody said:
Walter said:
When I run this program.

Please don't post in MIME encoding. I have to manually decode the
mime to answer your question.
[...]

According to my newsreader, his message has no "MIME encoding":

Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

If you look at the original message source (typically via
Message/View Source or Properties/Original Message or something along
those lines, depending on your newsreader), I think you will see his
point.

He (the OP) is posting from Google. There was some question about the
"quoted-printable" in another newsgroup, which I seemed to recall was
only for replies but perhaps not. At any rate, it's not something that
is easy for the OP to deal with, if possible at all.

Most newsreaders these days handle quoted-printable, but obviously
there are some that don't or can be set to not do so. One of the
problems I have with my newsreader is that it wraps text at the
boundary I specify, even if it's a long URI. The designer of the
program told me just to post in q-p, but I'm an old usenet hand and
reluctant to do so.




Brian (tradeoffs here, tradeoffs there)
 
W

Walter Roberson

Walter Roberson wrote:
He's posting from Google, and I don't believe there's any control over
that sort of thing.

It might have been due to posting characters not representable
in ASCII (which is the character set mandated for NNTP).

I -speculate- that if only the ASCII subset is used, and no
attachments (or multiparts etc) that google does not include
the content-encoding: header.
 
D

Default User

Walter said:
It might have been due to posting characters not representable
in ASCII (which is the character set mandated for NNTP).

I -speculate- that if only the ASCII subset is used, and no
attachments (or multiparts etc) that google does not include
the content-encoding: header.


That could be. That kind of makes sense, as Google isn't really a
newsreader with settings for the most part. An automatic switch to some
sort of encoding when needed is probably the easiest solution.




Brian
 
M

Mark McIntyre

You should be able to adjust _your_ newsreader settings to display the
message _properly_.

why? The OP is the one quoting in a weird fashion, and not all
newsreaders offer the facility to decode weird formats.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
D

Default User

Mark said:
why? The OP is the one quoting in a weird fashion, and not all
newsreaders offer the facility to decode weird formats.

Again, he's not actually doing anything, Google is. There is no
facility for the user to correct the problem, short of not using Google
to post non-ASCII characters.




Brian
 
W

Walter Roberson

Default User said:
Again, he's not actually doing anything, Google is. There is no
facility for the user to correct the problem, short of not using Google
to post non-ASCII characters.

There isn't any other method of posting non-ASCII characters that
is acceptable either. nntp is ASCII only, as are all Big 8 Newsgroups
unless otherwise chartered.
 
D

Default User

Walter said:
There isn't any other method of posting non-ASCII characters that
is acceptable either. nntp is ASCII only, as are all Big 8 Newsgroups
unless otherwise chartered.


Right. There's just no point in hammering on the guy, as he didn't do
anything intentional, probably, to do that.

I've found it helpful for me to identify Google users. To do that, I
added a header line to the ones displayed at the top of messages, so if
I see:

User-Agent: G2/1.0


I can tailor advice/criticism appropriately. This started in the "no
quote" days, but I've found it useful to keep for circumstances like
the one we have here.



Brian
 
K

Kenneth Brody

Richard said:
Kenneth Brody said:
Walter said:
When I run this program.

Please don't post in MIME encoding. I have to manually decode the
mime to answer your question.
[...]

According to my newsreader, his message has no "MIME encoding":

Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

If you look at the original message source (typically via Message/View
Source or Properties/Original Message or something along those lines,
depending on your newsreader), I think you will see his point.

For example, if( ( fp=3Dfopen("stuff", "w")) =3D=3D NULL )

Ahh... I was looking for the "obvious" stuff, like BASE64, or
multipart posting.
which is never going to compile. :)


Why? Your library doesn't include the 3Dfopen() function? :)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
M

Mark McIntyre

Again, he's not actually doing anything, Google is. There is no
facility for the user to correct the problem, short of not using Google
to post non-ASCII characters.

I'm struggling to understand why a space is a non-ascii character.
Don't bother to explain, I actually don't care. If google is so broken
that it posts in fonts that don't contain space characters, then....
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
W

Walter Roberson

Mark McIntyre said:
I'm struggling to understand why a space is a non-ascii character.
Don't bother to explain, I actually don't care. If google is so broken
that it posts in fonts that don't contain space characters, then....

The original posting contained sample output of the program. That
sample output included non-ASCII characters. The quoted-printable
encoding string was =E0=C7=F9=BF-F=A6Y=E2 which would correspond to
"\xE0\xC7\xF9\xBF-F\xA6Y\xE2" or
"\340\307\371\277-F\246Y\342" .

In 8859-1 these characters correspond to (respectively)
lowercase-a with ` accent
capital C with a cidile
lowercase-u with ` accent
upsidedown question mark
broken bar
lowercase-a with circumflex

The posting also contained incidental encodings of all the equal
signs (because they introduce quoted-printable characters), and
as well an encoding of a period that happened to occur at the
beginning of a line.
 
R

Rajen

Walter said:
When I run this program.

Please don't post in MIME encoding. I have to manually decode the
mime to answer your question.
#include<stdio.h>

int main()
{
FILE *fp;

int i;
float f;
char str[20];

if( ( fp=fopen("stuff", "w")) == NULL )
{
printf("Sorry! Unable to create/read stuff");
exit(-1);
}

for( i=0; i<=10; i++ )
fprintf(fp, "%d:%s:%.2f\n", i, "Hello", 12.3f);

for( i=0; i<=5; i++)
{
fscanf( fp, "%d:%s:%.2f\n" , &i, &str, &f);

You have several problems here.

- you are trying to read from the end of a file
-------This was correct. I closed the file and opened it in read mode.
It's working.
- you have not flushed the output buffer
--------I'm not sure about this.
- you are trying to read from a file while it is in write mode; you
need to use rewind() or fseek() to switch to read mode

- you are printing out a ':' after the "Hello" and you are
expecting that when you fscanf() with %s format that that ':' will mark
the end of the "Hello" string. It will not, however: the %s format
reads to the next white-space. If you only want to read as far as
the next : then you need to use %[^:] as your format specifier.

----This is actual solution. I tried this and now it is reading the
string values upto ":"
Thank you for this Walter.
I have a problem reading the float values.
I modiifed my above code as:
.....
fclose(fp);
fp=NULL;
fp = fopen("stuff", "r") ;
for( i=0; i<=5; i++)
{
fscanf( fp, "%d:%[^:]s:%.2[^:]f\n" , &i, &str, &f);
printf("Hi %s %d %.2f Size:%d\n", str, i, f, strlen(str));
}
......

Can i Use [^:] to limit float values as above.
Now the output is:
Hi Hello 0 0.00 Size:5
Hi Hello 1 0.00 Size:5
Hi Hello 2 0.00 Size:5
Hi Hello 3 0.00 Size:5
Hi Hello 4 0.00 Size:5
Hi Hello 5 0.00 Size:5

Please tell me how to solve this new problem. Thanks in advance Walter.
-----------------------------------------------------------------------------------------------------
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top