Question on streams

K

K S M

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");

}
 
D

Default User

K said:
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
 
M

Malcolm McLean

K S M said:
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.
 
A

Army1987

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");

}
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top