Validate the code if existing on the file, otherwise, display “record not found”)

I don't know how to validate the code if existing on the file, otherwise, display “record not found”)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>

struct product{
	char C[5], NAME[10];
	int Q;
};

int main ()
{
	struct product X[20];
	int ctr, N; 
	char choice;
	
	FILE *fp;
	fp = fopen("Inventory1.txt","w");
	
	
	printf("\nHow Many Products:\t");
	scanf("%d", &N);
	
		for (ctr=0; ctr<N; ctr++)
			{
				printf("\n\n%d.  Product Code:\t", ctr+1);
				scanf("\n");
				gets(X[ctr].C);
				printf("    Product Name:\t");
				scanf("\n");
				gets(X[ctr].NAME);
				printf("    Quantity:\t\t");
				scanf("\n%d", &X[ctr].Q);
			}
		
		for (ctr=0; ctr<N; ctr++)
		{
			fprintf(fp, "\n\n%s\n%s\n%d", X[ctr].C, X[ctr].NAME, X[ctr].Q);	
		}	
	printf("Do you want to update the inventory [Y/N]?   ");
	scanf(" %c", &choice);
	
	if (toupper(choice) == 'y')
	{
			if (X[ctr].C != ) // validating the code whether it exist or not
				{
					
				}
			else 
				{
					printf("Record not found!");
				}
				
		for (ctr=0;strcmp(goods[ctr].C,D)!=0;ctr++);
		{
			if (UC=='a'||UC=='A') 
				goods[ctr].Q+=X;
			else if (UC=='s'||UC=='S') 
				goods[ctr].Q-=X;
		}
	}
	else 
	{
			for (ctr=0; ctr<N; ctr++)
				{
					printf("\n%s \t\t\t %s \t\t %d", goods[ctr].C, goods[ctr].NAME, goods[ctr].Q);
				}
	}
	fclose(fp);
	
	getch();
}
Last edited on
In this program there is both an array product X[20] and a file "Inventory1.txt". After the first part of the program, both of these should contain the same data. Hence to find whether a record exists already, it would seem ok to search (use a loop) the array.
can you show an example?

1
2
3
4
5
6
7
printf("\nProduct Code: ");
			scanf("\n");
			gets(newc);
			printf("Update Code: ");
			scanf("%c", &op);
			printf("Quantity: ");
			scanf("\n%d", &qu);


for (i=0;strcmp(X[ctr].C,newc)!=0;i++);
can you show an example?


Maybe something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    char newc[] = "abc";
    
    int found = 0;
    int ctr = 0;
    for ( ; ctr<N; ctr++)
    {
        if (!strcmp(X[ctr].C, newc))
        {
            found = 1;
            break;
        } 
    }

    if (found)
        printf("%s was found at position %d \n", newc, ctr);
    else
        printf("%s was not found\n", newc);
Topic archived. No new replies allowed.