'Reading next lines from txt file in C
I have created the codes down below where it reads from a data.txt file and split each character into the array. Right now it reads the first line from the data.txt only. How can I make it loop and read the next line?
Can someone show me what needs to be added?
Contents of data.txt file
O7550x
N9300x
H51200
This is my existing code:
#include<stdio.h>
int main()
{
printf("Product code: ");
char a[6];
int i=0;
FILE *fptr;
fptr=fopen("data.txt", "r");
while((a[i++]=getc(fptr)) != '\n' && i < 6);
if(a[0] >= 'A' && a[0] <= 'Z')
{
if (a[0]=='O')
{
printf("State: Ohio");
}
else if (a[0]=='I'||a[0]=='H')
{
printf("State: Hawaii");
}
else if (a[0]=='N')
{
printf("State: New York");
}
else
{
printf("State: Unknown");
}
printf("\n");
if (a[1]=='5')
{
printf("Stores: Five");
}
else if (a[1]=='7')
{
printf("Stores: Seven");
}
else if (a[1]=='9')
{
printf("Stores: Nine");
}
else
{
printf("Stores: Unknown");
}
printf("\n");
if (a[2]=='3'&&a[3]=='0'&&a[4]=='0')
{
printf("Inventory: 300 units");
}
else if (a[2]=='5'&&a[3]=='5'&&a[4]=='0')
{
printf("Inventory: 500 units");
}
else if (a[2]=='1'&&a[3]=='2'&&a[4]=='0'&&a[5]=='0')
{
printf("Inventory: 1200 units");
}
else
{
printf("Inventory: Unknown");
}
}
else
printf("Invalid code");
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
