slurping in binary data

G

George

Lots of people work with something called a "BOOK" or "MANUAL".
You should try it. You might get hooked. :)

Gosh, fellas, I'm in the position of being an antagonist w.r.t. the man
pages. Since no hispanic on the earth can spell 'Merrill', I tell them I'm
manual.
--
George

The terrorists and their supporters declared war on the United States - and
war is what they got.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
C

CBFalconer

Richard said:
CBFalconer said:

Because it's useful when you want to avoid being considered illiterate.


No, it doesn't. A good snipper will leave *only* the material to which
he is replying directly,

In which case the reply answers (and refers to) the message and the
material quoted. This really isn't very hard. Try to think about
the purpose of Usenet quotes.
 
G

George

I am and I can. Any other ethnic groups you'd like to disparage?

How do you believe other ethniticies fare with 'Merrill'?

I used to say "like Merrill Lynch, minus the money," but since I've got a
hundred bucks, my wealth exceeds theirs minus Bush welfare.

My bases are covered with Obamanos. The mexican bar where i threw somebody
across the parking lot tonight employs the beautiful girl who drovemehome.
--
George

The thing that's wrong with the French is that they don't have a word for
entrepreneur.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
N

Nick Keighley

I think in context "platform" means the OS, compiler and C library.
Note gcc is a compiler (a collection of compilers atcually) and often
ships without a library. You may well be using Microsoft's C library.

MS documentation is usually pretty good. I failed to find snprintf()
in their documentaion though. I *did* find _snprintf(). Google
for snprintf() and you get loads of hits. What don't you
understand?

please use standard english. What does "I thought I was getting
flashbacks"
mean? Subscribe to what?

Note I am a native english speaker (though probably from a different
sub-culture) and I'm finding it increasingly difficult to understand
you.


ok, whatever helps you
No, I wasn't asking about this function which I don't know, but I would
believe that someone had a relevant reason to bring it up.

why not look it up then you'd know what it did? Its a slightly
safer version of sprintf().


I don't recall anyone saying this. Though it is often very
good advice.
I arrived here in albuquerque and finally encountered the manual.  Ron is
the R and 1966 is the year.

Sorry you fell off the edge of my language skills again.
What has albuquerque (a twon in the United States of America?)
have to do with reading a computer manual? Who's ron?
The f part

the "f" part of what?
is something I'vebeen thinking about.  In chuck's ggets, he has
a file * f.  Since we all know that gets() takes from stdin, I was
thinkingabouthow to achieve this minimalism.
??

Is it:

#include<stdio.h>

int main(){
extern FILE * stdin;

you don't need to declare stdin, stdio.h pulls in the definition.
call ggets( char*, stdin)

is that C?
But f is for file pointer.

sdin is file pointer (actually FILE*)...
 int main yields 'tm'

no it doesn't
Puttong them together in order of appearance could produce 'rftm'.

I've no idea what you are on about
If one believes that another is suficently wrong that he needs to say, "
consult a non-existent user's manual,"

or "consult an existent manual"
maybe its usual non-existence would
be by acknowledged that another has something with F and T flipped.  Hence
I endorse:

RFTM
incomprehensible

unfamilar term used

I *think* there is another standard somewhere that has these
sort of standard terms defined. I find the C Standard an easy
read.


<snip>
 
J

James Kuyper

George said:
Huh? Are you sure?

Of course. Why aren't you sure?
q2) What can a variadic function do that a garden-variety function can't?
take a [variable] number of arguments

But what good is it? Have you ever used a variadic function and said,
"whew it was a good thing I had that variadic function because ______."

Yes, I use members of the printf() and scanf() families of functions all
the time, and am profoundly grateful that the variadic ones are
variadic. So grateful, in fact, that I've written an error logging
variadic function which is basically a wrapper for vfprintf(stderr, ...).
 
N

Nick Keighley

Lots of people work with something called a "BOOK" or "MANUAL".
You should try it. You might get hooked. :)

I've never managed to get the copy-paste function to work properly
with
one of those.
 
J

James Kuyper

CBFalconer said:
Why bother? ...

