Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   error (http://www.velocityreviews.com/forums/t951610-error.html)

Bill Cunningham 08-30-2012 07:47 PM

error
 
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;
}



Lanarcam 08-30-2012 07:55 PM

Re: error
 
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);
}


Bill Cunningham 08-30-2012 08:05 PM

Re: error
 
Lanarcam wrote:

> 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



Lanarcam 08-30-2012 08:11 PM

Re: error
 
Le 30/08/2012 22:05, Bill Cunningham a écrit :
> Lanarcam wrote:
>
>> 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.
>

Yes, in short:

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

fclose(fp);
}


Mark Storkamp 08-30-2012 08:18 PM

Re: error
 
In article <k1ofvu$g2o$1@speranza.aioe.org>,
"Bill Cunningham" <nospam@nspam.invalid> wrote:

> 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 ^

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


Casey Carter 08-30-2012 08:30 PM

Re: error
 
On 2012-08-30 14:47, Bill Cunningham wrote:
> 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);


Bill Cunningham 08-30-2012 08:57 PM

Re: error
 
Mark Storkamp wrote:
> In article <k1ofvu$g2o$1@speranza.aioe.org>,
> "Bill Cunningham" <nospam@nspam.invalid> wrote:
>
>> 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;
>> }




Bill Cunningham 08-30-2012 09:03 PM

Re: error
 
Bill Cunningham wrote:
> Mark Storkamp wrote:
>> In article <k1ofvu$g2o$1@speranza.aioe.org>,
>> "Bill Cunningham" <nospam@nspam.invalid> wrote:
>>
>>> 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.

Bill



Keith Thompson 08-30-2012 09:11 PM

Re: error
 
Lanarcam <lanarcam1@yahoo.fr> writes:
[...]
> Yes, in short:
>
> if (fp = fopen() != NULL)
> {
> // do something
>
> fclose(fp);
> }


You need parentheses:

if ((fp = fopen()) != NULL)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Bill Cunningham 08-30-2012 09:15 PM

Re: error
 
Casey Carter wrote:
> On 2012-08-30 14:47, Bill Cunningham wrote:
>> 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




All times are GMT. The time now is 05:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.