Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > error

Reply
Thread Tools

error

 
 
Bill Cunningham
Guest
Posts: n/a
 
      08-30-2012
Mark Storkamp wrote:
> In article <k1ofvu$g2o$(E-Mail Removed)>,
> "Bill Cunningham" <(E-Mail Removed)> 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 ^


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

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



 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-30-2012
"Bill Cunningham" <(E-Mail Removed)> writes:
> 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-30-2012
Casey Carter <(E-Mail Removed)> writes:
> 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);


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

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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"
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      08-30-2012
On Thu, 30 Aug 2012 17:03:48 -0400, "Bill Cunningham"
<(E-Mail Removed)> wrote:

>Bill Cunningham wrote:
>> Mark Storkamp wrote:
>>> In article <k1ofvu$g2o$(E-Mail Removed)>,
>>> "Bill Cunningham" <(E-Mail Removed)> 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.


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.

--
Remove del for email
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      08-30-2012
On Thu, 30 Aug 2012 17:16:40 -0400, "Bill Cunningham"
<(E-Mail Removed)> wrote:

>Mark Storkamp wrote:
>> In article <k1ofvu$g2o$(E-Mail Removed)>,
>> "Bill Cunningham" <(E-Mail Removed)> 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 ^

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

--
Remove del for email
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      08-30-2012
Barry Schwarz wrote:

> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      08-30-2012
Barry Schwarz wrote:

> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      08-30-2012
Barry Schwarz wrote:

> 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


 
Reply With Quote
 
Edward A. Falk
Guest
Posts: n/a
 
      08-30-2012
In article <(E-Mail Removed)>,
Kenneth Brody <(E-Mail Removed)> wrote:
>
>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.

--
-Ed Falk, (E-Mail Removed)
http://thespamdiaries.blogspot.com/
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      08-31-2012
Kenneth Brody wrote:
> On 8/30/2012 6:55 PM, Bill Cunningham wrote:
>> Barry Schwarz wrote:
>>
>>> 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.

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0xffc Thread 0x228 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 0 03-30-2006 03:16 PM
Error connecting to SQLExpress 2005 locally (error: 26 - Error Locating Server/Instance Specified) hfk0 ASP .Net 2 03-27-2006 08:43 PM
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x8fc Thread 0x934 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 1 03-24-2006 04:20 PM
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x8fc Thread 0x934 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 0 03-24-2006 02:22 PM
Error 500: ERROR: Cannot forward. Writer or Stream already obtained. Error JavaQueries Java 1 03-01-2005 06:30 PM



Advertisments