Database fscanf

This is a discussion on Database fscanf within the C Programming forums, part of the General Programming Boards category; Hello Guys, I just wanted to ask on how will I be able to print out the list of names ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    34

    Database fscanf

    Hello Guys, I just wanted to ask on how will I be able to print out the list of names in a data.txt file that I'm using..

    My data.txt contains the Following
    <Student Number> <First Name> <Last Name> <Age>
    Code:
    1 Chan Test 10
    2 Test Chan 20
    3 Chan Chan 30
    4 Test Test 40
    But the only one getting printed is
    Code:
    Chan Test
    What I want to happen is like this
    Code:
    Chan Test
    Test Chan
    Chan Chan
    Test Test
    Code:
    #include <conio.h>
    #include <stdio.h>
    main()
    {
          FILE *pFile;
          char name[21],lname[21],answer;
          int age,num,i;
          pFile=fopen("data.txt","r");
          fscanf(pFile,"%d",&num);
          printf("List of names in the database");
          for(i=0;i<num;i++)
          {
          fscanf(pFile,"%s %s",&name,&lname);
          printf("\nName: %s %s",&name,&lname);
          }
          if(pFile!=NULL)
          {
          repeat:
          printf("\n\nStudent Number: ");
          scanf("%d",&num);
          printf("First Name: ");
          scanf("%s",&name);
          printf("Last Name: ");
          scanf("%s",&lname);
          printf("Age: ");
          scanf("%d",&age);
          fprintf(pFile,"%d %s %s %d\n",num,name,lname,age);
          {
          fclose(pFile);
          }
          printf("Are there any more students? [Y/N]");
          answer=getche();
          if(answer=='Y'||answer=='y')
          {
                                      goto repeat;
          }
          else
          {
              printf("\nClosing Program");
              }
          }
          else
          {
          printf("Could not open the file.\n");
          }
    getch();
    }
    Thank you in advance

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Where is pFile referenced in your program? ???
    Let's see:

    line 5: ok, declare the variable
    line 8: ok (kinda -- you missed testing for errors), open the file for reading
    line 9: ok (kinda -- you missed testing for errors), read an integer (the 1)
    line 13: ok (kinda -- you missed testing for errors), read two words (the "Chan" and "Test")
    line 16: hmmm??? late test for errors??? this should always be true here
    line 27: OOPS -- writing to a file opened for reading.
    line 29: ok, closing the file ... but you didn't read anything after the 2nd word.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    34
    Could you please tell me what should I do?
    I really dont get it

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    You want to have a loop where you read the data from the file.
    basically:
    Code:
    /* pseudo code */
    fopen();
    some_kind_of_loop {
        /* readdata */
        /* printdata */
    }
    fclose();
    Happy Coding :-)

  5. #5
    Registered User
    Join Date
    Nov 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    34
    I really dont get it LOL Sorry Im just new in making database in C

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're not making a database, yet. You're just reading data from a file. Line #9, you are asking for input, but you haven't told the user WHAT the input should be.

    First you always have to print out what you want the user to input, THEN you have the code to get the input.

    IMO, you should set your program up to read ALL the students in the data file, and not ask the user about it - because user's are goofy, and they will input the wrong number, more often than the right number.

    Click on the C Tutorial tab near the top middle of this forum, and work through it. It shows you how to do this, and lots of other good stuff for working with files. This is a LOT of the stuff you WILL need.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    34
    But uhmm could you help me fix my script please?

  8. #8
    Registered User
    Join Date
    Nov 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    34
    I just really need this for my project

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm trying!

    You are mixing input with output, and getting the wires crossed on them. That's a big error for your program. Separate them - a file opened for reading, can't be written to, and if you open the file for reading and writing, you will surely wreck your data (right now). Open the file for reading, read in all the students data into your array, then close it.

    If you want to add students to it, open the data file in append mode, and write out your data. That way the new data will be put at the end of the file, and not overwrite your older data.

    If you go through the C tutorial I mentioned, you will have the basics you need to do this. Right now, you don't know enough basics to do it, and only you can change that. I don't have time to write your projects code, which is what would be needed, right now.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    34
    :IhavenoideawhatImdoing:
    I really dont know what's happening with my program

  11. #11
    Registered User
    Join Date
    Nov 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    34
    Can someone help me please?
    Sorry for double posting

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by chanhalzkie View Post
    Can someone help me please?
    Sorry for double posting
    You are going to have to get more basic C skills under your belt, before you can hope to complete this program.

    Go through the C tutorial on the tab, at the top of this forum (near the middle). It will answer many of your most basic C questions.

    Go! Then come back and we'll help you. Jump ahead to the parts dealing with file handling, and how to read in all the data, and how to write it out, especially.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    34
    Here's my new code..

    Here's what is inside my .txt file
    Code:
    <name><age><gender>
    My Name,18,Male
    But the output is becoming like this
    Code:
    Name: My
    Age: Name
    Gender: Male
    Name: 18
    Age: Name
    Gender: 18
    What I wanted to happen is like this
    Code:
    Name: My Name
    Age: 18
    Gender: Male
    Just a lil' more help I'll be finishing my project

    Code:
    #include <stdio.h>
    #include <conio.h>
    main()
    {
          FILE *pFile;
          char name[100],gender[10],answer,age[10];
          pFile=fopen("data.txt","a");
          if(pFile!=NULL)
          {
          Repeat:
          printf("\n\nStudent Name: ");
          gets(name);
          printf("Age: ");
          gets(age);
          printf("Gender: ");
          gets(gender);
          fprintf(pFile,"%s %s %s\n",name,age,gender);
          {
          fclose(pFile);
          }
          printf("Are there any more students? [Y/N]: ");
          answer=getche();
          if(answer=='Y'||answer=='y')
          {
          goto Repeat;
          }
          else
          {
          printf("\nClosing Program");
          }
          }
          else
          {
          printf("Could not open the file.\n");
          }
    
    
          pFile=fopen("data.txt","r+");
          if(pFile!=NULL)
          {
          printf("\n\nList of names in the database");
          while(fscanf(pFile,"%s %s %s",name,age,gender)!=EOF){
          printf("\nName: %s",name);
          printf("\nAge: %s",age);
          printf("\nGender: %s",gender);
          }
          fclose(pFile);
          }
          else
          {
          printf("Could not open the file.\n");
          }
    getch();
    }

  14. #14
    Registered User
    Join Date
    Nov 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    34
    Bump

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm disappointed that you didn't go through the C Tutorial, as requested. I wrote this for you, anyway. Good luck with your project.

    Code:
    /* The names.txt file is:
    Abe Alfa,10,Male
    Bella Bravo,15,Female
    Charles Charlie,18,Male
    Donna Delta,22,Female
    */
    
    #include <stdio.h>
    
    typedef struct {      //declare a structure
       char fname[30];    //for each student record
       char lname[30];
       int age;
       char sex;
    }students;            
    
    int main(void) {
       FILE *fpIn;
       int i,studnumber;
       char buffer[80];
       students stud[4];   //make an array of student records
    
       fpIn=fopen("names.txt","r");
       if(!fpIn) {
          printf("Error opening file!\n");
          return 1;
       }
       i=0;
       while(fgets(buffer, sizeof(buffer), fpIn)) { 
          sscanf(buffer, "%s %[^,],%d,%c",stud[i].fname,stud[i].lname,&stud[i].age,&stud[i].sex);
          printf("Name: %s %s \nAge: %d \nSex: %c\n",stud[i].fname,stud[i].lname,stud[i].age,stud[i].sex);
          ++i;
       }
       studnumber=i;
    
       fclose(fpIn);
       printf("\nnumber of students: %d \n",studnumber);
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database to Database Transfer?
    By draggy in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2007, 10:50 AM
  2. fscanf() help!!
    By hykyit in forum C Programming
    Replies: 4
    Last Post: 05-13-2005, 07:35 AM
  3. fscanf
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 04-16-2002, 01:29 PM
  4. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 02:22 PM
  5. Replies: 1
    Last Post: 10-09-2001, 11:20 PM

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