By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,599 Members | 1,678 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,599 IT Pros & Developers. It's quick & easy.

Binary adress book-minor syntax -- mitola

P: 17
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


struct oseba
{
char ime[20];
char priimek[15];
char ulica[50];
char mesto[50];
char tel_st[10];
short ucni_uspeh;
}pod;

void vnos(struct oseba pod,FILE *fd)
{
printf("Vnesi ime: "); scanf("%s",pod.ime);
printf("Vnesi priimek: "); scanf("%s",pod.priimek);
printf("Vnesi ucni uspeh: "); scanf("%d",&pod.ucni_uspeh);
printf("Vnesi ulico in st.: "); fgets(pod.ulica,100,fd); //scanf("%s",pod.ulica);
printf("Vnesi ime mesta: "); scanf("%s",pod.mesto);
printf("Vnesi tel st: "); scanf("%s",pod.tel_st);
fd=fopen("DIJAKI.BIN","a");
fwrite(&pod,sizeof(pod),1,fd);
fclose(fd);
}

void izpis(struct oseba pod,FILE *fd)
{
fd=fopen("DIJAKI.BIN","r");
fread(&pod,sizeof(pod),1,fd);
fclose(fd);
printf("%s %s\n", pod.ime, pod.priimek);
printf("Ucni uspeh: %d\n",pod.ucni_uspeh);
printf("Ulica: %s\n",pod.ulica);
printf("Mesto: %s\n",pod.mesto);
printf("Telefonska stevilka: %s\n",pod.tel_st);
}
int main(void)
{
FILE *fd;
int a;
int menu;
printf("------------------------------------------------------------------------------- \n");
printf(" IMENIK \n");
printf("------------------------------------------------------------------------------- \n");
printf("Vnesi izbiro:\n");
printf("1.)Vpis podatkov\n");
printf("2.)Izpis vseh podatkov\n");
printf("3.)Iskanje po imenu ali priimku\n");



scanf("%d",&menu);
switch(menu)
{
case 1: vnos(pod,fd);
break;
case 2: izpis(pod,fd);
break;
}

system("pause");
return 0;
}


heres the code :

void vnos(struct oseba pod,FILE *fd)
{
printf("Vnesi ime: "); scanf("%s",pod.ime);
printf("Vnesi priimek: "); scanf("%s",pod.priimek);
printf("Vnesi ucni uspeh: "); scanf("%d",&pod.ucni_uspeh);
printf("Vnesi ulico in st.: "); fgets(pod.ulica,100,fd);//here is the problem
//scanf("%s",pod.ulica);
printf("Vnesi ime mesta: "); scanf("%s",pod.mesto);
printf("Vnesi tel st: "); scanf("%s",pod.tel_st);
fd=fopen("DIJAKI.BIN","a");
fwrite(&pod,sizeof(pod),1,fd);
fclose(fd);
}

well i have problem when trying to scan a string longer then one word for example: railroad 25
with normal scan i can rea only first word and i dont know how to correct this mistake so im asking for help.
Nov 20 '07 #1
Share this Question
Share on Google+
3 Replies


P: 90
Use
Expand|Select|Wrap|Line Numbers
  1. gets();
  2.  
or

Expand|Select|Wrap|Line Numbers
  1. scanf("%[^\n]s",s);
  2.  
Nov 20 '07 #2

P: 17
void vnos(struct oseba pod,FILE *fd)
{
printf("Vnesi ime: "); scanf("%s",pod.ime);
printf("Vnesi priimek: "); scanf("%s",pod.priimek);
printf("Vnesi ucni uspeh: "); scanf("%d",&pod.ucni_uspeh);
printf("Vnesi ulico in st: ");scanf("%[^\n]s",pod.ulica);
printf("Vnesi ime mesta: "); scanf("%s",pod.mesto);
printf("Vnesi tel st: "); scanf("%s",pod.tel_st);
fd=fopen("DIJAKI.BIN","a");
fwrite(&pod,sizeof(pod),1,fd);
fclose(fd);
}

i tryed that what you said and it dont wrote any syntax but just automaticly skips the : scanf("%[^\n]s",pod.ulica);

so how can i fix that i'll be able to actually wrote an ino there
Nov 21 '07 #3

P: 90
use fflush(stdin)

Expand|Select|Wrap|Line Numbers
  1. void vnos(struct oseba pod,FILE *fd)
  2. {
  3.     printf("Vnesi ime: ");            scanf("%s",pod.ime);
  4.                 fflush(stdin);
  5.     printf("Vnesi priimek: ");        scanf("%s",pod.priimek);
  6.                 fflush(stdin);
  7.     printf("Vnesi ucni uspeh: ");    scanf("%d",&pod.ucni_uspeh);
  8.                 fflush(stdin);
  9.     printf("Vnesi ulico in st: ");scanf("%[^\n]s",pod.ulica);
  10.                 fflush(stdin);
  11.     printf("Vnesi ime mesta: ");    scanf("%s",pod.mesto);
  12.                 fflush(stdin);
  13.     printf("Vnesi tel st: ");        scanf("%s",pod.tel_st);
  14.                 fflush(stdin);
  15.     fd=fopen("DIJAKI.BIN","a");
  16.     fwrite(&pod,sizeof(pod),1,fd);
  17.     fclose(fd);
  18. }
  19.  
Nov 21 '07 #4

Post your reply

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