473,499 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading file into array problems

4 New Member
I'm having trouble with the following program. i want it to have the options to save a list of data entered by the user and display it the next time they run it. Could someone please either show me a sample program or tell me where i'm going wrong.

i use DEV C++
Written in C

[code]
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char friends_name[20];
int number_of_friends_i;
int found_i;

/* Prototype */
int f_menu();
void f_recall();
void f_enter_names_ages();
void f_display_list();
void f_extend_list();
void f_delete_name();
void f_search();
void f_save();


typedef struct
{
int friends_age_i;
char friends_name[20];
}FRIENDS;

FRIENDS friends_details[20]; /* Stes up 20 friends in an array */


int main(void)
{
int Choice_d = 0;
printf ("This is a program to store and display a list of names and ages\n");
printf ("You can enter upto 20 names and ages, add and delete from the list, display the list and search for a name on the list\n\n");

do
{
Choice_d = f_menu ();
switch (Choice_d)
{
case 1: f_recall();
break;

case 2: f_enter_names_ages();
break;

case 3: f_display_list();
break;

case 4: f_extend_list();
break;

case 5: f_delete_name();
break;

case 6: f_search();
break;

case 7: f_save();
break;

case 8: printf ("Bye Bye\n\n");
break;
}
}
while (Choice_d != 8);
system ("Pause");
return 0;
}

/* ************************************************** ******************* */
int f_menu ()
{
int Selected_d = 0;


do
{

printf ("(1) Load saved list\n(2) Enter names & ages\n(3) Display list\n(4) Extend list\n(5) Delete from list\n(6) Search for name\n(7) Save list\n(8) Quit\n\n");

printf ("Enter a number that corresponds to your choice > ");
scanf ("%d", &Selected_d);
printf("\n");

if (( Selected_d < 1 ) || ( Selected_d > 8))
{
printf("You have entered an invalid choice. Please try again\n\n\n");
}
}
while (( Selected_d < 1) || ( Selected_d > 8));

return Selected_d;
}

/* ************************************************** ************************** */
void f_recall()
{
FILE *fp;
int i;

/* Set up blank array */
for (i = 0; i < 20; i++ )
{
strcpy ("", friends_details[i].friends_name);
friends_details[i].friends_age_i = 0;
}
number_of_friends_i = 0;

if ((fp = fopen ("friends_age_i", "rb" )) == NULL)
{
printf("No list saved to file\n Creating new file\n");
}
else
{
/* Read existing data from file */
fread ( &friends_details[number_of_friends_i], sizeof (FRIENDS), 1, fp);

while ( !(feof (fp)))
{
number_of_friends_i ++;
fread ( &friends_details[number_of_friends_i], sizeof (FRIENDS), 1, fp);
}
printf("There are %d friends already stored\n", number_of_friends_i);
fclose(fp);
}
}


/* ************************************************** ************************** */
void f_enter_names_ages()
{
int count_i = 0;
printf("Enter Names and Ages of up to 20 friends\n");
printf("Enter number of friends you wish to enter > ");
scanf("%d", &number_of_friends_i);
fflush(stdin);
printf("\n");


do
{
printf("Enter friends name > ");
scanf("%s", &friends_details[count_i].friends_name);
printf("enter %s's age > ", friends_details[count_i].friends_name);
scanf("%d", &friends_details[count_i].friends_age_i );
fflush(stdin);
count_i ++;
} while ((count_i <= 19) && (count_i < number_of_friends_i));
printf("\n\n");

}