To avoid annoying people, as you routinely do, by providing the same
answer that someone else already has (often, several someones).
Sometimes, you're not an annoyance, just a laughingstock, like the
person who shows up days late for a battle, and yells out "Beware
Mordred, I have returned, and I will have my vengeance!" to an empty
battlefield .

Also, it helps to check for the possibility that someone else has
noticed a point that you've missed. At the very least, this helps avoid
the embarrassment of revealing that you did miss the point (as you
frequently do). Also, when you become aware of that point, you might
have a unique perspective on that issue that you can add to your response.

....
visible to other readers. You seem to feel that answering a
message requires first reviewing something like 100 (maybe more,
maybe less) other messages hooked to the same thread.

Any decent newsreader will provide methods that make it easy to identify
which messages are actually responses to a given message, you only need
to review those messages in order to find out whether you're repeating a
response that someone else has already made.

However, that's not necessary. When I see a message that I want to
respond to, I post a response, but don't hit the send button. I then
keep reading further messages from the newsgroup. If I see a message
which gives me a reason to modify or cancel my original response, I do
so. Only when I've read all the new messages do I hit the Send buttons
on all of the pending responses I've got piled up.
... And, if you do mention such things as my habits in order to
criticize, it would appear to be reasonable to quote the allegedly
failed reply, etc. ...

People frequently criticize you when you post late duplicate responses,
I didn't think it necessary to provide examples.
... Notice that your message is rather meaningless,
since it appears to revolve around the entities q2 and q3, which
are undefined, are mentioned as being questioned, without stating
the questions, etc.

They were not defined in the message I was responding to. While I would
certainly have retained the actual questions if they had been quoted, I
do not routinely back-fill quotations that have been snipped by others.

I knew what questions he was referring to. This is partly because I have
a decent memory, partly because I use a decent newsreader which actually
displays previous messages in the thread for easy review, but even if
neither of those had been true, I could easily have retrieved the
information from either google or from my own folder filled with copies
of messages I've posted.
 
G

George

I've also found the search facilities rather poor, too.

Sjouke is of the opinion that halting englisch is better dutch.

I find myself reluctant to acknowledge Arnout Alex Krijn van den Heide,
except that I don't.
--
George

I believe that God has planted in every heart the desire to live in
freedom.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
G

George

Of course. Why aren't you sure?

One would have thought the opposite because of the presence of the v in the
name. I wonder why they came up with the *scanfs that are not variadic.
q2) What can a variadic function do that a garden-variety function can't?
take a [variable] number of arguments

But what good is it? Have you ever used a variadic function and said,
"whew it was a good thing I had that variadic function because ______."

Yes, I use members of the printf() and scanf() families of functions all
the time, and am profoundly grateful that the variadic ones are
variadic. So grateful, in fact, that I've written an error logging
variadic function which is basically a wrapper for vfprintf(stderr, ...).

I'm clearly struggling to understand as this last version shows:

#include <stdio.h>
#include <stdlib.h>

#define PATH "george.txt"
#define NUMBER 100
#define MAXFMTLEN 2000

int main(void)
{
FILE *fp;
char pattern[MAXFMTLEN];
char lnumber[NUMBER];
char line[MAXFMTLEN];

if ((fp = fopen(PATH, "r")) == NULL ) {
fprintf(stderr, "can't open file\n");
exit(1);
}

sprintf(pattern, "%%%ds", NUMBER-1);

// while ((fgets(line, MAXFMTLEN, fp)) != NULL)

while(fgets(line, MAXFMTLEN, fp) == line){
sscanf(line, pattern , lnumber);

printf("%s\n", lnumber);
}

if(ferror(fp))
perror(PATH);

fclose(fp);
return 0;
}

// gcc -o x.exe chad4.c

Since the data set had meaningful line numbers, I attempted to conflate BIN
with NUMBER. I thought, heck, sprintf is variadic, so I can just remove an
argument, and the function won't be the wiser.

This compiles but doesn't behave; current output is:


C:\MinGW\source> gcc -o x.exe chad4.c
chad4.c:37:24: warning: no newline at end of file

C:\MinGW\source>x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

C:\MinGW\source>
C:\MinGW\source>g95 kuyper1.f03 -o x.exe


I'm still scratching for clues why fortran and care different here.

integer :: this
character (len=10) :: anything
this = 42
anything = "the other"

print *, this, "that, ", anything
endprogram

! g95 kuyper1.f03 -o x.exe

C:\MinGW\source>x
42 that, the other

C:\MinGW\source>

Without wanting to sound flamy, I like the fortran version just fine.
--
George

The tyrant has fallen, and Iraq is free.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
B

Barry Schwarz

One would have thought the opposite because of the presence of the v in the
name. I wonder why they came up with the *scanfs that are not variadic.

So that people could pass an argument of type va_list.
q2) What can a variadic function do that a garden-variety function can't?
take a [variable] number of arguments

But what good is it? Have you ever used a variadic function and said,
"whew it was a good thing I had that variadic function because ______."

Yes, I use members of the printf() and scanf() families of functions all
the time, and am profoundly grateful that the variadic ones are
variadic. So grateful, in fact, that I've written an error logging
variadic function which is basically a wrapper for vfprintf(stderr, ...).

I'm clearly struggling to understand as this last version shows:

#include <stdio.h>
#include <stdlib.h>

#define PATH "george.txt"
#define NUMBER 100
#define MAXFMTLEN 2000

int main(void)
{
FILE *fp;
char pattern[MAXFMTLEN];
char lnumber[NUMBER];
char line[MAXFMTLEN];

if ((fp = fopen(PATH, "r")) == NULL ) {
fprintf(stderr, "can't open file\n");
exit(1);
}

sprintf(pattern, "%%%ds", NUMBER-1);

So pattern contains the string "%99s".
// while ((fgets(line, MAXFMTLEN, fp)) != NULL)

while(fgets(line, MAXFMTLEN, fp) == line){

Do you think there is any functional difference between the two
version of your while? I will say that on my system testing against 0
is easier than other comparisons so the commented one may be faster.
sscanf(line, pattern , lnumber);

The 99 field width specification is the maximum number of characters
that will be process by the %s. However, %s will stop at the first
white space.
printf("%s\n", lnumber);
}

if(ferror(fp))
perror(PATH);

fclose(fp);
return 0;
}

// gcc -o x.exe chad4.c

Since the data set had meaningful line numbers, I attempted to conflate BIN
with NUMBER. I thought, heck, sprintf is variadic, so I can just remove an

NUMBER is a macro. What is BIN.
argument, and the function won't be the wiser.

And it did exactly what you asked, even if it is not what you wanted.
It processed all the characters up to the next white space or a max of
99, whichever came first. What you may have wanted was a format of
the form "%*s %99s" to skip over the first string.
This compiles but doesn't behave; current output is:


C:\MinGW\source> gcc -o x.exe chad4.c
chad4.c:37:24: warning: no newline at end of file

C:\MinGW\source>x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

C:\MinGW\source>
C:\MinGW\source>g95 kuyper1.f03 -o x.exe


I'm still scratching for clues why fortran and care different here.

integer :: this
character (len=10) :: anything
this = 42
anything = "the other"

print *, this, "that, ", anything
endprogram

! g95 kuyper1.f03 -o x.exe

C:\MinGW\source>x
42 that, the other

Why is there a space between the 42 and "that" but not between
"that, " and "the other"?
C:\MinGW\source>

Without wanting to sound flamy, I like the fortran version just fine.

Does this fortran code perform any input? How is it related to your C
code?

#include <stdio.h>
int main(void){
int this;
char len[10], *anything;
this = 42;
anything = "the other";
printf("%d %s %s\n", this, "that, ", anything);
return 0;
}

Other than using printf, how does this relate to your question about
variadic functions?
 
N

Nick Keighley

Sjouke is of the opinion that halting englisch is better dutch.

I find myself reluctant to acknowledge Arnout Alex Krijn van den Heide,
except that I don't.

I'm curious. Does anyone understand what this guy is on about?
 
J

