need some help in scanning files in database

This is a discussion on need some help in scanning files in database within the C Programming forums, part of the General Programming Boards category; ok i got this to work its a sample program in our module but i'm having a problem when i ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    8

    need some help in scanning files in database

    ok i got this to work its a sample program in our module but i'm having a problem when i want to seach for the student code it only seeks the file for the entry number not the code can someone help me or walk me through this!!

    i want to seek for the code not the entry number its like when i typed the code it would print the whole entry


    Code:
     #include<stdio.h>
    #include<string.h>
    
    typedef struct{
    	char studno[9];
    	int age;
    	char name[30];
    	}student;
    
    FILE *fil;
    void clrstr(char st[])
    {
    strcpy(st,"\0");
    }
    
    main()
    {
    student cs;
    int no,i;
    char ch;
    no=i=0;
    clrscr();
    fil=fopen("C:\\Stud.txt","w+b");
    puts("enter number of students to process"); scanf("%d",&no);
    for(i=1;i<=no;i++)
    	{
    		printf("this is student #%d\n",i);
    		clrstr(cs.studno);
    		puts("enter student number "); scanf("%s",&cs.studno);
    		clrstr(cs.name);
    		puts("enter student name "); scanf("%s",&cs.name);
    		puts("enter student age "); scanf("%d",&cs.age);
    		fwrite(&cs,sizeof(cs),1,fil);
    	}
    fclose(fil);
    
    /*reading the contents of file */
    
    clrscr();
    fil=fopen("C:\\stud.txt","r+b");
    fread(&cs, sizeof(cs), 1, fil);
    i=0;
    while (!feof(fil))
    	{
    		i++;
    		printf("%d. %s\t%s\t%d\n",i,cs.studno,cs.name,cs.age);
    		fread(&cs, sizeof(cs), 1,fil);
    	}
    fclose(fil);
    ch = getche();
    i=0;
    
    /*seeking a record*/
    
    fil = fopen ("C:\\stud.txt","rb+");
    puts("enter record number to seek : ");
    scanf("%d",&i);
    fseek(fil,(i-1)*sizeof(cs),0);
    fread(&cs,sizeof(cs),1,fil);
    printf("%d. %s\t%s\t%d\n",i,cs.studno,cs.name,cs.age);
    fclose(fil);
    ch=getche();
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by d4xyjen View Post
    i want to seek for the code not the entry number its like when i typed the code it would print the whole entry
    You cannot use fseek to do that. You will have to read thru the file and examine each line with strstr() or strcmp(), et al.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    8
    ok sir already tried doing strcmp(); but i can't make it work any suggestions about the code sir?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by d4xyjen View Post
    ok sir already tried doing strcmp(); but i can't make it work any suggestions about the code sir?
    strstr() may be better than strcmp() because strstr() will tell you if (for example), the pattern (aka. substring) "bc" occurs in the string "aabbcc" (it does). Whereas strcmp() will only tell you if "bc" is the same as "aabbcc", which it isn't.

    The trick to learning how to use a command you have never used before is: DON'T TRY TO APPLY IT DIRECTLY IN YOUR LATEST MAGNUM OPUS. Write as short a program as possible which "demonstrates" how the function works. This will help you to understand A) whether that command is going to work to do what you want, and if so B) how to accomplish that.

    If you have problems, you can then use this short experiment in a forum post, and it will be more easily understood and commented upon by others.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  2. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  4. connecting database to the interface
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-24-2002, 12:42 AM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 08:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21