473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Question on streams

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

}

Aug 24 '07 #1
4 1204
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
Aug 24 '07 #2

"K S M" <no****@no.spamwrote in message
news:sl********************@nospam.invalid...
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

Aug 24 '07 #3
Default User wrote:
What is entry,
It's an unused keyword in K&R C.

--
pete
Aug 25 '07 #4
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

Aug 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ronald Legere | last post by:
The new itertools stuff is pretty cool. One thing that bothers me though is that there seems to be no way to copy an iterator. How does one work around this? Is there a trick that I am missing? ...
0
by: David | last post by:
This might be a report question but I think it is more a query issue than a report. I am a novice at bothAccess reporting and queries and have been struggling to build the following. (Note using...
8
by: Alex | last post by:
Hi all, can someone please show me how to get the size of a file with ANSI C? both in text mode and binary mode. thanks in advance.
1
by: Chris | last post by:
I'm reading up on streams and I have two articles that seem to conflict with each other. One article describes streams and lists a few of the major ones (FileStream, Memory Stream, Network...
5
by: Dave Taylor | last post by:
I'm trying to derive a class from a typed DataSet to add some methods to one of the DataTables. I would like to keep the names the same in the derived class as in the base class, so I have used...
29
by: Bill Cunningham | last post by:
I wrote this small program to read a 512 block of binary data and write the same to a file. My code compiled well. The only thing is when I ran the compilers binary instead of a data file of 512...
7
by: Boltar | last post by:
Hi I'm using ifstream (which I hardly ever use) to read an ascii text file (containing numbers delimited by newlines) in a loop using something like: ifstream infile("filename") int value; ...
8
by: Tophurious | last post by:
First, let me start by saying I know what an overflow is (Too large of a value into something that can't hold it, i.e. 35000 into an int, or 0/0, etc). Now to the issue. I have a class that I am...
2
by: Damfino | last post by:
Hi all, Newbie question here wrt defining a class that will work on bits read from a binary file. How would you go about doing it? As an example please look at the structure of my data given...
3
by: Michel Esber | last post by:
Hi all, Db2 v8 FP15 LUW . create table T (ID varchar (24), ABC timestamp) There is an index for (ID, ABC), allowing reverse Scans. My application needs to determine MIN and MAX(ABC) for a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.