Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Segmentation fault with File I/O 1

Status
Not open for further replies.

GoAskAlice

Programmer
Aug 31, 2006
14
GB
Basically, I am learning C under Linux. What I am attempting is fwrite and fread functions on a file.

The write program works without fault, but when I try and read the data back from the file, the data is displayed, but then at the end of it all it states 'Segmentation fault'.

So, here is the code...can anyone help?

write file.c
----------------------------------------------------------
// Write to file using fwrite

#include <stdio.h>

int main() {
FILE *fp;
int writecount = 0;
int a[4];
a[0] = 12345678;
a[1] = 23456789;
a[2] = 34567891;
a[3] = 45678912;
if (( fp = fopen( "items.txt", "w" )) == NULL ){
perror ( "Opening data file" ); return -1;
}
writecount = fwrite( a, sizeof (a), 8, fp );




if ( fclose(fp) != 0)
{
fprintf(stderr, "Error closing file.\n");
return -2;
}
return 0;
}
----------------------------------------------------------



read file.c
-----------------------------------------------------------
// Read file using fread

#include <stdio.h>

int main() {
FILE *fp;
int a[4];
int readcount = 0;
int c;

if (( fp = fopen( "items.txt", "r+" )) == NULL){
perror( "Opening data file" );
return -1;
}

readcount = fread( a, sizeof( a ), 8, fp);

for( c = 0; c < 4; c++ ) {
printf("%i\n",a[c]);
}

if ( fclose(fp) != 0)
{
fprintf(stderr, "Error closing file.\n");
return -2;
}
return 0;
}
-----------------------------------------------------------
 
No worries, I figured it out.

The problem was with the fread() function.

Basically, I was asking it to read 8 times the data inside the array 'a'.

So, like,

instead of;
readcount = fread( a, sizeof( a ), 8, fp);

I used;
readcount = fread( a, sizeof( a ), 1, fp);

And this extracted all the data without the Segmentation Fault.

 
I suggest another way.

/*WRITING TO FILE*/
Code:
#include <stdio.h>

int main() {
FILE *fp;
int writecount = 0;
int a[4];
int size, i/*in loop*/;
/*if you don't know actual array size you can always check it*/
/*size=sizeof(a)/sizeof(a[0]);*/
/*but we know it's 4*/
size=4;
a[0] = 12345678;
a[1] = 23456789;
a[2] = 34567891;
a[3] = 45678912;
if (( fp = fopen( "items", "w" )) == NULL ){
    perror ( "Opening data file" ); return -1;
}
for (i=0; i<size; i++){
writecount=fprintf(fp,"%d \n",a[i]);
 if(writecount<0){
  perror("Error writing to a file");
  return -1;
}
}


if ( fclose(fp) != 0)
{
    fprintf(stderr, "Error closing file.\n");
    return -2;
}
return 0;
}

/*READING FROM FILE*/
Code:
#include <stdio.h>

int main() {
FILE *fp;
int writecount = 0;
int a[4];
int numberOfObjects=4	, i/*in loop*/;
if (( fp = fopen( "items", "r" )) == NULL ){
    perror ( "Opening data file" ); return -1;
}
for (i=0; i<numberOfObjects; i++){
writecount=fscanf(fp,"%d",&a[i]);
 if(writecount<0){
  perror("Error writing to a file");
  return -1;
}
}

for (i=0; i<numberOfObjects; i++)
printf("%d \n",a[i]);

if ( fclose(fp) != 0)
{
    fprintf(stderr, "Error closing file.\n");
    return -2;
}
return 0;
}
Hope it will be useful for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top