I've already write a program which allow you to enter name, address and write in the file name,length of name, address ,length of address. File is called "input.dat"


Now i need to write another program that reads the file "input.dat" (the one above), displays the name, the size of the name, the address and the size of the address for the person with the longest name as well as for the person with the longest address. The program should also display the average size of names and average size of addresses in the file.

I tried to do this but it doesnt work! can someone help, tell me whats wrong in it?wat need to be added?

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>
#include <stdlib.h>


int main()
{
	int file_person=open("input09.dat",O_RDONLY);
	
	char name[20];
	char address[20];
	char name_len[2];
	char address_len[2];

	int actual_pt=lseek(file_person,0,SEEK_SET);
	int ending_pt=lseek(file_person,0,SEEK_END);
	int max_name=0,max_add=0;

	while (actual_pt<=ending_pt)
	{
		
		
		read(file_person,&name_len,2);
		if (atoi(name_len>max_name))
		{
			max_name=atoi(name_len);
			read(file_person,&name,20);
			read(file_person,&address,20);

			printf("Name \n",name);
			printf("Address \n",address);
			printf("Name length \n",name_len);
			printf("Address length \n",address_len);
			int location=lseek(file_person,0,SEEK_CUR);	
		}
		actual_pt = lseek(file_person,30,SEEK_CUR);
		
		
	}

close (file_person);
}

The first problem I see is the "if (atoi(name_len>max_name))" should be:

if (atoi(name_len)> max_name) 
...

The other potential problem is that you should read the entire person structure before evaluating.

If the items written to the disk are all the same size, you can read it as a struct and eliminate the use of lseek.

no they are not of the same size!

You can force the container holding the records to be a consistent size (based on max lengths).

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.