从C中的txt文件中读取下一行

发布于 2025-01-09 02:59:16 字数 1289 浏览 3 评论 0原文

我在下面创建了代码,它从 data.txt 文件读取并将每个字符拆分到数组中。现在它只读取 data.txt 中的第一行。我怎样才能让它循环并读取下一行?

有人可以告诉我需要添加什么吗?

data.txt 文件的内容

 O7550x
 N9300x
 H51200

这是我现有的代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪了杯 2025-01-16 02:59:16

我不确定这是否是您想要做的事情,但下面的代码可以同时读取命令行输入和 txt 文件。但是,您无法使用此代码循环遍历所有元素。为此,您需要读取行数并动态分配内存并将每行存储在动态创建的数组的第二个维度中。

#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");
    }

}

另外,这是您需要为此代码提供的文本文件。

O7550x
N9300x
H51200

祝你好运!

我以一种可以读取所有行并打印结果的方式编辑了代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文