newbie has question on file processing for C. 
Author Message
 newbie has question on file processing for C.

Hi,

I have a text file with records in the following format:

DATA:
1000256I tin tan 95.00  03/05/2001 Magic Show By ABC Pte Ltd
2300211H linder eng 185.00 02/03/2001 Musical By Toyota
4660652K eng wah 235.00 02/10/2001 Drama By Acts

I have created a program to read the above file. The program supposed
to be able to ask for an ID then loop thru the above data file to get a
match. But I hit the following problem.

1. I can never able to read the 1st char ie. M for Magic Show By ABC Ptd
Ltd
2. When I select for the 2nd search ie. 2300211H, I keep getting no data
was
found.

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

char cust_name1[20], cust_name2[20];
char cust_id[9], customer_id[9];

void search_list();

main()
{

    search_list();

Quote:
}

void search_list()
{
    int found=0;
    char        cont, p_date[11];
    float total_price;
    char p_desc[41];

    FILE *customer_search_ptr;

    clrscr();

    do
    {
       printf("Please Enter Customer ID : \n");
       scanf("%s", customer_id);
       fflush(stdin);

       (customer_search_ptr = fopen("c:\cust_data.dat", "r"));

       if (customer_search_ptr == NULL)
         {
           printf("File could not be opened - Program Aborting\n");
           exit(-1);
         }

       while (!feof(customer_search_ptr))
       {
           fscanf(customer_search_ptr, "%9s", cust_id);
           fgetc(customer_search_ptr);
           fscanf(customer_search_ptr, "%s", cust_name1);
           fgetc(customer_search_ptr);
           fscanf(customer_search_ptr, "%s", cust_name2);
           fgetc(customer_search_ptr);
           fscanf(customer_search_ptr, "%f", &total_price);
           fgetc(customer_search_ptr);
           fscanf(customer_search_ptr, "%11[^\n]%*c", p_date);
           fgetc(customer_search_ptr);
           fscanf(customer_search_ptr, "%40[^\n]%*c", p_desc);
           fgetc(customer_search_ptr);
           fflush(stdin);

           if (strcmp(customer_id,cust_id) == 0)
             {
                printf("Customer NRIC/Passport No.: %s \n", cust_id);
                printf("Customer First/Last Name  : %s %s \n", cust_name1,
cust_name2);
                printf("Total Price               : %3.2f \n",
total_price);
                printf("Performance Date          :%11s\n", p_date);
                printf("Performance               : %-40s \n", p_desc);

                found=1;
             }

           if (found==0)
             {
                printf("No record was found! Try Again.\n");
             }
                 }

       // close customer file
       fclose(customer_search_ptr);

    printf("\nContinue (Y/N)? ");
    scanf("%c", &cont);
    fflush(stdin);
    found=0;

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

Quote:
}

RESULTS:
Please Enter Customer ID :
1000256I

Customer NRIC/Passport No.: 1000256I
Customer First/Last Name    : tin tang
Total Price                          : 95.00
Performance Date                : 03/05/2001
Performance                       : Magic Show By ABC Pte Ltd

Continue (Y/N)? y
Please Enter Customer ID :
2300211H

No record was found! Try Again.
No record was found! Try Again.

Continue (Y/N)? y
Please Enter Customer ID :
2300211H

No record was found! Try Again.
No record was found! Try Again.

Continue (Y/N)?

What happen actually? Have I lost the pointer? Please help.
Thanks.

Cindy



Fri, 14 May 2004 00:47:00 GMT  
 newbie has question on file processing for C.

Quote:

> I have a text file with records in the following format:

> DATA:
> 1000256I tin tan 95.00  03/05/2001 Magic Show By ABC Pte Ltd
> 2300211H linder eng 185.00 02/03/2001 Musical By Toyota
> 4660652K eng wah 235.00 02/10/2001 Drama By Acts

> I have created a program to read the above file. The program supposed
> to be able to ask for an ID then loop thru the above data file to get
> a match. But I hit the following problem.

> 1. I can never able to read the 1st char ie. M for Magic Show By ABC
> Ptd Ltd
> 2. When I select for the 2nd search ie. 2300211H, I keep getting no
> data was found.

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

DELETE - non - standard header.

Quote:

> char cust_name1[20], cust_name2[20];
> char cust_id[9], customer_id[9];

> void search_list();

void search_list(void)

Quote:

> main()

int main(void)

Quote:
> {

>     search_list();
> }

> void search_list()
> {
>     int found=0;
>     char        cont, p_date[11];
>     float total_price;
>     char p_desc[41];

>     FILE *customer_search_ptr;

>     clrscr();

DELETE - non standard function, and even if functional very
annoying to the user.

Quote:

>     do
>     {
>        printf("Please Enter Customer ID : \n");
>        scanf("%s", customer_id);

FAILURE to check returned error code.  No protection against
overruns.

Quote:
>        fflush(stdin);

BONG - no such operation on input files.  DELETE.

Quote:

>        (customer_search_ptr = fopen("c:\cust_data.dat", "r"));

>        if (customer_search_ptr == NULL)
>          {
>            printf("File could not be opened - Program Aborting\n");
>            exit(-1);

BING - undefined behavior.  use EXIT_FAILURE.

Quote:
>          }

>        while (!feof(customer_search_ptr))

CRASH - feof is not used this way.  It is used to check why
something else failed.

Quote:
>        {
>            fscanf(customer_search_ptr, "%9s", cust_id);

FAILURE to check error returns.  Wide open to overrun errors. BAD.

Quote:
>            fgetc(customer_search_ptr);

DISCARDING an input character without a trace.

Quote:
>            fscanf(customer_search_ptr, "%s", cust_name1);

FAILURE to check error returns.  Wide open to overrun errors. BAD.

Quote:
>            fgetc(customer_search_ptr);

DISCARDING an input character without a trace.  Did it fail?

Quote:
>            fscanf(customer_search_ptr, "%s", cust_name2);

FAILURE to check error returns.  Wide open to overrun errors. BAD.

Quote:
>            fgetc(customer_search_ptr);

DISCARDING an input character without a trace.  Did it fail?

Quote:
>            fscanf(customer_search_ptr, "%f", &total_price);
>            fgetc(customer_search_ptr);

DISCARDING an input character without a trace.  Did it fail?

I am getting tired of this - fix your code, turn up the warning
levels, and fix what the compiler tells you about it.

- Show quoted text -

Quote:
>            fscanf(customer_search_ptr, "%11[^\n]%*c", p_date);
>            fgetc(customer_search_ptr);
>            fscanf(customer_search_ptr, "%40[^\n]%*c", p_desc);
>            fgetc(customer_search_ptr);
>            fflush(stdin);

>            if (strcmp(customer_id,cust_id) == 0)
>              {
>                 printf("Customer NRIC/Passport No.: %s \n", cust_id);
>                 printf("Customer First/Last Name  : %s %s \n", cust_name1,
> cust_name2);
>                 printf("Total Price               : %3.2f \n",
> total_price);
>                 printf("Performance Date          :%11s\n", p_date);
>                 printf("Performance               : %-40s \n", p_desc);

>                 found=1;
>              }

>            if (found==0)
>              {
>                 printf("No record was found! Try Again.\n");
>              }
>                  }

>        // close customer file
>        fclose(customer_search_ptr);

>     printf("\nContinue (Y/N)? ");
>     scanf("%c", &cont);
>     fflush(stdin);
>     found=0;

>         } while((cont == 'y') || (cont == 'Y'));

> }

> RESULTS:
> Please Enter Customer ID :
> 1000256I

> Customer NRIC/Passport No.: 1000256I
> Customer First/Last Name    : tin tang
> Total Price                          : 95.00
> Performance Date                : 03/05/2001
> Performance                       : Magic Show By ABC Pte Ltd

> Continue (Y/N)? y
> Please Enter Customer ID :
> 2300211H

> No record was found! Try Again.
> No record was found! Try Again.

> Continue (Y/N)? y
> Please Enter Customer ID :
> 2300211H

> No record was found! Try Again.
> No record was found! Try Again.

> Continue (Y/N)?

> What happen actually? Have I lost the pointer? Please help.

See above.  Time for a rewrite.

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)



Fri, 14 May 2004 01:49:16 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. A newbie needs help with file processing

3. A newbie needs help with file processing

4. Easy Newbie question How to process the contents of a structure

5. process I/O manipulation with C.

6. resx files needed for cs - files ??

7. Having the resource file dependent on an external file

8. CS files display in VS6 like CPP files ?

9. newbie having trouble

10. Newbie having problems with General Protection Exception

11. Having a newbie problem....

12. Another newbie having problem with the registry

 

 
Powered by phpBB® Forum Software