Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Problem in c programming?
Given below is my program
It is a program to create a linked list.
The contents of record.h is given in double quotes
"
/*record.h*/
#ifndef __RECORD_H__
#define __RECORD_H__
struct _Record;
typedef struct _Record{
char *name;
unsigned int age;
struct _Record *next;
} Record;
#endif
"
#include <stdio.h>
#include <stdlib.h>
#include "record.h"
#include <string.h>
Record* createLinkedList(char *fileName);
void deleteRecords(Record **listHead, char *fileName);
void insertRecords(Record **listHead, char *fileName);
void printLinkedList(Record *listHead, char *fileName); /*prints linked list to file*/
void sortLinkedList(Record **listHead);
int main() {
Record *head;
head = createLinkedList("allRecords.txt");
printf("%s",head->name);
printf("\n");
printLinkedList(head,"readRecords.txt");
sortLinkedList(&head);
printLinkedList(head,"afterSort.txt");
deleteRecords(&head,"deleteList.txt");
printLinkedList(head,"afterDelete.txt");
insertRecords(&head,"insertList.txt");
printLinkedList(head,"afterInsert.txt");
return 0;
}
void printLinkedList(Record *listHead, char *fileName) {
printf("Printing linked list to file %s\n",fileName);
FILE *fp;
char s[40];
strcpy(s,fileName);
fp=fopen(s,"r");
{
}
}
void sortLinkedList(Record **listHead) {
/*Sort the linked list by Age (ascending order)*/
}
Record* createLinkedList(char *fileName) {
Record *localHead;
Record ghart;
localHead=&ghart;
printf("Loading records from file %s \n",fileName);
FILE *fp;
char y[40];
int a;
Record *temp;
temp=&ghart;
strcpy(s,fileName);
fp=fopen(s,"r");
localHead->next=(struct _Record*)malloc(sizeof(struct _Record));
localHead=localHead->next;
while(!(feof(fp)))
{
fscanf(fp,"%s",y);
fscanf(fp,"%d",&a);
localHead->name=y;
localHead->age=a;
printf("%d %s",localHead->age,localHead->name);
localHead->next=(struct _Record*)malloc(sizeof(struct _Record));
localHead=localHead->next;
}
localHead->next=NULL;
fclose(fp);
while(temp!=NULL)
{
printf("%s %d",temp->name,temp->age);
temp=temp->next;
}
return temp;
}
void deleteRecords(Record **listHead, char *fileName) {
printf("Deleting records from file %s \n",fileName);
}
void insertRecords(Record **listHead, char *fileName) {
printf("Inserting records from file %s \n",fileName);
}
I get a seg mentation fault in the function where a linked list is to be created.How do i rectify it so that a proper variable of type Record (containing the linked list is returned).
The file that is passed to the createlinked list function is of the form
Name
Age
Name
Age
and so on.
2 Answers
- ?Lv 78 years agoFavorite Answer
Re _Record:
name needs to allocate some memory. Either you need to allocate name at run time, or before. Here I wasted 32 bytes for the name allocation. The alternative is that you i.e. had an array of c strings and point name to this existing memory location.
Otherwise assignment to name would remain local
This piece of code:
you are declaring local variables y and a and fill them from the file. Then over and over again you point the variable pointing to char (*name) to the address of y. Your printf statement would output the last content of y.
As the variable age has allocation for sizeof(int), this variable changes according to file content.
After the function createLinkedList is exited, the variable y ceases existing. location to the address stored will reflect "undefined" content. I hope the disaster of your function becomes a bit clearer. I would help you discussing this more in-depth, in case you e-mail your questions.
Have fun.
while(!(feof(fp)))
{
fscanf(fp,"%s",y);
fscanf(fp,"%d",&a);
localHead->name=y;
localHead->age=a;
printf("%d %s",localHead->age,localHead->name);
localHead->next=(struct _Record*)malloc(sizeof(struct _Record));
localHead=localHead->next;
}
------------------------
Start of corrected code
------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _Record{
char name[32];
unsigned int age;
struct _Record *next;
} Record;
Record* createLinkedList(const char *fileName);
void deleteRecords(Record **listHead, const char *fileName);
void insertRecords(Record **listHead, const char *fileName);
void printLinkedList(Record *listHead, const char *fileName); /*prints linked list to file*/
void sortLinkedList(Record **listHead);
int main()
{
Record *head = createLinkedList("allRecords.txt");
printf("%s\n",head->name);
printLinkedList(head, "readRecords.txt");
sortLinkedList(&head);
printLinkedList(head, "afterSort.txt");
deleteRecords(&head, "deleteList.txt");
printLinkedList(head, "afterDelete.txt");
insertRecords(&head, "insertList.txt");
printLinkedList(head, "afterInsert.txt");
return 0;
}
void printLinkedList(Record *listHead, const char *fileName)
{
FILE *fp;
char s[40];
printf("Printing linked list to file %s\n", fileName);
strcpy(s,fileName);
//fp=fopen(s,"r");
}
void sortLinkedList(Record **listHead)
{
/*Sort the linked list by Age (ascending order)*/
}
Record* createLinkedList(const char *fileName)
{
Record *localHead, *temp;
FILE *fp;
printf("Loading records from file %s \n",fileName);
localHead = malloc(sizeof(struct _Record));
localHead->next = NULL;
temp = localHead;
fp = fopen(fileName,"rt");
while(!feof(fp)) {
if (fscanf(fp, "%s\n", temp->name) != 1) {
break;
}
if (fscanf(fp, "%d\n", &temp->age) != 1) {
break;
}
temp->next = NULL;
if (!feof(fp)) {
temp->next = malloc(sizeof(struct _Record));
temp = temp ->next;
}
}
fclose(fp);
temp = localHead;
while(temp) {
printf("[%s %d]\n", temp->name, temp->age);
temp = temp->next;
}
return localHead;
}
void deleteRecords(Record **listHead, const char *fileName)
{
printf("Deleting records from file %s \n",fileName);
}
void insertRecords(Record **listHead, const char *fileName)
{
printf("Inserting records from file %s \n",fileName);
}
- Anonymous8 years ago
See the troubleshooting guide here: http://ubuntuforums.org/showthread.php?t=691451