#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
if (argc > 10) {
printf(" too many characters in name\n try 10 or less\n");
exit(1);
}
char p[20];
FILE *fp;
size_t t;
if ((fopen("argv[1]", "r")) == EOF) {
fprintf(stderr, "fopen error\n");
exit(1);
}
t = fread(p, sizeof(int), 20, fp);
if (t = ferror(fp)) {
fprintf(stderr, " file error indicated\n");
exit(1);
} else if (t = feof(fp)) {
fprintf(stderr, " EOF indicated\n");
exit(1);
}
In the code above the 3rd parameter of malloc() is taking 20 character
members. That's not enough. But I don't want buffer overflow either. I know
this is kind of reinventing the wheel so far in what is above but there is a
reason for this. I am trying to write a text editor that takes code that
only ends with "+" instead of C's normal ";". Can malloc be put where the 20
is and just take a malloc? malloc returns void * and this 3rd parameter a
size_t so maybe a struct or union would be easier. Such as
struct values{
size_t t;
void * val;
};
Would that eliminate the need for casting?
Bill
[snip]
> In the code above the 3rd parameter of malloc() is taking 20
> character members. That's not enough. But I don't want buffer
> overflow either.
[...]
Sorry that is not 3rd parameter of malloc but of fread().
Bill
2/10.
You're loosing your touch.
--
Ian Collins
fread(p,sizeof(int),20,fp);
Ok nothing will go above 20 size_t's but if only 15 size_t's are filled I
want to return 5 to memory. Is there a way to do that?
Bill
Sorry.
realloc
> realloc
Then I'm not going to want to use fread's standard parameters am I? I am
going to have to write a function that uses realloc and fread or fgetc for
char * types anyway.
Bill
"luser- -droog" <mij...@yahoo.com> wrote in message
news:baab1bba-f3f2-40da...@c41g2000yqm.googlegroups.com...
Will a conjunction do?
--
Bartc
For the umpteenth time.
I suggest you try something simpler that you might actually get to work.
Write a program that tries to read 20 char from a file that may or may not
exist. The file's name is specified by a command line argument.
You can create a file to test your program by using a text editor.
One of your more pedestrian trolling efforts Bill!
Original post:
Title completely unrelated to the code
Bad grammar
Comparing the number of command line arguments to the length
of a value
Comparing a pointer to a negative int
Non-portable return value from main
Trying to read 20 int into an array of 20 char
Assigning a potentially negative number to a size_t
Using = instead of == in an if
Asking a question about a function call that does not appear
in the post
Discussing the third parameter of a function that has only one
Discussing non-existent casts
Follow up:
Discussing input operations on size-t objects that do not
exist.
Here:
Finally reaching the inevitable conclusion of all your
threads, random guessing on how to solve an unspecified problem.
I think the 2 out of 10 rating given else thread was too generous. I
suggest you change your goal from error density to subtlety.
--
Remove del for email
> I have this unfinished untested code that I would like to enquire to
> someone that has used malloc() before if I can use it here.
>
> [snip]
You might want to try posting this again 11 months from now.
> For the umpteenth time.
>
> I suggest you try something simpler that you might actually get to
> work. Write a program that tries to read 20 char from a file that may
> or may not exist. The file's name is specified by a command line
> argument.
> You can create a file to test your program by using a text editor.
Looking over that code I have noticed several bugs. the argv[1]
shouldn't be in quotes. The tests for t isn't an assignment like written but
should be t== not t=. If I can't write something like this I'm a very sad
story. I've had success with fgetc more anyway.
Bill
I think so. (Linking up phrases and something and something and
something...)
I gave up trying to spot all the errors so I tried to find the first
one.
--
lothario draganeti
> fread(p,sizeof(int),20,fp);
Allocate a suitably large buffer with malloc().
Call fread() to put stuff in the buffer.
If the buffer ends up being too large, resize it with realloc().
I noticed a problem with your original code:
> char p[20];
> t = fread(p, sizeof(int), 20, fp);
p is declared as an array of 20 characters, however you're trying to stuff
20 ints into it, which is too much. If you know you will put ints into p,
declare it that way.
--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
> One of your more pedestrian trolling efforts Bill!
>
> Original post:
> Title completely unrelated to the code
> Bad grammar
> Comparing the number of command line arguments to the length
> of a value
> Comparing a pointer to a negative int
> Non-portable return value from main
> Trying to read 20 int into an array of 20 char
> Assigning a potentially negative number to a size_t
> Using = instead of == in an if
> Asking a question about a function call that does not appear
> in the post
> Discussing the third parameter of a function that has only one
> Discussing non-existent casts
>
> Follow up:
> Discussing input operations on size-t objects that do not
> exist.
>
> Here:
> Finally reaching the inevitable conclusion of all your
> threads, random guessing on how to solve an unspecified problem.
>
> I think the 2 out of 10 rating given else thread was too generous. I
> suggest you change your goal from error density to subtlety.
I disagree with some of your views of the OP. malloc does match with the
intention of the post though not in the code because I didn't know how to
use it. The whole point of the post. The grammer is not perfect granted. It
seems that "Mickey Mouse" was the only one so far who has understood my
inquiry and post.
Bill
Thank you for your response. You have answered my question and seem to
be the only one understanding my inquiry.
Bill
You are welcome.
You might be better posting questions in 'comp.lang.c.moderated', it
is a mostly quiet group but one where questions get answered.
"Bill" first asked about malloc in 2003 (as seen from google).
Go figure.
--
Ian Collins
But it's also a group where the moderator might not approve posts which
were blatantly trolling. You never know.
I honestly don't know what I'd do if one of Bill's surrealist pieces showed
up. I have spent a fair amount of my recreational time over the last twenty
years marvelling at the many and diverse ways in which humans can be
mind-numbingly stupid, and I still can't figure out for sure whether his
posts are *possibly* the result of sincere stupidity. I tend to suspect
that he's an extremely talented and persistent troll. If not, I imagine
someone could get a doctorate by mapping the boundaries of his cognitive
dysfunction.
-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
Hookin' up clauses and phrases and words ...
(IIRC -- it's been 30 years or so -- but probably YTIYF))
=)
> You might want to try posting this again 11 months from now.
I don't believe I've ever posted any question like this before
concerning memory reallocation.
Bill
> One of your more pedestrian trolling efforts Bill!
>
> Original post:
> Title completely unrelated to the code
> Bad grammar
> Comparing the number of command line arguments to the length
> of a value
> Comparing a pointer to a negative int
> Non-portable return value from main
> Trying to read 20 int into an array of 20 char
> Assigning a potentially negative number to a size_t
> Using = instead of == in an if
> Asking a question about a function call that does not appear
> in the post
> Discussing the third parameter of a function that has only one
> Discussing non-existent casts
>
> Follow up:
> Discussing input operations on size-t objects that do not
> exist.
>
> Here:
> Finally reaching the inevitable conclusion of all your
> threads, random guessing on how to solve an unspecified problem.
>
> I think the 2 out of 10 rating given else thread was too generous. I
> suggest you change your goal from error density to subtlety.
Note: See other posts concerning this thread.
---
Sorry that is not 3rd parameter of malloc but of fread().
Bill
---
Why?
The file's name is specified by a command line
> argument.
So?
>
> You can create a file to test your program by using a text editor.
What's this post all about? What's it's title? I could've picked picked
a better title true. Which do you think would've made a better title?
1) stream question or
2) memory allocation question ?
Bill
Your post made clear that you were trying to skip the fundamentals and go
right to the meat of the thing; for this week your target was "malloc". My
suggestion was for a more ordered approach in which you might actually learn
something and get a program you had written to work. But clearly you don't
like that approach.
Your approach reminds me of someone in a foreign country opening an English
dictionary at random, and asking questions about a few words every week.
What is a pharmacist? What is a dormer? What is rubidium? What is a
carnival?
Since you are unlikely to live for much more than 100 years, I think that
method is doomed to failure.
> "Bill" first asked about malloc in 2003 (as seen from google).
>
> Go figure.
malloc is the only memory allocation I know about other than arrays.
realloc must come into play somewhere.
Bill
There are malloc(), calloc() and realloc(). In addition, on a Unix
system there are also sbrk() and mmap(), but if you want to write
portable code you should stick to malloc() and friends.
realloc() could, for example, be used to implement a dynamic "array" that
grows in size as elements are added and shrinks as elements are removed.
--
The perfected state of a spam server is a smoking crater.
- The Crater Corollary to Rule #4
http://groups.google.com/group/comp.lang.c/browse_thread/thread/8505637713d6e1eb/885a55feeb4d25d0 Nope after viewing the link above, I think I can still say I don'tbelieve I've ever posted anything about memory allocation. Encrypting andrandom strings maybe. I've never used malloc before in code but it is verypopular.B
> "Bill" first asked about malloc in 2003 (as seen from google).
>
> Go figure.
Fill me in.
Bill
> Your post made clear that you were trying to skip the fundamentals
> and go right to the meat of the thing; for this week your target was
> "malloc". My suggestion was for a more ordered approach in which you
> might actually learn something and get a program you had written to
> work. But clearly you don't like that approach.
[...]
How much more to "the meat of the thing" is malloc? I'm not sure that
I'm understanding you. What do you mean by a "more ordered approach" ? The
program that was written did not included malloc. And I noticed later it was
full of bugs as posted. I must admit I need to start testing code and
examining it more closely before posting. fread as was in the program is
designed to take a set number of data.
Hence my point. I am in no position to write code that expands and
ontracts memory storage on the fly. That was what my inquiry was concerning.
Using the FILE* and streams as an example.
Bill
[snip]
> realloc() could, for example, be used to implement a dynamic "array"
> that grows in size as elements are added and shrinks as elements are
> removed.
Exactly what I was looking for. Using the example of streams and the
FILE* struct. I don't think fread should even be used for this come to think
of it. fgetc might be a better way. But I have no idea where to even begin
with designing a function to set aside storage and allow it to grow and
shrink dynamically.
Bill
Memory that expands or contracts is more complicated than a fixed target of
known maximum size.Your post tells me you can't even handle that. Thus the
simpler assignment I suggested. Your knowledge of argc, for example is
woefully wrong.. If you understand the basics you should be able to do what
I wrote in five minutes or less.
Then you didn't read the full thread.
--
Ian Collins
Your quoting is broken again.
Another one then: in Message-ID: <AUnuj.5177$kD3.212@trnddc08> (18 Feb
2008) you asked:
| So I'll need a buffer for fread and write.
| char buff(10);
| And a pointer for realloc
| char *b=&buff;
| Something like that right?
You seem to keep asking the same questions. The message I've just
quoted is from a thread about almost exactly the same issues as the
current thread: expanding a buffer to take more input.
BTW, I don't mind. The same questions keep getting asked anyway and it
really doesn't matter if it is many people asking them or just one.
--
Ben.
Here is a very quick, very dirty (no error handling or boundary
checking) implementation of a variable-length first-in, first-out
character buffer. It's only as large as it needs to be,
and the maximum size is only limited by available memory.
(Yes, I know there are far better ways to do this. It's just an example
of using realloc(), nothing more.)
#include <stdlib.h>
#include <string.h>
static char *fifo = NULL;
static size_t fifo_count = 0;
size_t fifo_write(const char *data, const size_t len)
{
fifo = realloc(fifo, fifo_count + len);
memcpy(fifo + fifo_count, data, len);
fifo_count += len;
return len;
}
size_t fifo_read(char *data, const size_t len)
{
memcpy(data, fifo, len);
memmove(fifo, fifo + len, fifo_count - len);
fifo_count -= len;
fifo = realloc(fifo, fifo_count);
return len;
> Memory that expands or contracts is more complicated than a fixed
> target of known maximum size.Your post tells me you can't even handle
> that. Thus the simpler assignment I suggested. Your knowledge of
> argc, for example is woefully wrong.. If you understand the basics
> you should be able to do what I wrote in five minutes or less.
You're obviously more experienced than I. And have a better
understanding of C and programming in general.
Bill
> You might be better posting questions in 'comp.lang.c.moderated', it
> is a mostly quiet group but one where questions get answered.
Bill has supposedly been learning C for the better part of a decade. Over
the course of numerous threads during that time, his knowledge of the
language and specific parts of the language have waxed and waned. He will
use some function correctly in one program, then a month later use it with
the wrong inputs, or even claim to be unaware of it.
Many of us have come to the conclusion that it's a long-con troll. If it's
not, then his capacity for learning the language simply doesn't exist. He
demonstrates less capability than a first-sememster student at mid-terms.
Either way, a waste of time to respond.
Brian
He's said in the past that it's due to side effects of some
medication he's on. Either it's true, and he's (sadly) wasting his
time, or it's not, and he's (annoyingly) wasting ours. (I'm aware
that some people here dismiss the former possibility; I don't care
to debate the point.)
If Bill really wants to learn programming, I suggest that C may be one
of the worst possible languages for him to use. Of the languages I'm
familiar with, Python might be a much better choice.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Replying to his quests here is kind if not helpful to him. Your reply to
Bill might be helpful to other myriad readers here. Don't stop.
--
Joe Wright
"If you rob Peter to pay Paul you can depend on the support of Paul."
[...]
> If Bill really wants to learn programming, I suggest that C may be one
> of the worst possible languages for him to use. Of the languages I'm
> familiar with, Python might be a much better choice.
I have looked at python and it totally blows my mind. Just looking at
the code mind you and not trying to learn. I have read so many times the C
is easy to learn. I've looked at C++ and it is alittle more understnadable
to me. clc is the only place I know to go if I have any C questions. I know
nothing about algorithms and computer science. I have no college training.
I just want to learn a language. I claim nothing more. I know how to use
functions half wy decent in C but coding for results is beyond me. Right now
anyway. I hope that changes some day.
Bill
While the syntax of C is fairly simple, the language allows way too much
freedom that can result in bad code or subtle bugs, so IMHO it is not
the best language for someone completely new to programming. For
learning purposes, a strongly typed and rigidly structured language
might be better.
I started with Pascal myself back in high school (okay, I did C64 BASIC
before that), but nowadays Java seems to be the educational language of
choice. If you've seen C++, Java will look very familiar.
If you still want to learn C, well the FAQ for this newsgroup holds a
lot of answers to common questions, take a look there. There are also
free online courses available, just Google a bit for them.
Of course, like with so many computer things, the best way to learn it
is to play with it. Start with small things, like for example a program
that works like the Unix command "cat", and work your way up from there.