LOADING / READING FILE SEGMENTATION FAULT | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

LOADING / READING FILE SEGMENTATION FAULT

#include <stdio.h> #include <string.h> #include <ctype.h> struct songs { char title[30]; char artist[30]; char album[30]; }; struct playlist { char name[30]; int count; struct songs list[10]; }; int addPlaylist(struct playlist *music, int *index) { char tempName[20]; int temp = *index; if (temp == 10) { printf("\nCan't add another playlist.\nSystem is full !\n\n"); printf("-------------------------------\n"); return temp; } printf("Enter playlist name : "); scanf(" %[^\n]s", tempName); for(int i=0; i < temp; i++) { if (strcmp(music[i].name,tempName) == 0) { printf("\nPlaylist name already exists !\n"); printf("-------------------------------\n"); return temp; } } strcpy(music[temp].name, tempName); music[temp].count = 0; temp = temp + 1; printf("\nSuccessfully added playlist !\n"); printf("-------------------------------\n"); return temp; } void savePlaylists(struct playlist *music, int index) { FILE *fp = fopen("Playlist.txt", "w"); fprintf(fp, "Total Count of the Playlists %d", index); if (index != 0) { for(int i=0; i < index; i++) { fprintf(fp,"\nName of the Playlist %s\n", music[i].name); fprintf(fp,"\nNumber of Songs in Playlist %d\n", music[i].count); for(int j=0; j < music[i].count; j++) { fprintf(fp,"Title of Song %s\n", music[i].list[j].title); fprintf(fp,"Artist of Song %s\n", music[i].list[j].artist); fprintf(fp,"Album of Song %s\n", music[i].list[j].album); } } printf("Successfully saved data!\n"); fclose(fp); } else { printf("\nThere is no existing playlist to save !\n\n"); } } int loadPlaylists(struct playlist *music, int *index) { FILE *fp = fop

10th Jun 2022, 3:53 AM
Becky Forger
1 Answer
0
It is too hard to debug code pasted in the question description. And yours seem incomplete. Let's do it the right way: 1. Save your code in Code Playground as public 2. Edit your question 3. In the question description, include a link to your code in Code Playground - use "+" button 4. Also in the question description, paste the error exactly as is I also recomend removing all tags except the one with "c". Tags are for searching, so we better have few with more relevance.
1st Jul 2022, 1:37 AM
Emerson Prado
Emerson Prado - avatar