Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question on streams

Reply
Thread Tools

Question on streams

 
 
K S M
Guest
Posts: n/a
 
      08-24-2007
The code below causes a run time error. The point it breaks down
is when I call createLog, is there something wrong with the way I open
and close the streams. There seems to be no issues with it when I
compile and run this code under linux but when I use Win NT I get
a "General Protection Fault" SISEGV error.

Please help.

strcpy(&file[0],"backup.txt");

strcpy(&file2[0], file);

stream = (void *)malloc(4);
stream = fopen(&file[0], "r");

if(!stream){
printf("File %s NOT FOUND! \n\n\t...Program Terminating...\n", file);
exit(0);
}

quit = 0;

scanf("%c", &entry);

if(entry =='s'){
/* backup is a method that I have written... */
backup(stream);
fclose(stream);
free(stream);

stream = fopen(&file2[0], "r");

/* Create Log is a Method that I have written... */

createLog(stream);
free(stream);
printf("\n");

}

 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      08-24-2007
K S M wrote:

> The code below causes a run time error. The point it breaks down
> is when I call createLog, is there something wrong with the way I open
> and close the streams. There seems to be no issues with it when I
> compile and run this code under linux but when I use Win NT I get
> a "General Protection Fault" SISEGV error.
>
> Please help.


Help us help you by posting a complete, minimal program that
demonstrates the problem.

> strcpy(&file[0],"backup.txt");


What is file, and how is it defined? We don't know.

> strcpy(&file2[0], file);


What is file2, and how is it defined? We don't know.

> stream = (void *)malloc(4);


What is stream, and how is it defined? We don't know. Why are you
allocating four bytes of memory for it?

> stream = fopen(&file[0], "r");


This just leaked the four bytes from above. What is it you think that
accomplished?

> if(!stream){
> printf("File %s NOT FOUND! \n\n\t...Program Terminating...\n",
> file); exit(0);
> }
>
> quit = 0;
>
> scanf("%c", &entry);


What is entry, blah blah.

> if(entry =='s'){
> /* backup is a method that I have written... */
> backup(stream);
> fclose(stream);
> free(stream);


There's likely your problem. You freed something that wasn't allocated
by malloc() or relatives.





Brian
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      08-24-2007

"K S M" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> The code below causes a run time error. The point it breaks down
> is when I call createLog, is there something wrong with the way I open
> and close the streams. There seems to be no issues with it when I
> compile and run this code under linux but when I use Win NT I get
> a "General Protection Fault" SISEGV error.
>
> Please help.
>
> strcpy(&file[0],"backup.txt");
>
> strcpy(&file2[0], file);
>
> stream = (void *)malloc(4);
> stream = fopen(&file[0], "r");
>
> if(!stream){
> printf("File %s NOT FOUND! \n\n\t...Program Terminating...\n", file);
> exit(0);
> }
>
> quit = 0;
>
> scanf("%c", &entry);
>
> if(entry =='s'){
> /* backup is a method that I have written... */
> backup(stream);
> fclose(stream);
> free(stream);
>
> stream = fopen(&file2[0], "r");
>
> /* Create Log is a Method that I have written... */
>
> createLog(stream);
> free(stream);
> printf("\n");
>
> }
>


&file2[0] is strictly correct, but betrays a misunderstanding. Arrays decay
to pointers when you pass them to functions. Therefore fopen() normally
takes the name of an array, or a hardcoded string literal.
Strings are just arrays of chars, with a zero on the end to indicate
termination.

I suspect that if you post your declarations of file and file2 we will see
the problem.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm



 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-25-2007
Default User wrote:

> What is entry,


It's an unused keyword in K&R C.

--
pete
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      08-25-2007
On Fri, 24 Aug 2007 23:14:44 +0200, K S M wrote:

> The code below causes a run time error. The point it breaks down
> is when I call createLog, is there something wrong with the way I open
> and close the streams. There seems to be no issues with it when I
> compile and run this code under linux but when I use Win NT I get
> a "General Protection Fault" SISEGV error.
>
> Please help.
>
> strcpy(&file[0],"backup.txt");
>
> strcpy(&file2[0], file);
>
> stream = (void *)malloc(4);

What on hell are you doing? malloc returns a void *, so all what
that cast do is hide errors due to forgetting to include stdlib.h.
> stream = fopen(&file[0], "r");

You don't need those four bytes anymore?
(Maybe stream is a FILE **, you happen to somehow know that
sizeof (FILE *) is 4, the void* gets converted to FILE **, but
then here you need *stream = fopen etc.)
>
> if(!stream){
> printf("File %s NOT FOUND! \n\n\t...Program Terminating...\n", file);
> exit(0);

Writing to stdout rather than stderr and returning success in this
case is quite bizarre.
> }
>
> quit = 0;
>
> scanf("%c", &entry);

What's wrong with getchar()?
>
> if(entry =='s'){
> /* backup is a method that I have written... */
> backup(stream);
> fclose(stream);
> free(stream);

The original pointer to memory allocated by malloc was
overwritten. You are trying to free a FILE object set up by fopen
and possibly made useless or destroyed by fclose. On the
DeathStation 9000 this would "free" your neurons to die at their
own will. You meant backup(*stream), fclose(*stream)?


> stream = fopen(&file2[0], "r");
>
> /* Create Log is a Method that I have written... */
>
> createLog(stream);
> free(stream);
> printf("\n");
>
> }


--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

 
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
Re: Question: OSPF path selection based on streams bod43 Cisco 1 09-29-2009 02:28 AM
closures and streams question grocery_stocker Perl Misc 3 02-05-2009 11:15 PM
Basic question on streams John Simeon C++ 16 07-25-2007 06:22 PM
question about streams Jackie Java 6 04-30-2006 07:51 AM
odd C++ crash question with streams laniik C++ 4 04-08-2005 03:00 AM



Advertisments