/* ************************************************** ************************** */
void f_display_list() /* Display list in alphabetical order */
{
int count_i = 0;
int i;
int sorted_i; /* Flag that tells if array is already sorted */

FRIENDS temp;

if(number_of_friends_i < 1)
{
printf("List empty\n\n");
}
else
{

do /* Sort into alphabetical order */
{
sorted_i = 0;

for ( i = 0; i < number_of_friends_i - 1; i++)
{
if(strcmp (friends_details[i].friends_name,friends_details[i + 1].friends_name) > 0 )
{
temp = friends_details[i];
friends_details[i] = friends_details[i + 1];
friends_details[i + 1] = temp;
sorted_i = 1;
}
}
}while (sorted_i == 1);

printf("Friend\n"); /* Display sorted list */
do
{
printf("%d: %s age %d\n", count_i + 1, friends_details[count_i].friends_name, friends_details[count_i].friends_age_i );
count_i ++;
}
while (count_i < number_of_friends_i);
printf("\n\n");
}
}

/* ************************************************** ************************** */
void f_extend_list() /* Add a friend to the list */
{
int i = number_of_friends_i;

if (number_of_friends_i < 19) /* Max 20 names on list. Can't add another if list is full */
{
printf("Enter friends name > ");
scanf("%s", &friends_details[i].friends_name);
printf("enter %s's age > ", friends_details[i].friends_name);
scanf("%d", &friends_details[i].friends_age_i );
fflush(stdin);

printf("\n\n");
number_of_friends_i = number_of_friends_i + 1; /* List increased by 1 */
}

else
{
printf("Sorry, you can't enter any more to the list");
}
}

/* ************************************************** ************************** */
void f_delete_name() /* Remove name from list */
{
char search_name [20];
int i;


printf("Enter the name of the friend you want to delete > ");
scanf("%s", &search_name);
fflush(0);
printf("\n");


found_i = 0;
i = 0;

while (( i < number_of_friends_i ) && ( found_i == 0))
{

if (strcmp (friends_details[i].friends_name, search_name) == 0)
{
found_i = 1;
}
i++;
}

if (found_i == 1)
{ /* Copy last entry on list onto name to be deleted. Reduce list by 1 */
i --;
printf("%s age %d has been deleted\n\n", friends_details[i].friends_name, friends_details[i].friends_age_i);
strcpy(friends_details[i].friends_name, friends_details[number_of_friends_i - 1].friends_name);
friends_details[i].friends_age_i = friends_details[number_of_friends_i - 1].friends_age_i;
number_of_friends_i = number_of_friends_i - 1;

}
else
{
printf("name not found\n\n");
}
}

/* ************************************************** ************************** */
void f_search() /* Find name on list */
{
char search_name [20];
int i;

printf("Enter the name of the friend you want to find > ");
scanf("%s", &search_name);
fflush(0);
printf("\n");


found_i = 0;
i = 0;

while (( i < number_of_friends_i ) && ( found_i == 0))
{

if (strcmp (friends_details[i].friends_name, search_name) == 0)
{
found_i = 1;
}
i++;
}

if (found_i == 1)
{
i --;
printf("%s age %d has been found\n\n", friends_details[i].friends_name, friends_details[i].friends_age_i);
}
else
{
printf("name not found\n\n");
}
}

/* ************************************************** ************************** */
void f_save()
{
FILE *fp;
int i;

if ((fp = fopen ( "friends_age_i", "wb" )) == NULL )
{
printf("Cannot open file\n");
return;
}

for (i = 0; i < number_of_friends_i; i ++)
{
fwrite (&friends_details[i], sizeof (FRIENDS), 1, fp);
}

fclose (fp);
}

[code]

Thanks
Dec 7 '06 #1
6 3748
DeMan
1,806 Top Contributor
That's very comprehensive code (use [/code] to finish your code segment. )

What is the problem, that is how is this code failing, what unusual behaviour is it demonstrating, or what output is it giving (compared to what you expect).

It is great that you submit what you have done so far, but could you please explain what is the problem you are experiencing (because it is difficult to wade through code and highlight a problem).

Please Reply.....
Dec 7 '06 #2
svsandeep
15 New Member
[quote=chris237]I'm having trouble with the following program. i want it to have the options to save a list of data entered by the user and display it the next time they run it. Could someone please either show me a sample program or tell me where i'm going wrong.

i use DEV C++
Written in C