James Kuyper

Barry said:
....

Do you think there is any functional difference between the two
version of your while? I will say that on my system testing against 0
is easier than other comparisons so the commented one may be faster.

He did that on my recommendation. There is a functional difference: it
treats an invalid return value from fgets() the same as an error, while
the original version treats an invalid return value from fgets() the
same as a successful call. The chance that fgets() will malfunction is
negligible, but so is the timing difference between !=NULL and ==line. I
prefer to trade off robust behavior when used with a defective version
of the standard library over the minor difference in performance. YMMV.
NUMBER is a macro. What is BIN.

In a previous version of the code, BIN was also a macro.
C:\MinGW\source>
C:\MinGW\source>g95 kuyper1.f03 -o x.exe


I'm still scratching for clues why fortran and care different here. ....
character (len=10) :: anything
....
char len[10], *anything;

This is a mis-translation. "character (len=10)" in Fortran is equivalent
to "char [10]" in C.
 
J

James Kuyper

George said:
One would have thought the opposite because of the presence of the v in the
name. ...

Well, your first thought, if you weren't sure, should have been to check
the documentation before asking whether I was sure.

The variadic ones came first, the non-variadic ones were labeled with
'v' to indicate that they take a va_list argument instead of being
variadic. If they'd been designed at the same time, it would probably
have been done the other way around.
... I wonder why they came up with the *scanfs that are not variadic.

I order to figure out why the va_list versions exist, just think about
what's required to do this:

Try to imagine how you could possibly write such a wrapper that used
fprintf() rather than vfprintf().

....
I'm clearly struggling to understand as this last version shows:

#include <stdio.h>
#include <stdlib.h>

#define PATH "george.txt"
#define NUMBER 100
#define MAXFMTLEN 2000

int main(void)
{
FILE *fp;
char pattern[MAXFMTLEN];
char lnumber[NUMBER];
char line[MAXFMTLEN];

if ((fp = fopen(PATH, "r")) == NULL ) {
fprintf(stderr, "can't open file\n");
exit(1);
}

sprintf(pattern, "%%%ds", NUMBER-1);

// while ((fgets(line, MAXFMTLEN, fp)) != NULL)

while(fgets(line, MAXFMTLEN, fp) == line){
sscanf(line, pattern , lnumber);

printf("%s\n", lnumber);
}

if(ferror(fp))
perror(PATH);

fclose(fp);
return 0;
}

// gcc -o x.exe chad4.c

Since the data set had meaningful line numbers, I attempted to conflate BIN
with NUMBER. I thought, heck, sprintf is variadic, so I can just remove an
argument, and the function won't be the wiser.

It is variadic, you can pass it fewer arguments and it will still work
so long as the format string is adjusted accordingly, which you have
done. As a result, "pattern" will now contain a string with only one
format specifier, not two, but you have modified the sscanf() call which
uses "pattern" to only scan in the line number. Everything looks fine to me.
This compiles but doesn't behave; current output is:


C:\MinGW\source> gcc -o x.exe chad4.c
chad4.c:37:24: warning: no newline at end of file

Add a newline at the end of chad4.c. It's just one extra character, it
makes these warnings go away, and on some compilers it's absolutely
necessary in order for chad4.c to be compiled correctly.
C:\MinGW\source>x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

I'm not sure I understand why you consider that misbehavior. You told it
to scan and print only the line numbers, and that's exactly what it does.
C:\MinGW\source>
C:\MinGW\source>g95 kuyper1.f03 -o x.exe


I'm still scratching for clues why fortran and care different here.

integer :: this
character (len=10) :: anything
this = 42
anything = "the other"

print *, this, "that, ", anything
endprogram

! g95 kuyper1.f03 -o x.exe

C:\MinGW\source>x
42 that, the other

C:\MinGW\source>

Without wanting to sound flamy, I like the fortran version just fine.

You've provided C code and fortran code that perform very different
tasks. Why are you comparing them? The version of fortran you're using
is quite different from the last version that I ever used, but I believe
that equivalent C code would look something like this:

#include <stdio.h>

