Thread: File input & output (Full coding)

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    2

    Question File input & output (Full coding)

    /*
    This program is used to input file, search particular record and display all the record. I'm using TcLite to compile.
    */

    /*PROBLEM...
    1) The data type i used to search for a particular record is char, everytime i do a searching, it cannot found any record. I try to change the date type to int, then the program working correctly.

    2) My Display function is also not working.

    Can some one compile my program and help to solve my problem?
    */


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

    #define false 0
    #define true 1

    void add();
    void display();
    void search();

    FILE *MyFile;
    int bytesWritten;
    int bytesRead;
    char found;

    char wanted[10]; /*If i using int, the program is working correctly. */

    struct WizardStructure
    {
    char Name[30], Magic[100];
    char WizardID[10]; /*If using int, program working correctly*/
    }Wizard;

    ///////////////////////////////////////////////////////////////////////////////////

    int main()
    {
    char i, choice;

    do{
    clrscr();
    printf("\n Main Menu");
    printf("\n 1. Add new Wizard");
    printf("\n 2. Display all Wizard's name and ID");
    printf("\n 3. Search Wizard record");
    printf("\n 4. Exit");
    printf("\n\n Please enter your choice : ");
    scanf(" %[^\n]", &choice);
    switch (choice)
    {
    case '1' : add(); break;

    case '2' : display(); break;

    case '3' : search(); break;

    default: printf("\n Exit the program ");
    }

    printf("\n Do you want to continue? Press [ Y ] for YES... else NO : ");
    scanf(" %c", &i);

    }while(i=='y'|| i=='Y');

    getch();
    return 0;
    }


    /* To add record *************************************/

    void add()
    {
    MyFile = fopen("Wizard.dat", "r+b");

    if (!MyFile)
    MyFile = fopen("Wizard.dat", "w+b");

    printf("\n Enter Wizard name : ");
    scanf(" %[^\n]", &Wizard.Name);
    printf("\n Enter Wizard ID : ");
    scanf(" %[^\n]", &Wizard.WizardID); /*if using %d*/
    printf("\n Enter MAGIC POWER of the Wizard : ");
    scanf(" %[^\n]", &Wizard.Magic);

    fseek (MyFile, 0, SEEK_END);

    bytesWritten = fwrite((void*)&Wizard, sizeof(Wizard), 1, MyFile);
    fclose (MyFile);
    }


    /*To display all the records *****************************/

    void display()
    {
    fprintf(stdout, "\n Wizard Name\t\t Wizard ID\t\t Wizard Magic");
    fprintf(stdout, "\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~");

    FILE *MyFile;
    MyFile = fopen ("Wizard", "r+b");
    if (fseek (MyFile, 0, SEEK_SET) == 0 );
    {
    bytesRead = fread ((void*) & Wizard, 1, sizeof(Wizard), MyFile);
    while ( !feof (MyFile))
    {
    fprintf(stdout, "\n %s\t\t\t %s\t\t\t %s", Wizard.Name, Wizard.WizardID, Wizard.Magic);
    bytesRead = fread ((void*) & Wizard, 1, sizeof(Wizard), MyFile);
    }
    }
    }


    /* To search for a particular record ***********************/

    void search()
    {
    MyFile = fopen ("Wizard.dat", "r+b");

    printf("\n Enter Wizard's ID : ");
    scanf(" %[^\n]", &wanted); /*if using int*/

    fseek (MyFile, 0, SEEK_SET);

    found = false;

    while (( !feof (MyFile)) && (found != true))
    {
    bytesRead = fread ((void*) & MyFile, sizeof (MyFile), 1, MyFile);
    if (bytesRead > 0)
    if ("Wanted" == "Wizard.WizardID")/*if without" "*/
    found = true;
    }

    if (found == true)
    {
    printf("\n WizardID : %s", Wizard.WizardID);
    printf("\n Name : %s", Wizard.Name);
    printf("\n Wizard's Magic : %s", Wizard.Magic);
    }
    else
    printf("\n WizardID is not found");
    fclose(MyFile);
    }

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Hm,
    > #define false 0
    > #define true 1
    if (fseek (MyFile, 0, SEEK_SET) == 0 );
    {
    bytesRead = fread ((void*) & Wizard, 1, sizeof(Wizard), MyFile);
    while ( !feof (MyFile))
    {
    fprintf(stdout, "\n %s\t\t\t %s\t\t\t %s", Wizard.Name, Wizard.WizardID, Wizard.Magic);
    bytesRead = fread ((void*) & Wizard, 1, sizeof(Wizard), MyFile);
    Why?

    Use code tags BTW.
    The world is waiting. I must leave you now.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >if ("Wanted" == "Wizard.WizardID")
    Use strcmp to compare strings.

    >while (!feof(MyFile))
    Dont loop using this as your control. Use the return code from fread().

    I have rewritten your display function. Have a look, then you can rework your other broken functions to match:

    Code:
    void display(void)
    {
    	FILE	*MyFile;
    	fprintf(stdout, "\n Wizard Name\t\t Wizard ID\t\t Wizard Magic");
    	fprintf(stdout, "\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    	
    	if ((MyFile = fopen("Wizard.dat", "rb")) == NULL)
    	{
    		perror("Wizard.dat");
    		getch();  /* I'm guessing your have this function */
    		return;
    	}
    	
    	while (fread(&Wizard, sizeof(Wizard), 1,MyFile) == 1)
    	{
    		fprintf(stdout, "\n %s\t\t\t %s\t\t\t %s", Wizard.Name, Wizard.WizardID, Wizard.Magic);
    	}
    	fclose(MyFile);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  4. File I/O with 2 input and 1 output file
    By Sandy in forum C Programming
    Replies: 1
    Last Post: 04-19-2003, 12:06 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM