FSCANF PROBLEM 
Author Message
 FSCANF PROBLEM

#include<conio.h>
#include<io.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char name[30];
char address[40];
char phone[15];
char namei[30];

FILE *f;

void checki();
void filec();

void main()
{
        clrscr();
        f=fopen("n.n","r");
        scanf("%s",namei);
        while(!feof(f))
        {
        fscanf(f,"%[^\t]\t%[^\t]\t%[^\n]",name,phone,address);
        if(!strcmp(namei,name))
        {
                printf("\nThe Details of %s ",name);
                printf("\n   Phone %s ",phone);
                printf("\n   Address %s ",address);
                printf("\n");
                getch();
        }
        else
        {
//      goto sc;
        }
        }

Quote:
}

this is my code

when i read a user input the program
should check for the input in the file and then display the necessary information
but the problem is i am able to read only the first line but not the other

the text file is: (n.n)
R.Rupesh    981781623    Flat no.4 Vijaya Residency
R.S.P       987329588    hwekjhklj sadhfkjasdf
R.Lalitha   324789766    23487253 3465jkhgg



Sat, 10 Jan 2004 09:33:44 GMT  
 FSCANF PROBLEM
(Note - an all-caps title means you will probably be filtered by some
readers)

    rR> #include<conio.h>

Non-standard header

    rR> #include<io.h>

Non-standard header

    rR> #include<stdio.h>
    rR> #include<stdlib.h>
    rR> #include<string.h>

    rR> char name[30];
    rR> char address[40];
    rR> char phone[15];
    rR> char namei[30];

    rR> FILE *f;

    rR> void checki();
    rR> void filec();

    rR> void main()

int main(void)

    rR> {
    rR>      clrscr();

Non-standard function

    rR>      f=fopen("n.n","r");

Better hope that file exists (Hint: check for failure)

    rR>      scanf("%s",namei);

scanf() is evil, read the FAQ for reasons

    rR>      while(!feof(f))

This construct is evil, read the FAQ.

    rR>      {
    rR>      fscanf(f,"%[^\t]\t%[^\t]\t%[^\n]",name,phone,address);

You may want to check for overflow (ie limit the length of the scans)

    rR>      if(!strcmp(namei,name))
    rR>      {
    rR>              printf("\nThe Details of %s ",name);
    rR>              printf("\n   Phone %s ",phone);
    rR>              printf("\n   Address %s ",address);
    rR>              printf("\n");
    rR>              getch();

Non-standard function

    rR>      }
    rR>      else
    rR>      {
    rR> //   goto sc;

Questionable comment, commenting out a questionable statement.  In
general, it is better to use /* */ comments in C, as they are not
affected by trailing backslashes

    rR>      }
    rR>      }
    rR> }

    rR> this is my code

    rR> when i read a user input the program should check for the
    rR> input in the file and then display the necessary information
    rR> but the problem is i am able to read only the first line but
    rR> not the other

Fix the problems I have pointed out.  Then read the parts of the FAQ
that I didn't point out.  You may be surprised at what errors show
themselves.

    rR> the text file is: (n.n)
    rR> R.Rupesh    981781623    Flat no.4 Vijaya Residency
    rR> R.S.P       987329588    hwekjhklj sadhfkjasdf
    rR> R.Lalitha   324789766    23487253 3465jkhgg

Cheers,
Kevin

--
He that breaks a thing to find out what it is has left the path of
wisdom                                         -- Gandalf the Grey



Sat, 10 Jan 2004 10:34:06 GMT  
 FSCANF PROBLEM

Quote:

> #include<conio.h>
> #include<io.h>
> #include<stdio.h>
> #include<stdlib.h>
> #include<string.h>

> char name[30];
> char address[40];
> char phone[15];
> char namei[30];

> FILE *f;

> void checki();
> void filec();

> void main()
> {
>         clrscr();
>         f=fopen("n.n","r");
>         scanf("%s",namei);
>         while(!feof(f))
>         {
>         fscanf(f,"%[^\t]\t%[^\t]\t%[^\n]",name,phone,address);
>         if(!strcmp(namei,name))
>         {
>                 printf("\nThe Details of %s ",name);
>                 printf("\n   Phone %s ",phone);
>                 printf("\n   Address %s ",address);
>                 printf("\n");
>                 getch();
>         }
>         else
>         {
> //      goto sc;
>         }
>         }
> }

> this is my code

> when i read a user input the program
> should check for the input in the file and then display the necessary information
> but the problem is i am able to read only the first line but not the other

Read FAQ Question 12.2

One suggestion, use fgets to get the line from the file and use sscanf to parse the
line.

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

#define FILEN "n.n"

char *getname(char *b, size_t size);

int main(void) {
   char name[30], address[40], phone[15], namei[30], buf[80];
   FILE *f;

   if((f=fopen(FILEN,"r")) == NULL) {
      fprintf(stderr,"Failure to open file %s: \n",FILEN);
      exit(EXIT_FAILURE);
      }
   printf("To Search, enter name: ");
   fflush(stdout);
   getname(namei,sizeof buf);

   while(fgets(buf,sizeof buf, f)!= NULL) {
      if(3 != sscanf(buf,"%[^\t]\t%[^\t]\t%[^\n]",name,phone,address)) {
         fprintf(stderr,"error in scanning line");
         exit(EXIT_FAILURE);
         }
      if(!strcmp(namei,name)) {
         printf("\nThe Details of %s ",name);
         printf("\n   Phone %s ",phone);
         printf("\n   Address %s ",address);
         printf("\n");
         }
      }
   return 0;
   }

char *getname(char *b, size_t size) {
   char *s;
   int c;

   if(fgets(b,size,stdin) == NULL) return NULL;
   if((s = strchr(b,'\n')) != NULL) *s = '\0';
   else while((c = getchar()) != '\n' || c != EOF);
   return b;
   }

--
Al Bowers
Tampa, FL  USA
comp.lang.c faq: http://www.eskimo.com/~scs/C-faq/top.html

http://www.geocities.com/abowers822/



Sun, 11 Jan 2004 00:01:50 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. fscanf: problem skipping characters

2. fscanf problem

3. fscanf: problem skipping characters

4. fscanf problem

5. This should be easy! fscanf problems with EOF

6. fscanf problem - core dump

7. fscanf problem

8. fscanf problem

9. fscanf problem

10. fscanf problem

11. fscanf problems

12. fscanf problem...

 

 
Powered by phpBB® Forum Software