write a bit of code to read from a file and compare a string but it dies before it finishes.
the complier tells me there is a segment fault on line 31 can anyone help.

[LIST=1]
[*]#include <stdio.h>
[*]#include <string.h>
[*]#include <stdlib.h>

[*]#define max 100

[*]struct customer{
[*]       
[*]      char Fname[20];
[*]      char Lname[20];
[*]      char Address[80];
[*]      int lastTrans;
[*]}*cus[max];

[*]main()
[*]{
[*]  FILE *fptr;
[*]  char *filename = "index";
[*]    int i=0;   
[*]     
[*]      char name[20];
[*]      
[*]      
[*]      printf("name : ");
[*]      scanf("%s",&name);
[*]      
[*]      
[*]  
[*]  fptr = fopen(filename,"rb");
[*]  
[*]       while(!feof(fptr))//this is where it say's there is a 'segment fault' 
[*]           { 
[*]              fscanf(fptr,"%s%s%s%d",&cus[i]->Fname,&cus[i]->Lname,&cus[i]->Address,&cus[i]->lastTrans);
[*]              printf("%s %s\t%s\t%d\n",cus[i]->Fname,cus[i]->Lname,cus[i]->Address,cus[i]->lastTrans);
[*]              i++;
[*]              
[*]              if(!strcmp(name,cus[i]->Fname ))
[*]              {
[*]                printf("found %s",name);
[*]                break;
[*]                }
[*]           }      
[*]      system("pause");
[*]}
[/LIST]

Recommended Answers

All 12 Replies

*cus[max]; This declares 100 pointers of type customer, but you haven't point them to any memory, therefore you cannot use them in the fscanf() call.
Malloc can set apart memory of type structure customer and you can access it via cus pointers.

thanks very much Aia it's working now but now it's giving me the same error at line 24 but changed the while statement to a for loop so now it's working so thanks very much.nearly finished my project now but may need your help again thanks!!

It's never a good idea to control the exit of a loop by the use of the feof() function.
For some clear explanation, click here.
And since your are there, take a look at scanf for reading strings, as well. You won't regretted.

quickly came across another problem I can only read in one customer from the file it will onnly take in the first person in the file?

quickly came across another problem I can only read in one customer from the file it will onnly take in the first person in the file?

Post the modified version of your code.

commented: always helpful!! +1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define max 100

struct customer{
       
      char Fname[20];
      char Lname[20];
      char Address[80];
      int lastTrans;
}*cus[max];

main()
{
  FILE *fptr;
  char *filename = "index";
    int i=0;   
     
      char name[20];
      
      
     printf("name : ");
     scanf("%s",&name);
      
      cus[i] = malloc(sizeof(struct customer));
  
  fptr = fopen(filename,"rb");
  
       for(i;i<!feof(fptr);i++)
           { 
              fscanf(fptr,"%s%s%s%d",&cus[i]->Fname,&cus[i]->Lname,&cus[i]->Address,&cus[i]->lastTrans);
             // printf("%s %s\t%s\t%d\n",cus[i]->Fname,cus[i]->Lname,cus[i]->Address,cus[i]->lastTrans);
           
              
              if(!strcmp(cus[i]->Fname,name))
           {
              printf("\nfound %s\n",name);
                 
            }
            else 
            printf("\n\n%s not found\n",name);
           }      
      system("pause");
}

cus = malloc(sizeof(struct customer));
Only one time memory is set apart. In essence cus[0], because int i is set to 0;
The program hits the loop and makes use of that memory via cus[0], but in the next cycle it tries to make use of cus[1] which chuck of memory doesn't exist.
Remember to free that malloc-ed memory after you're done with it.
scanf( "%s", &name ); doesn't need the &. Is already a pointer to the first element of the string.
However I would stop any use of scanf() when you want to read a string.
fgets( name, sizeof name, stdin ) is a much safer way.

on that note using fgets() if I was reading in ints,chars etc from the same file. would i need to use fgets(name,sizeof name,stdin);
fgets(amount,sizeof amount,stdin); etc?

on that note using fgets() if I was reading in ints,chars etc from the same file. would i need to use fgets(name,sizeof name,stdin);
fgets(amount,sizeof amount,stdin); etc?

fgets accepts as parameters an array of char, the length of that array ( how many chars can read into it without going over the space set apart for it ), and the file handle from where it needs to read; in this case the standard input buffer stream, but it can be any file you have opened for reading.
It doesn't accept integers. However integer can be read as strings and then converted by another function like sscanf() ( notice the extra s which stand for string ).

Quick and dirty way of getting an integer from user.

/*
 * =====================================
 *
 *    Description: show how sscanf works 
 *
 *        Version:  1.0
 *        Created:  2/12/2008 5:15:26 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Aia 
 *        Company:  AiaONE
 *
 * =====================================
 */

#include <stdio.h>

int main( void )
{
	char integer[13];
	int result = 0; /* default value */

	printf( "Enter an integer: " );
	fflush( stdout );

	if( fgets( integer, sizeof integer, stdin ) )
	{
		if ( sscanf( integer, "%d", &result ) < 1 )
		{
			/* sscanf failed, going with default */
			puts( "Sorry not integer entered" );
		}
	}	
	printf( "result = %d\n", result );

	getchar(); /* pause if less than 13 characters were entered */
	return 0;
}

Aia still need your help!!
tried changing fscanf();,to fgets() like so

[LIST=1]
for(i;i<!feof(fptr);i++)
           { 
               cus[i] = malloc(sizeof(struct customer));
                               
              fgets(cus[i]->Fname,sizeof(cus[i]->Fname),fptr);
              fgets(cus[i]->Lname,sizeof(cus[i]->Lname),fptr);
              fgets(cus[i]->Address,sizeof(cus[i]->Address),fptr);
              fgets(temp,sizeof(temp),fptr);
              cus[i] -> lastTrans = (unsigned) atoi(temp);
              
              printf("%s %s\t%s\t%s\n",cus[i]->Fname,cus[i]->Lname,cus[i]->Address,cus[i]->lastTrans);
           
              
              if(!strcmp(cus[i]->Fname,name))
                {
                  printf("\nfound %s\n",name);
                 
                }
            else 
                 printf("\n\n%s not found\n",name);
           }      

[/LIST]


It will read in two customers and print them out followed by null.Also the strcmp() will not compare the names

fscanf() would read from the same line and will read any data according to the % format you give it.
e.g

fscanf( file, "%s%d", stringArray, &digit );

will scand a line in the opened file and will try to read a string which will store in stringArray, and following it will try to read an integer; storing it in digit. After that it will return how many items was successful in reading or EOF if it failed. Remember everything from one line alone.

fgets() works differently.
e.g

fgets( string, totalsize, file );

will start reading from file until it reaches the totalsize less one for the '\0' terminator, or encounters a newline '\n' or EOF end of file, whichever is first of these. Upon completion it will return a NULL pointer if the EOF was reached and nothing was read, or an error occurred; if it was successful it returns a pointer to string changed with what it was read.

Suppose you have these words in a file:
Jiminy Cricket
And you want to read it into char name[12]; and char lastname[12];
if you call

fgets( name, sizeof name, file );
fgets( lastname, sizeof lastname, file );

What do you think it will produce?

char name[12] = "Jiminy Cric"; /* count the characters */
char lastname[12] = "ket\n";

I hope this will help telling you what's happening in your code.
Did you take a look at why feof() is not a good idea inside a loop?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.