Home > Mobile >  Reading next lines from txt file in C
Reading next lines from txt file in C

Time:02-23

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;
}

CodePudding user response:

I am not sure this is the thing you want to do, but here is the code below that does both command-line input and txt file read. However, you cannot loop through all elements with this code. For that purpose, you need to read the number of lines and dynamically allocate memory and store each line in the second dimension of the dynamically created array.

#include<stdio.h>
#include<string.h>

void printProductCode(char*);
int numberOfLines(FILE*);
void display_array(char**, int, int);

int main()
{
    int flag, sizeOfFile = 0, readFrom = 0;
    char **b;
    char a[6];
    int i=0;
    FILE *fptr;

    do {
        flag = 1;
        printf("Which way you will enter the product code? (Command-Line: 1), (File: 2) ");
        scanf("%d",&readFrom);

        if(readFrom == 1) {
            printf("Product code: ");
            getchar();
            while((a[i  ]=getchar()) != '\n' && i < 6);
        } else if(readFrom == 2) {
            int j = 0;
            fptr=fopen("data.txt", "r");

            sizeOfFile = numberOfLines(fptr);
            b = (char**)malloc(sizeOfFile * sizeof(char*));

            for (int i = 0;i < sizeOfFile;i  ) {
                b[i] = (char*)malloc(6 * sizeof(char));
            }

            while(fscanf(fptr, "%s", b[j  ]) != EOF);
        } else {
            printf("Please enter valid input!");
            flag = 0;
        }
    } while(flag == 0);

    if (readFrom == 1) {
        printProductCode(a);
    }
    else if(readFrom == 2){
        for(int i = 0;i < sizeOfFile;i  ) {
            printProductCode(b[i]);
        }

        //Freeing could done with a different if statement because
        //we may want to use b in the following steps, but for
        //simplicity I freed the memory here for now on.
        for(int i=0;i<sizeOfFile;i  ) {
            free(b[i]);
        }
        free(b);
    }



    return 0;
}

void printProductCode(char* a) {
    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");
    printf("\n\n");
}

int numberOfLines(FILE* fp) {
    int counter = 0;
    char c = fgetc(fp);
    while(c != EOF) {
        c = fgetc(fp);
        if (c == '\n'){
            counter  ;
        }
    }
    rewind(fp);
    return (counter   1);
}

void display_array(char** arr, int r, int c) {
    for(int i = 0;i<r;i  ) {
        for(int j = 0;j < c;j  ) {
            printf("%c", arr[i][j]);
        }
        printf("\n");
    }

}

Also, here is the text file you need to provide for this code.

O7550x
N9300x
H51200

Good luck!

I edited the code in a way that it can read all lines and print the results.

  • Related