int main(void)
{
int this = 42;
char anything[] = "the other";
printf("%d that, %s\n", this, anything);
return 0;
}
 
J

James Kuyper

George said:
One would have thought the opposite because of the presence of the v in the
name. ...

Well, your first thought, if you weren't sure, should have been to check
the documentation before asking whether I was sure.

The variadic ones came first, the non-variadic ones were labeled with
'v' to indicate that they take a va_list argument instead of being
variadic. If they'd been designed at the same time, it would probably
have been done the other way around.
... I wonder why they came up with the *scanfs that are not variadic.

I order to figure out why the va_list versions exist, just think about
what's required to do this:

Try to imagine how you could possibly write such a wrapper that used
fprintf() rather than vfprintf().

....
I'm clearly struggling to understand as this last version shows:

#include <stdio.h>
#include <stdlib.h>

#define PATH "george.txt"
#define NUMBER 100
#define MAXFMTLEN 2000

int main(void)
{
FILE *fp;
char pattern[MAXFMTLEN];
char lnumber[NUMBER];
char line[MAXFMTLEN];

if ((fp = fopen(PATH, "r")) == NULL ) {
fprintf(stderr, "can't open file\n");
exit(1);
}

sprintf(pattern, "%%%ds", NUMBER-1);

// while ((fgets(line, MAXFMTLEN, fp)) != NULL)

while(fgets(line, MAXFMTLEN, fp) == line){
sscanf(line, pattern , lnumber);

printf("%s\n", lnumber);
}

if(ferror(fp))
perror(PATH);

fclose(fp);
return 0;
}

// gcc -o x.exe chad4.c

Since the data set had meaningful line numbers, I attempted to conflate BIN
with NUMBER. I thought, heck, sprintf is variadic, so I can just remove an
argument, and the function won't be the wiser.

It is variadic, you can pass it fewer arguments and it will still work
so long as the format string is adjusted accordingly, which you have
done. As a result, "pattern" will now contain a string with only one
format specifier, not two, but you have modified the sscanf() call which
uses "pattern" to only scan in the line number. Everything looks fine to me.
This compiles but doesn't behave; current output is:


C:\MinGW\source> gcc -o x.exe chad4.c
chad4.c:37:24: warning: no newline at end of file

Add a newline at the end of chad4.c. It's just one extra character, it
makes these warnings go away, and on some compilers it's absolutely
necessary in order for chad4.c to be compiled correctly.
C:\MinGW\source>x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

I'm not sure I understand why you consider that misbehavior. You told it
to scan and print only the line numbers, and that's exactly what it does.
C:\MinGW\source>
C:\MinGW\source>g95 kuyper1.f03 -o x.exe


I'm still scratching for clues why fortran and care different here.

integer :: this
character (len=10) :: anything
this = 42
anything = "the other"

print *, this, "that, ", anything
endprogram

! g95 kuyper1.f03 -o x.exe

C:\MinGW\source>x
42 that, the other

C:\MinGW\source>

Without wanting to sound flamy, I like the fortran version just fine.

You've provided C code and fortran code that perform very different
tasks. Why are you comparing them? The version of fortran you're using
is quite different from the last version that I ever used, but I believe
that equivalent C code would look something like this:

#include <stdio.h>

int main(void)
{
int this = 42;
char anything[] = "the other";
printf("%d that, %s\n", this, anything);
return 0;
}
 
B

Barry Schwarz

He did that on my recommendation. There is a functional difference: it
treats an invalid return value from fgets() the same as an error, while
the original version treats an invalid return value from fgets() the
same as a successful call. The chance that fgets() will malfunction is
negligible, but so is the timing difference between !=NULL and ==line. I
prefer to trade off robust behavior when used with a defective version
of the standard library over the minor difference in performance. YMMV.

I'm confused. There are only two possible return values from fgets:
NULL or the first argument (line in this case). How is
while (fgets(...) != NULL)
any different from
while (fgets(...) == line)
in the functional sense? If fgets does not return NULL, it must
return line.

How can a non-NULL return be an "invalid return" and how can it
signify anything but success?
 

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,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top