file bug

W

William Pursell

You can easily get the size of a file:
FILE *in;
char *filedata;
long size_in_bytes;
in=fopen("file.txt","rb");
fseek(in,0,SEEK_END);
size_in_bytes = ftell(in);
fseek(in,0,SEEK_SET);
filedata = malloc(size_in_bytes);
fread(in,sizeof(char),size_in_bytes);

But you need to be careful:

$ cat a.c
#include <stdlib.h>
#include <stdio.h>

int
main( int argc, char **argv )
{
FILE *in;
char *filedata;
long size_in_bytes;

in=fopen("file.txt","rb");
fseek(in,0,SEEK_END);
size_in_bytes = ftell(in);
printf( "%ld\n", size_in_bytes );
}

$ ./a.out
-1
$ cat file.txt
foo

Not everything is a regular file.
 
R

Randy Howard

Ok thanks I need to see as much simple code as I can. All that I seem to
get hold of is written in shorthand and such.

Why not just buy a copy of K&R2, or check it out from a library?
 
B

Bill Cunningham

Randy Howard said:
Why not just buy a copy of K&R2, or check it out from a library?
Randy I have kandr2. And it's a good book. The reading is kinda tough
but some of the examples are clear. As far as tutorial which is what I need
and use most this is what I usehttp://www.eskimo.com/~scs/cclass/

They will tell you this is one of the best tutorials there is and it is as
far as I am concerned. Other than asking a question here if I am reallt
stumped that's where I go for a tutorial. I hope it never closes down. I
also just bought a little C information book I've been reading called "C
pocket reference" by O'reilly. This covers some of those strange headers I
come across I understand are c99 now like signal.h.

Bill
 
J

Joachim Schmitz

William said:
But you need to be careful:

$ cat a.c
#include <stdlib.h>
#include <stdio.h>

int
main( int argc, char **argv )
{
FILE *in;
char *filedata;
long size_in_bytes;

in=fopen("file.txt","rb");
fseek(in,0,SEEK_END);
size_in_bytes = ftell(in);
printf( "%ld\n", size_in_bytes );
}

$ ./a.out
-1
And -1 being returned by ftell is an error, which should be checked for.
§7.19.9.4.3: On failure, the ftell function returns ?1L and stores an
implementation-defined positive value in errno.

$ cat file.txt
foo

Not everything is a regular file.

Bye, Jojo
 
N

Nick Keighley

please use the normal quoting conventions it makes things easier to
read

in pcode:

LOOP UNTIL NO MORE DATA
     read;
     write;
END

in C

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

int main(void)
{
    char buf [2048];
    FILE *fp_in;
    FILE *fp_out;
    size_t ch_read;

    if ((fp_in = fopen ("as.exe", "rb")) == NULL)
    {
        fprintf (stderr, "failed to open input file\n");
        exit (EXIT_FAILURE);
    }

    if ((fp_out = fopen ("a.exe", "wb")) == NULL)
    {
        fprintf (stderr, "failed to open output file\n");
        fclose (fp_in);
        exit (EXIT_FAILURE);
    }

    do
    {
        if ((ch_read = fread (buf, 1, sizeof(buf), fp_in))
!= sizeof(buf)
             &&  ferror(fp_in))
        {
            fprintf (stderr, "read error\n");
            fclose (fp_in);
            fclose (fp_out);
            exit (EXIT_FAILURE);
        }

        if (fwrite (buf, 1, ch_read, fp_out) != ch_read)
        {
            fprintf (stderr, "write error\n");
            fclose (fp_in);
            fclose (fp_out);
            exit (EXIT_FAILURE);
        }
    }
    while (ch_read == sizeof(buf));

    fclose (fp_in);
    fclose (fp_out);

    return 0;

}

this compiles and runs without error but is otherwise untested.
Oh, the output file is the same size as the input file.


Bill wrote this:
    Whoa. Wow that's a lot of code

you havn't lived. :)
I'll copy and try to digest it. I don't
see enough simple code to get readily. I've never seen do used for this

I'm not a fan of do-while usually. I prefer to test before looping.
But in this case it naturally fit. It's another tool in the box
 
N

Nick Keighley

please leave attributions in *and* quote correctly

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

int main(void)
{
char buf [2048];
FILE *fp_in;
FILE *fp_out;
size_t ch_read; /*cool. */

if ((fp_in = fopen ("as.exe", "rb")) == NULL)
{
fprintf (stderr, "failed to open input file\n");
exit (EXIT_FAILURE);
}

if ((fp_out = fopen ("a.exe", "wb")) == NULL)
{
fprintf (stderr, "failed to open output file\n");
fclose (fp_in);
exit (EXIT_FAILURE);
}

do
{
if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
sizeof(buf)

Bill:
absolutlely brillant.

my genius is finally recognised.

Bill:
I always wondered how to deal with these return
values. Since fread returns a size_t declare a size_t to hold the return
value and use = to assign. Other than the fp=fopen example I haven't seen
this much.

it's the same technique. There's no law that says it only applies to
fopen()

Bill:
Not in code that I can read.

brilliant *and* readable

Nick:
&& ferror(fp_in))
{
fprintf (stderr, "read error\n");
fclose (fp_in);
fclose (fp_out);
exit (EXIT_FAILURE);
}

the error handling could be a subroutine (function)

if (fwrite (buf, 1, ch_read, fp_out) != ch_read)
{
fprintf (stderr, "write error\n");
fclose (fp_in);
fclose (fp_out);
exit (EXIT_FAILURE);
}
}
while (ch_read == sizeof(buf));

fclose (fp_in);
fclose (fp_out);

return 0;

}

this compiles and runs without error but is otherwise untested.
Oh, the output file is the same size as the input file.
Bill:
I'll tell you I can read very little of any C code I come across because
it's written by people who know what they're doing and they take shortcuts
in writing and it just leaves me in the dust. This I can make out enough to
not be just confused and actually learn a little.

happy to help. You seemed to be struggling with descriptions of what
to do
so I thought I'd try working code on you.


--
Nick Keighley

And you'll tell your children how excited you were to get 2GB to store
email,
and they'll laugh at you. Their nail polish has more than 2GB.
 
D

Default User

Nick said:
please leave attributions in and quote correctly

If the problem is that bill's replying without quote markers (I can't
be sure because I killfiled him) it's probably that Google
Groups/Outlook Express incompatibility thing. I've heard reports that
OE quotefix will correct that as well as some other problems.




Brian
 

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

Similar Threads

Can not read VCD file in Linux 8
URGENT 1
error 28
fread/fwrite 2 18
Working with files 1
malloc 40
no error by fscanf on reading from output file 18
Cgi and file access 8

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top