Thread: file input and output

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

    file input and output

    /*
    This program is to input wizard detail into disk, display all the wizard details and search for particular wizard detail using wizardID or name. How to solve my problem as stated below?
    My display function also cannot work... PLEASE 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();
    void remove();

    FILE *MyFile;
    int bytesWritten;
    int bytesRead;
    char wanted[10]; //If this is int, then my file input and output
    char found; // is working correctly.

    struct WizardStructure
    {
    char Name[30], Magic[100];
    char WizardID[10]; //If this is int, then my file input and
    }Wizard; //output is working correctly.

    /************************************************** */

    void main()
    {

    }

    /************************************************** */

    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); //question as above
    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);
    }

    /************************************************** */

    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);
    }
    }
    }

    /************************************************** */

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

    printf("\n Enter Wizard's ID : ");
    gets (wanted);

    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") //question as
    found = true; //above
    }

    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
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Smile

    Some help:

    1. void main is wrong; use int main
    2. fopen returns a value; ALWAYS check this value
    3. don't use gets; use fgets instead (it's safer)
    4. be carefull with using scanf and gets functions in the same program (scanf leaves the newline chars in your input buffer).
    5. use CODE TAGS
    6. it's not working because your main function is empty

    Cheers,
    Monster

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I used to use void main(void) myself before I started using comman-line options. What's the reasoning behind it?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Sean
    I used to use void main(void) myself before I started using comman-line options. What's the reasoning behind it?
    Read why void main is wrong......
    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. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. 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
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM