Thread: Help on simple file functions

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    21

    Unhappy Help on simple file functions

    Hi guys,

    its me again i got problem on my file function.

    A function new() that appends a new contact information into the file.
    The function asks for a name, telephone no., and a cell phone no., stores these in a structure, then inserts it at the end of the file. If the file does not exist yet, create a new file.

    I got this working, but in case of any corrections...here it is:

    Code:
    void new (void)
    {
    FILE *fp;
    contact_rec X;
    
    if (( fp = fopen ("phone.dat", "ab")) == NULL)
    {
    
    printf ("Error : Unable to open file");
    exit(1);
    
    }
    
    else
    {
    
    printf("Enter a Name: "); fflush(stdin);
    gets(X.name);
    printf("Enter tel no.: "); fflush(stdin);
    gets(X.tel);
    printf("Enter cel no.: "); fflush(stdin);
    gets(X.cel);
    
    fwrite(&X, sizeof(contact_rec), 1, fp);
    fclose(fp);
    }
    }


    A function view() that displays all contact information from the file.
    The function opens the file, reads one record at a time and displays it onscreen.

    I get an error of "Expression Syntax"
    Here is my code:

    Code:
    void view (void)
    {
    
    FILE *fp;
    contact_rec X;
    
    if ((fp = fopen ("phone.dat", "rb")) == NULL)
    {
    printf("Error : File is empty");
    exit(1);
    }
    
    
    while (fread(&X, sizeof(contact_rec), 1, fp)) != NULL) <----"Expression Syntax Error"
    {
    printf("%s",X.name);
    printf("%s",X.tel);
    printf("%s",X.cel);
    }
    
    fclose(fp);
    }
    Last. A function search() that displays the contact information given the name.
    The fucntion asks for a name, searches the name from the file, then displays the telephone no., and cell phone no. If the name is not found, display "record not found."


    I havent got to this one yet since im having problem in the second function
    Heres my code anyway:

    Code:
    void search (void)
    {
    
    FILE *fp;
    contact_rec X[4];
    char c;
    int i = 0;
    
    if ((fp = fopen ("phone.dat", "rb")) == NULL)
    {
    printf ("Error : file is does not exist");
    exit(1);
    }
    
    printf ("Enter a name to be search: ");
    gets(s);
    
    for ( i = 0; i < 5; i++)
    {
    
    if ((strcmp (s, X[i].name)) == 0)
    {
    
    printf("%s", X[i].tel);
    printf("%s", X[i].cel);
    
    }
    
    else
    {
    
    printf("Error : Record not found");
    
    }
    
    }

    Your help is very much appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have a bunch of problems here. You use gets, which is a horrible function. You use fflush on an input stream, which is undefined behavior (it doesn't work on my compiler, as it shouldn't). You apparently dont' know how to press the space bar, because you have no indentation. You aren't actually reading your file at all when you attempt to search, so I'm not sure how you expect to find anything.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    21
    if not gets then what should i use?

    for the spacebar the program is not finish yet...i make indentation after i finish making the whole program.


    You aren't actually reading your file at all when you attempt to search, so I'm not sure how you expect to find anything.
    which part?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by enjoyincubus
    if not gets then what should i use?
    FAQ (clicky click)

    Quote Originally Posted by enjoyincubus
    for the spacebar the program is not finish yet...i make indentation after i finish making the whole program.
    That's just ... sad.
    Quote Originally Posted by enjoyincubus
    which part?
    I already said. The search function. It doesn't do any file reading.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    i make indentation after i finish making the whole program
    It is too late... There will be no reason to do it... You need the correct indentation during your work not after its finished
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    21
    @quazah

    thanks for the quick faqs

    hmm i see...about the search funtion you mean the fread right?

    other than file reading hows my search function?, is it correct?

    @vart and quazah

    ok, ill practice making indentations while making the program...

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    ok, ill practice making indentations while making the program...
    In VC6.0 you can use Alt+F8 to autoindent selected code... I think in other IDEs this feature can be also available
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    21
    i really got used to in writing the code in the paper because all of the exams in my school is written in paper and i try as much as i could to fit the whole program...so thats why.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    21
    can anyone check this?

    im still having problem

    Code:
    void view (void)
    {
    
    FILE *fp;
    contact_rec X;
    
    if ((fp = fopen ("phone.dat", "rb")) == NULL)
    {
    printf("Error : File is empty");
    exit(1);
    }
    
    
    while (fread(&X, sizeof(contact_rec), 1, fp)) != NULL) <----"Expression Syntax Error"
    {
    printf("%s",X.name);
    printf("%s",X.tel);
    printf("%s",X.cel);
    }
    
    fclose(fp);
    }
    the expression syntax

  10. #10
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    For one thing, fread returns the number of parameters read. So you want to check for 0, not null.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    21
    ive got it to work...

    it was the parenthesis

    im down to the last

    Code:
    void search (void)
    {
    
    FILE *fp;
    contact_rec X[4];
    char c;
    int i = 0;
    
    if ((fp = fopen ("phone.dat", "rb")) == NULL)
    {
    printf ("Error : file is does not exist");
    exit(1);
    }
    
    printf ("Enter a name to be search: ");
    gets(s);
    
    for ( i = 0; i < 5; i++)
    {
    
    if ((strcmp (s, X[i].name)) == 0)
    {
    
    printf("%s", X[i].tel);
    printf("%s", X[i].cel);
    
    }
    
    else
    {
    
    printf("Error : Record not found");
    
    }
    
    }
    any corrections?

  12. #12
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You're still using gets. Did you read what the others said ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You also still aren't doing anything in your search function. Did you even bother reading what I wrote? What exactly are you expecting to search? You don't have any data in your structures.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    while (fread(&X, sizeof(contact_rec), 1, fp)) != NULL) <----"Expression Syntax Error"

    while (fread(&X, sizeof(contact_rec), 1, fp) != 0) //<----"Expression Syntax Error"

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    21
    thanks for all your reply

    @Happy_reaper

    i changed it now im using fgets


    @jermy75

    thanks for the correction


    @quzah

    ive read what youve wrote

    if you mean the data that are being ask by the user , i already have them

    it works w/ view function



    i was able to run it and the result of the search function is this

    it wont let me type the name to be search

    it will result into an "Error: Record not found"

    here is my code again:

    Code:
    void search (void)
    {
    
    FILE *fp;
    contact_rec X[4];
    char s;
    int i = 0;
    
    if ((fp = fopen ("phone.dat", "rb")) == NULL)
    	{
    		printf ("Error : file is does not exist");
    		exit(1);
    	}
    
        	printf ("Enter a name to be search: ");
        	fgets(&s, sizeof(contact_rec), stdin);
    
    	for ( i = 0; i < 5; i++)
    	{
    
    		if ((strcmp (&s, X[i].name)) == 0)
    		{
    
    			printf("%s", X[i].tel);
    			printf("%s", X[i].cel);
    
    		}
    
    		else
    		{
    
    			printf("Error : Record not found");
    
    			fclose(fp);
    		}
          	 }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 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