C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 11th, 2012, 09:50 AM
Marrika Marrika is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 1 Marrika User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 44 sec
Reputation Power: 0
Fprintf and fscanf

Hello everyone! I'm a biginner in C programming and i have a problem with a program in my textbook.It's a phonebook but only the "enter" and "find" functions work.Can anyone tell me why the other two functions "save" and "load" doesn't work ( at least with me). Thanks.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char names[100][40];
char numbers[100][40];

int loc=0;

int menu(void);
void enter(void);
void find(void);
void save(void);
void load(void);


main()
{
 int choice;
  do{
	 choice=menu();
	 switch(choice){
		case 1: enter();
			break;
		case 2: find();
			break;
		case 3: save();
			break;
		case 4: load();
			break;

	  }
}while(choice!=5);

return 0;
}

int menu(void)
{

int i;
char str[80];

printf("1.Enter the names and numbers.\n");
printf("2.Find numbers.\n");
printf("3.Save directory to disk.\n");
printf("4.Load directory from disk.\n");
printf("5.Quit.\n");

do{
	printf("Enter your choice: ");
	gets(str);
	i=atoi(str);
	printf("\n");
 }while(i<1 || i>5);
	return i;
}

void enter(void)
{

  for(;loc<100;loc++){
	 if(loc<100){
		printf("Enter name and phone number:\n");
		gets(names[loc]);
		if(!*names[loc]) break;
		gets(numbers[loc]);
	  }
	}
 }

 void find(void)
 {

	char name[80];
	int i;

	printf("Enter name:");
	gets(name);

	for(i=0;i<100;i++)
		if(!strcmp(name,names[i]))
			printf(" %s %s\n",names[i],numbers[i]);
	}

	void load(void)
	{

	  FILE *fp;


	  if((fp=fopen("phone.txt","r"))==NULL){
		 printf("Cannot open file.\n");
		 exit(1);
	 }

	 loc=0;
	 while(!feof(fp)){
		fscanf(fp,"%s %s",names[loc],numbers[loc]);
		loc++;
	 }
	 fclose(fp);
 }

	 void save(void)
	 {
		FILE *fp;
		int i;

		if((fp=fopen("phone","w"))==NULL){
			printf("Cannot open file.\n");
			exit(1);
	 }

	 for(i=0;i<loc;i++){
		fprintf(fp, "%s %s", names[i], numbers[i]);
	  }
	  fclose(fp);
	 }

Reply With Quote
  #2  
Old September 11th, 2012, 10:20 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
When you save two people to the phone book, it gets saved as
Code:
PERSONONE 111PERSONTWO 222

When you try to read that the program will not know where one person ends and the next one starts.

Hint: when writing to the file, also write an ENTER ('\n') to separate people.
HINT2: when reading from the file, use fgets() rather than fscanf(): fgets makes it very much easier to deal with spaces.

Reply With Quote
  #3  
Old September 15th, 2012, 01:33 PM
rainbowrain rainbowrain is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 rainbowrain User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 13 sec
Reputation Power: 0
Thumbs up

Also, fp=fopen("phone","w"))==NULL ...try to make it more simple (not too much =,== at the same line )!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Fprintf and fscanf

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap