gpt4 book ai didi

c - 如何跳过一行中的第一个字符 - C

转载 作者:行者123 更新时间:2023-11-30 14:23:15 24 4
gpt4 key购买 nike

我有一个简单的程序,可以读取一行,其中包含 3 个数字。我需要跳过第一个数字,这是“产品”中的代码。

所以,我只需要读取第二个和第三个字符。

我该怎么做?

到目前为止的代码:

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

int calculate_price (number, value)
{
int price=0;
price= number*value;
return price;
}

int main(void)
{
int number, value, u,price;
FILE *bill, *total_price;
bill= fopen("bill.txt","rt");
total_price= fopen("total_price.txt","wt");

if (bill== NULL)
{
printf("The file cannot be open.\nQuitting the program.\n");
exit(1);
}

if (total_price== NULL)
{
printf("The file canno be written.\nQuitting the program.\n");
exit(1);
}

while (fscanf(bill, "%d %d",&number, &value) != EOF)
{
u=calculate_price(number, value);
fprintf(total_price,"The total price is %d\n", u);
}
printf("File created sucessfully. Check the file.\n");

}

最佳答案

int main(void)
{
int number, value, u,price;
int dummy; //this is a dummy var

...

while (fscanf(bill, "%d %d %d",&dummy, &number, &value) != EOF)
{
u=calculate_price(number, value);
fprintf(total_price,"The total price is %d\n", u);
}
printf("File created sucessfully. Check the file.\n");

编辑之前我使用了一个 char 作为虚拟变量,因为不知何故我首先误解了这个问题,但我将其更正为 int...

关于c - 如何跳过一行中的第一个字符 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13001340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com