473,766 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segmentation fault with file io while closing main

Hi,
I created a test file for my application by writing structures to a
binary file. To make sure that I will get correct data for the
pointers inside the structure, i wrote actual data they pointed to and
data size/len to the file, after the structure. But when I tried to
read the file in same sequence, it gives me segmentation fault.
When I debugged the program with GDB, it executed well until
fclose() and read all the member of sturecture correrctly, but after
that at closing mains bracket it gave me SIGSEGV. I am posting code of
the read-file operation here. Please tell me why i am getting fault.
To make code compact here, I have removed checks for return values
of fread().

int main(int argc, char *argv[]){
int rc=0;
int len=0;
FILE *fp;
struct kml_rec *rec;

fp = fopen("cfile.km l","rb");
while((rc = fread(rec,sizeo f(struct kml_rec),1,fp)) ){
rc = fread(&len,size of(int),1,fp); //HDR
size
rec->prefix.hdr = (struct kml_prefix_hdr
*)malloc(len);
rc = fread(rec->prefix.hdr,len ,1,fp);
//Header

rc = fread(&len,size of(int),1,fp); //Path
Len
rec->path = (char *)malloc(len+1) ;
rc = fread(rec->path,len,sizeo f(char),fp); //Path
rec->path[len]='\0';

rc = fread(&len,size of(int),1,fp); //Name
len
rec->name = (char *)malloc(len+1) ;
rc = fread(rec->name,len,sizeo f(char),fp); //Name
rec->name[len]='\0';

rc = fread(&len,size of(int),1,fp);
//Target len
rec->target = (char *)malloc(len+1) ;
rc = fread(rec->target,len,siz eof(char),fp);
//Target
rec->target[len]='\0';

printf("%s %s %s %s\n",
commandp(rec->prefix.hdr->opcode),rec->path,rec->name,rec->target);
free(rec->prefix.hdr);
free(rec->path);
free(rec->name);
free(rec->target);
}
fflush(fp);
if((rc=fclose(f p))!=0)
printf("ERROR: in closing file.\n");
}
Nov 13 '05 #1
2 5547
Atulvid wrote:
Hi,
I created a test file for my application by writing structures to a
binary file. To make sure that I will get correct data for the
pointers inside the structure, i wrote actual data they pointed to and
data size/len to the file, after the structure. But when I tried to
read the file in same sequence, it gives me segmentation fault.
When I debugged the program with GDB, it executed well until
fclose() and read all the member of sturecture correrctly, but after
that at closing mains bracket it gave me SIGSEGV. I am posting code of
the read-file operation here. Please tell me why i am getting fault.
To make code compact here, I have removed checks for return values
of fread().

int main(int argc, char *argv[]){
int rc=0;
int len=0;
FILE *fp;
struct kml_rec *rec;

fp = fopen("cfile.km l","rb");
while((rc = fread(rec,sizeo f(struct kml_rec),1,fp)) ){


Where does rec point? Nowhere!

So either write:
struct kml_rec *rec = malloc(sizeof *rec);
or:
struct kml_rec rec = {0};
fread(&rec, ...

Jirka

Nov 13 '05 #2
Atulvid wrote:
Hi,
I created a test file for my application by writing structures to a
binary file. To make sure that I will get correct data for the
pointers inside the structure, i wrote actual data they pointed to and
data size/len to the file, after the structure. But when I tried to
read the file in same sequence, it gives me segmentation fault.
When I debugged the program with GDB, it executed well until
fclose() and read all the member of sturecture correrctly, but after
that at closing mains bracket it gave me SIGSEGV. I am posting code of
the read-file operation here. Please tell me why i am getting fault.
To make code compact here, I have removed checks for return values
of fread().

int main(int argc, char *argv[]){
int rc=0;
int len=0;
FILE *fp;
struct kml_rec *rec;

fp = fopen("cfile.km l","rb");
while((rc = fread(rec,sizeo f(struct kml_rec),1,fp)) )


You never allocated space for rec to point to. This is easy to fix with
using malloc:
struct kml_rec rec; /* no longer a pointer */
^ notice no '*'
while((rc = fread(&rec,size of rec,1,fp)))
^ notice '&', and delinking the size from the
type

--
Martin Ambuhl

Nov 13 '05 #3

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

Similar topics

2
6809
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script works fine and do all what I need. But at the end of the execution, I can read "Segmentation Fault". The segmentation fault appear at the end of my script execution,
6
2315
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
10
401
by: F?bio Botelho | last post by:
Sorry About the english.... This program copy one file to another , but when i run it it's gives-me an error: Segmentation fault I don't understand ... because it pass my test : opens the first file and create the second. If any one could give me a help I would be apreciated. thanks.
5
2997
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
12
2312
by: nae zot bba la | last post by:
Hi, very short code here. Basically I want to open a file called instruction in a++ mode and then write data of %s type to it, then read from the same file and print to screen. The code compiles but when I execute the binary and enter some string to expect the program to print them for me to screen, segmentation fault occurs. Any ideas anyone? #include <stdio.h> #include <stdlib.h> char *add;
27
3366
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
3
5187
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
6
2479
by: Wes | last post by:
I'm running FreeBSD 6.1 RELEASE #2. The program is writting in C++. The idea of the program is to open one file as input, read bytes from it, do some bitwise operations on the bytes, and then write them to this second file. However, when the second file is 15360 bytes long, the program dies with a "Segmentation Fault (core dumped)" error! I checked with gdb, and it says the last function to run was memcpy() from libc, which would...
6
5043
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on gcc. The only difference is that in first I only call "main" and in second call
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.