1

I need to write a C program to fetch data from one file and write it to another file, without using user defined functions. My requirements are to:

  • Search customer details by Name.
  • Store the transaction data (paid amount) in another text file.

I did the code to search by name. But its not working,

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

int main () {
   char name[10], nic[10], mobile[10];
   char fname[10], fnic[10], fmobile[10];
   char choice;
   int amount;
   FILE *cfptr;


   printf("Enter search type  - \n 1. NAME \n 2. NIC \n 3.MOBILE \n ----> ");
   scanf("%c", &choice);

        printf("Enter search text : ");
        scanf("%s", &name);

        cfptr = fopen ("customer.dat", "r");

        while (!feof(cfptr)){

            fscanf(cfptr, "%s %s %s", fname, fnic, fmobile);

            printf("Read Name |%s|\n", fname );
            printf("Read NIC |%s|\n", fnic );
            printf("Read Mobile |%s|\n", fmobile );
   }
   fclose(cfptr);
   scanf("%d", &amount);

   return(0);
}

customer.dat File

Shan    100012  200202
Marsh   121213  667675
Kim     126573  663412

This code is not complete asI cant filter the single name assigning

if(name == fname)

as am getting

assignment to expression with array type error

Can any one complete me the code to search and save to another file so I can do the amount calculation part?

Bubashan_kushan
  • 384
  • 8
  • 24

3 Answers3

1

few comments:

  1. when scanning the choice, read it as an integer and not as a character.
    scanf("%c", &choice); // change to scanf("%d", &choice);
  1. single '=' is an assigment, you meant comparison which is double '=='
    if(name = fname) // comparison is if(name == fname)
  1. in order to compare string, do not use '==' operator. use strcmp or implement an equivalent of strcmp.
AEM
  • 1,354
  • 8
  • 20
  • 30
nivpeled
  • 1,810
  • 5
  • 17
1
int Search_in_File(char *fname, char *str) {
    FILE *fp;
    int line_num = 1;
    int find_result = 0;
    char temp[512];

    //gcc users
    //if((fp = fopen(fname, "r")) == NULL) {
    //  return(-1);
    //}

    //Visual Studio users
    if((fopen_s(&fp, fname, "r")) != NULL) {
        return(-1);
    }

    while(fgets(temp, 512, fp) != NULL) {
        if((strstr(temp, str)) != NULL) {
            printf("A match found on line: %d\n", line_num);
            printf("\n%s\n", temp);
            find_result++;
        }
        line_num++;
    }

    if(find_result == 0) {
        printf("\nSorry, couldn't find a match.\n");
    }

    //Close the file if still open.
    if(fp) {
        fclose(fp);
    }
    return(0);
}
Makwana Prahlad
  • 1,025
  • 5
  • 20
0

Thanks for the effort, As with changes, I have changed my code as below and its working. Without checking with the name, I alternately checked with the nic.

#include <stdio.h>
int main(void){
    int nic, n, mobile;
    char name[30]; 
    FILE *aPtr;

    aPtr = fopen("Details.txt","w"); 
    if(aPtr == NULL){
        printf("File cannot be opened");
        return -1;
    }
    printf("Enter nic to search - "); 
    scanf("%d", &n); 
    fscanf(aPtr, "%d %-s %d", &nic, name, &mobile); 

    while(!feof(aPtr)){
        if(nic == n){
            Printf("%d %s %d \n", nic, name, mobile); 
        }
        fscanf(aPtr, "%d %s %d", &nic, name, &mobile); 
    }
    fclose(aPtr); 
    return 0;
}
Bubashan_kushan
  • 384
  • 8
  • 24