Expand|Select|Wrap|Line Numbers
  1.  
Hi,

You are using f_enter_names_ages function to read your frnds age and name. After reading why are you not calling your f_save function which will write the details in to a file. And also what abt number_of_friends_i variable why are you not incrementing it when ever you are reading a new friend. Do these two changes i am sure your code will start working atleast some functionality of what you have expected to do. I got more point but not now later....

regards,
ShaggY@FtF
Dec 7 '06 #3
chris237
4 New Member
When i run the program there is a list of options.
1)Load saved list
2)Enter names and ages
3)Display list
4)Extend list
5)Delete from list
6)Save list
7)Quit

Options 2-5 work.
Option 1 crashes the program
Option 6 i have no way of checking as option 1 won't work

I think the problem is within
Expand|Select|Wrap|Line Numbers
  1.  Void f_recall 
(Option 1) function. I don't want to integrate the save function into the
Expand|Select|Wrap|Line Numbers
  1.  f_enter_names_ages 
as this would remove the option of saving by choice.

If you could help me on this it would be greatly appreciated.

Thanks for your reply

Chris

That's very comprehensive code (use [/code] to finish your code segment. )

What is the problem, that is how is this code failing, what unusual behaviour is it demonstrating, or what output is it giving (compared to what you expect).

It is great that you submit what you have done so far, but could you please explain what is the problem you are experiencing (because it is difficult to wade through code and highlight a problem).

Please Reply.....
Dec 8 '06 #4
chris237
4 New Member
That's very comprehensive code (use [/code] to finish your code segment. )

What is the problem, that is how is this code failing, what unusual behaviour is it demonstrating, or what output is it giving (compared to what you expect).

It is great that you submit what you have done so far, but could you please explain what is the problem you are experiencing (because it is difficult to wade through code and highlight a problem).

Please Reply.....
Do i have to save the list as it is entered or can i have an option to save it at the end if the user wants?

Please Reply

Thanks
Dec 9 '06 #5
willakawill
1,646 Top Contributor
Do i have to save the list as it is entered or can i have an option to save it at the end if the user wants?

Please Reply

Thanks
Hi there are 2 things about f_recall that may be of interest to you.
firstly strcpy works by putting the destination string first and the source string second. You have it the other way around.
secondly you are miscounting the number of 'friends' stored on file by 1, and you don't have a check to ensure that you are not reading in more than 20 friends.
Dec 9 '06 #6
DeMan
1,806 Top Contributor
Do i have to save the list as it is entered or can i have an option to save it at the end if the user wants?
That depends on what you mean by save. You store the information in the list (in memory) while you use it. If you mean saving to disk, then there is no harm in waiting for the list to be fully populated before storing it)
Dec 10 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
12786
by: Erpman | last post by:
I am trying to access the data with in a wav file. I am testing with very small files in order to keep the code simple to start with. Basically, im writing the entire wav file to a byte using a...
29
10371
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and...
10
2750
by: nuke1872 | last post by:
Hello guys, I have a file names network.txt which contains a matrix. I want to read this matrix as store it as an array. I am new to stuff like these...can anybody help me out !! Thanks nuke
7
2924
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
6
5237
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
1
2498
by: paulh | last post by:
Hi All, I am having problems reading from a file into an array of structures. I have defined an array of 100 structures as shown below. struct address_t {
2
2365
by: totoro2468 | last post by:
Here is my code and output. Why is it writing to the array incorrectly, when I rewinded the file to the beginning? CODE: void ReadString (char *filename, int *lengthPtr) { FILE *ifp; char...
11
3556
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
2
1457
by: ajay0419 | last post by:
Hi: I am trying to read from a file, which stores answers for a particular question(The answers are stop listed ). Each answer in the file is enclosed in opening and closing braces. Here is the...
3
1975
by: godblessingme | last post by:
Hi, I'm getting some problems when using fscanf to read a file. This is a piece of the program code: #include <stdlib.h> #include <stdio.h> int main() {
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.