从C中的txt文件中读取下一行
我在下面创建了代码,它从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是否是您想要做的事情,但下面的代码可以同时读取命令行输入和 txt 文件。但是,您无法使用此代码循环遍历所有元素。为此,您需要读取行数并动态分配内存并将每行存储在动态创建的数组的第二个维度中。
另外,这是您需要为此代码提供的文本文件。
祝你好运!
我以一种可以读取所有行并打印结果的方式编辑了代码。
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.
Also, here is the text file you need to provide for this code.
Good luck!
I edited the code in a way that it can read all lines and print the results.