error


B

Bill Cunningham

I'm hoping that someone can point out the error I'm getting here and seg
fault. I find it very hard to read code posted by these newsreaders. I'm
using OE. I hope someone can read this code.

Bill
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main()
{
errno=0;
size_t nread, nwrite, nbytes;
nread = nwrite = nbytes = 0;
char buf[9000] = { 0 };
FILE *fp, *fp2;
if ((fp = fopen("6", "rb")) == NULL)
perror("fopen 1");
if ((fp2 = fopen("a", "wb")) == NULL)
perror("fopen 2");
do {
nread = fread(buf, sizeof(buf), 1, fp);
nwrite = fwrite(buf, sizeof(buf), nread, fp2);
} while (!feof(fp));
fclose(fp);
fclose(fp2);
printf("%d %d\n", nread, nwrite);
printf("%s\n",strerror(errno));
return 0;
}
 
Ad

Advertisements

L

Lanarcam

Le 30/08/2012 21:47, Bill Cunningham a écrit :
I'm hoping that someone can point out the error I'm getting here and seg
fault. I find it very hard to read code posted by these newsreaders. I'm
using OE. I hope someone can read this code.

Bill
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main()
{
errno=0;
size_t nread, nwrite, nbytes;
nread = nwrite = nbytes = 0;
char buf[9000] = { 0 };
FILE *fp, *fp2;
if ((fp = fopen("6", "rb")) == NULL)
perror("fopen 1");
if ((fp2 = fopen("a", "wb")) == NULL)
perror("fopen 2");
do {
nread = fread(buf, sizeof(buf), 1, fp);
nwrite = fwrite(buf, sizeof(buf), nread, fp2);
} while (!feof(fp));
fclose(fp);
fclose(fp2);
printf("%d %d\n", nread, nwrite);
printf("%s\n",strerror(errno));
return 0;
}
Not sure, but you use fclose(fp) outside of the
block where fopen() succeeds.

if ((fp = fopen("6", "rb")) == NULL)
{
perror("fopen 1");
}
else
{
if ((fp2 = fopen("a", "wb")) == NULL)
perror("fopen 2");
else
{
do
{
nread = fread(buf, sizeof(buf), 1, fp);
nwrite = fwrite(buf, sizeof(buf), nread, fp2);
} while (!feof(fp));

fclose(fp2);
}
fclose(fp);
}
 
B

Bill Cunningham

Lanarcam said:
Not sure, but you use fclose(fp) outside of the
block where fopen() succeeds.

if ((fp = fopen("6", "rb")) == NULL)
{
perror("fopen 1");
}
else
{
if ((fp2 = fopen("a", "wb")) == NULL)
perror("fopen 2");
else
{
do
{
nread = fread(buf, sizeof(buf), 1, fp);
nwrite = fwrite(buf, sizeof(buf), nread, fp2);
} while (!feof(fp));

fclose(fp2);
}
fclose(fp);
}

Ok I think I see what your getting at. The ifs should have bigger blocks
instead of the one statement perrors.

Bill
 
L

Lanarcam

Le 30/08/2012 22:05, Bill Cunningham a écrit :
Ok I think I see what your getting at. The ifs should have bigger blocks
instead of the one statement perrors.
Yes, in short:

if (fp = fopen() != NULL)
{
// do something

fclose(fp);
}
 
M

Mark Storkamp

Bill Cunningham said:
I'm hoping that someone can point out the error I'm getting here and seg

Why don't you point out the error you're getting here, then someone can
help to explain what is causing the error. Or are we to just guess what
error you are getting?
fault. I find it very hard to read code posted by these newsreaders. I'm
using OE. I hope someone can read this code.

Bill
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main()
{
errno=0;
size_t nread, nwrite, nbytes;
nread = nwrite = nbytes = 0;
char buf[9000] = { 0 };

Your problem could be here ^
FILE *fp, *fp2;
if ((fp = fopen("6", "rb")) == NULL)
perror("fopen 1");
if ((fp2 = fopen("a", "wb")) == NULL)
perror("fopen 2");
do {
nread = fread(buf, sizeof(buf), 1, fp);
nwrite = fwrite(buf, sizeof(buf), nread, fp2);
} while (!feof(fp));
fclose(fp);
fclose(fp2);
printf("%d %d\n", nread, nwrite);

Or it could be here ^
 
Ad

Advertisements

B

Bill Cunningham

Mark said:
Why don't you point out the error you're getting here, then someone
can help to explain what is causing the error. Or are we to just
guess what error you are getting?

Well in the code I compile
fopen 2:text file busy
fault. I find it very hard to read code posted by these newsreaders.
I'm using OE. I hope someone can read this code.

Bill
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main()
{
errno=0;
size_t nread, nwrite, nbytes;
nread = nwrite = nbytes = 0;
char buf[9000] = { 0 };

Your problem could be here ^
FILE *fp, *fp2;
if ((fp = fopen("6", "rb")) == NULL)
perror("fopen 1");
if ((fp2 = fopen("a", "wb")) == NULL)
perror("fopen 2");
do {
nread = fread(buf, sizeof(buf), 1, fp);
nwrite = fwrite(buf, sizeof(buf), nread, fp2);
} while (!feof(fp));
fclose(fp);
fclose(fp2);
printf("%d %d\n", nread, nwrite);

Or it could be here ^
printf("%s\n",strerror(errno));
return 0;
}
 
B

Bill Cunningham

Bill said:
Mark said:
Why don't you point out the error you're getting here, then someone
can help to explain what is causing the error. Or are we to just
guess what error you are getting?

Well in the code I compile
fopen 2:text file busy
fault. I find it very hard to read code posted by these newsreaders.
I'm using OE. I hope someone can read this code.

Bill
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main()
{
errno=0;
size_t nread, nwrite, nbytes;
nread = nwrite = nbytes = 0;
char buf[9000] = { 0 };

Your problem could be here ^
FILE *fp, *fp2;
if ((fp = fopen("6", "rb")) == NULL)
perror("fopen 1");
if ((fp2 = fopen("a", "wb")) == NULL)
perror("fopen 2");
do {
nread = fread(buf, sizeof(buf), 1, fp);
nwrite = fwrite(buf, sizeof(buf), nread, fp2);
} while (!feof(fp));
fclose(fp);
fclose(fp2);
printf("%d %d\n", nread, nwrite);

Or it could be here ^
printf("%s\n",strerror(errno));
return 0;
}

Wait a minute ! wait! Silly me. I have a file areadly called "a" and
need to set the w to w+. Let me try that.

Bill
 
B

Bill Cunningham

Casey said:
int main()
{ [snip]
nread = fread(buf, sizeof(buf), 1, fp);

nread = fread(buf, 1, sizeof(buf), fp);
nwrite = fwrite(buf, sizeof(buf), nread, fp2);

nwrite = fwrite(buf, 1, nread, fp2);

You're right. That was part of the problem. Have I been reading this
functions parameters backward. Why number of items 1 in fread?

Bill
 
B

Bill Cunningham

Mark said:
Bill Cunningham said:
I'm hoping that someone can point out the error I'm getting here
and seg

Why don't you point out the error you're getting here, then someone
can help to explain what is causing the error. Or are we to just
guess what error you are getting?
fault. I find it very hard to read code posted by these newsreaders.
I'm using OE. I hope someone can read this code.

Bill
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main()
{
errno=0;
size_t nread, nwrite, nbytes;
nread = nwrite = nbytes = 0;
char buf[9000] = { 0 };

Your problem could be here ^

Yep! Yep! These cannnot be initialized. The compiler will complain that
nread and nwrite aren't declared for some reason.
 
Ad

Advertisements

K

Keith Thompson

Bill Cunningham said:
I'm hoping that someone can point out the error I'm getting here and seg
fault. I find it very hard to read code posted by these newsreaders. I'm
using OE. I hope someone can read this code.

Bill
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main()
{
errno=0;
size_t nread, nwrite, nbytes;

You never use nbytes.
nread = nwrite = nbytes = 0;
char buf[9000] = { 0 };
FILE *fp, *fp2;
if ((fp = fopen("6", "rb")) == NULL)
perror("fopen 1");
if ((fp2 = fopen("a", "wb")) == NULL)
perror("fopen 2");

You detect fopen() errors, but you don't handle them. perror() merely
prints an error message; it doesn't terminate the program. If fopen()
fails, you print an error message and continue executing.
do {
nread = fread(buf, sizeof(buf), 1, fp);
nwrite = fwrite(buf, sizeof(buf), nread, fp2);
} while (!feof(fp));

Don't use feof() to decide when to terminate an input loop. Use the
result returned by the function you're using to read the data. feof()
is useful mainly to distinguish between an end-of-file condition and an
error condition. (If there's an error, you'll have an infinite loop,
because feof() will never return a true result; instead, ferror() will
do so. No, I'm not telling you to call ferror() instead of feof().)
fclose(fp);
fclose(fp2);
printf("%d %d\n", nread, nwrite);

nread and nwrite are of type size_t. Use "%zu" rather than "%d" (or one
of the usual workarounds if your implementation doesn't support "%zu").
printf("%s\n",strerror(errno));
return 0;
}

There may be other errors; I haven't spent much time on this.
 
K

Keith Thompson

Casey Carter said:
int main()
{ [snip]
nread = fread(buf, sizeof(buf), 1, fp);

nread = fread(buf, 1, sizeof(buf), fp);
nwrite = fwrite(buf, sizeof(buf), nread, fp2);

nwrite = fwrite(buf, 1, nread, fp2);

The ordering of the second and third arguments to fread() and
fwrite() affects the value returned (because it's counting different
things), but not how much they attempt to read or write.

Since the program doesn't do anything with the results (other than
printing them), it probably doesn't matter much. But you should
definitely think about what you want to count when calling fread()
and fwrite().

Here's something I wrote about the difference:

http://stackoverflow.com/a/8589688/827263
 
B

Barry Schwarz

Bill said:
Mark said:
I'm hoping that someone can point out the error I'm getting here
and seg

Why don't you point out the error you're getting here, then someone
can help to explain what is causing the error. Or are we to just
guess what error you are getting?

Well in the code I compile
fopen 2:text file busy
fault. I find it very hard to read code posted by these newsreaders.
I'm using OE. I hope someone can read this code.

Bill
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main()
{
errno=0;
size_t nread, nwrite, nbytes;
nread = nwrite = nbytes = 0;
char buf[9000] = { 0 };

Your problem could be here ^

FILE *fp, *fp2;
if ((fp = fopen("6", "rb")) == NULL)
perror("fopen 1");
if ((fp2 = fopen("a", "wb")) == NULL)
perror("fopen 2");
do {
nread = fread(buf, sizeof(buf), 1, fp);
nwrite = fwrite(buf, sizeof(buf), nread, fp2);
} while (!feof(fp));
fclose(fp);
fclose(fp2);
printf("%d %d\n", nread, nwrite);

Or it could be here ^

printf("%s\n",strerror(errno));
return 0;
}

Wait a minute ! wait! Silly me. I have a file areadly called "a" and
need to set the w to w+. Let me try that.

I see you have reverted back to your standard of making random guesses
at both the problem and the solution. By now you should know it is
not effective.
 
B

Barry Schwarz

Mark said:
Bill Cunningham said:
I'm hoping that someone can point out the error I'm getting here
and seg

Why don't you point out the error you're getting here, then someone
can help to explain what is causing the error. Or are we to just
guess what error you are getting?
fault. I find it very hard to read code posted by these newsreaders.
I'm using OE. I hope someone can read this code.

Bill
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main()
{
errno=0;
size_t nread, nwrite, nbytes;
nread = nwrite = nbytes = 0;
char buf[9000] = { 0 };

Your problem could be here ^

Yep! Yep! These cannnot be initialized. The compiler will complain that
nread and nwrite aren't declared for some reason.

What on earth are you talking about? Don't guess what the compiler
will do. Show an actual compilation and the accompanying error
messages.
 
B

Bill Cunningham

Barry said:
What on earth are you talking about? Don't guess what the compiler
will do. Show an actual compilation and the accompanying error
messages.

Well that's what it said. Unless I'm imagining it. It said my objects
where undeclared.

B
 
Ad

Advertisements

B

Bill Cunningham

Barry said:
I see you have reverted back to your standard of making random guesses
at both the problem and the solution. By now you should know it is
not effective.

Guessed about what? "Let me try that." Wasn't a guess but a statement of
fact.

B
 
B

Bill Cunningham

Barry said:
I see you have reverted back to your standard of making random guesses
at both the problem and the solution. By now you should know it is
not effective.

When you rarely get good advice, atleast not without complaints on clc,
guessing sometimes is all that's left.

B
 
E

Edward A. Falk

Well, it's telling you that the fopen() for the file "a" failed.

Then, since you fwrite() to that file anyway, there's your segv.

OT hint: The error "text file busy" means that you are attempting to open an
executing program for write.

Good call.
 
Ad

Advertisements

B

Bill Cunningham

Kenneth said:
What was the exact wording of the error? And to which line does the
error message refer?

I can't remember exactly. I fixed it and got it working though. :)

B
 

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

code 34
fread/fwrite 2 18
A Question of Style 51
fseek 17
Do I need a FIFO queue? 8
epoll networking code review 4
no error by fscanf on reading from output file 18
can't get server socket working 9

Top