Okay, here's the code:
Code:
#include <stdio.h>
#include <string.h>
void add(char **data, int rows);
void del(char **data, int rows);
void display(char **data, int rows);
void save(char **data, int rows, FILE * fp);
void menu();
int main()
{
int x;
int q;
int quit;
char **data;
FILE * fp = fopen("list.dat", "r+");
if(fp == NULL)
{
fp = fopen("list.dat", "w+");
}
for(x=0;fgets(data[x], 1000, fp) != NULL;x++);
{
fgets(data[x], 1000, fp);
}
while(quit != 1){
menu();
scanf("%d", q);
switch(q)
{
case 1:{
add(data, x);
save(data, x, fp);
}
case 2:{
del(data, x);
save(data, x, fp);
}
case 3:{
display(data, x);
}
case 4:{
quit = 1;
}
}
}
fclose(fp);
return(0);
}
void add(char **data, int rows){
char *add;
printf("Please enter string to be added to the data file: ");
scanf("%s", *add);
strcat(add, "\n");
strcpy(data[rows+1], add);
}
void del(char **data, int rows){
int del;
display(data, rows);
printf("Please enter the number of the entry to be deleted: ");
scanf("%d", del);
data[del]= "";
}
void display(char **data, int rows){
int i;
for(i=0;i<=rows;i++){
printf("[%d]\t%s\n",i,data[i]);
}
}
void save(char **data, int rows, FILE * fp){
int i;
for(i=0;i<=rows; i++){
strcat(data[i], "\n");
fputs(data[i], fp);
}
}
void menu(){
printf("Welcome to the menu.\n");
printf("1) Add data to the file.\n");
printf("2) Remove data from the file.\n");
printf("3) Display contents of the file.\n");
printf("4) Exit the program.\n");
printf("Please insert the corresponding number for the function you wish to perform: ");
}
On compile it has no errors - but when I run it, it crashes before it displays the menu (menu(). I'm not sure why, do any of you have any ideas? Also, feel free to tell me where my code is inefficient