在文件c程序搜索的I / O [英] C Program search in FILE I/O

查看:73
本文介绍了在文件c程序搜索的I / O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code被认为能找到产品ID,名称,数量及价格。
当输入的产品ID,它应该打印全线该产品ID的位置。

该文本文件包含此输入:

  1吉百利999 1.900000
2 PEPSI 999 2.500000
3 IPHONE 976 2500.000000
4螺旋藻100 50.000000

在code已汇编4的输入。但输出循环是:

 请输入产品编号:

这不是阅读'4'的整行。

这里的code:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&WINDOWS.H GT;
#包括LT&;&文件ctype.h GT;INT addProduct命令();产品结构
{
    INT数量,重新排序,我,身份证;
    焦炭名[20];
    浮动价格;
};INT secondmain()
{
    FILE * FP;    INT I = 0;
    结构产品中;
    系统(CLS);    字符检查;    做
    {
        FP = FOPEN(addproduct.txt,A + T);
        系统(CLS);        的printf(请输入产品编号:);
        scanf函数(%d个,&安培; a.id);        的printf(请输入产品名称:);
        scanf函数(%S,a.name);        的printf(请输入产品数量:);
        scanf函数(%d个,&安培; a.quantity);        的printf(请输入产品价格:);
        scanf函数(%F,&安培; a.price);
        fprintf中(FP,%d个%S%d个%F \\ n \\ n,a.id,a.name,a.quantity,a.price);
        的printf(记录保存\\ n \\ n!);        FCLOSE(FP);        的printf(你想进入新产品的Y / N?);        scanf函数(%C,&安培;检查);
        检查= TOUPPER(检查);        我++;        系统(CLS);
    }
    而(检查=='Y');    如果(检查=='N')
        {
        FP = FOPEN(addproduct.txt,R);        而(的fscanf(FP,%D%S%D%F,&安培; a.id,a.name,&安培; a.quantity,&安培; a.price)== 4)
            {
        的fscanf(FP,%D%S%D%F,&安培; a.id,a.name,&安培; a.quantity,&安培; a.price);
        的printf(%d个%S%d个%F \\ n \\ n,a.id,a.name,a.quantity,a.price);
            }        FCLOSE(FP);        }
返回(0);
}/ * -------------- ERROR从这里开始----------------- * /诠释的main()
{
FILE * FP;
系统(CLS);
结构产品中;
字符数组[255];
INT的productID;FP = FOPEN(addproduct.txt,R);而(1){
的fscanf(FP,%D%S%D%F,&安培; a.id,a.name,&安培; a.quantity,&安培; a.price);=的productID与atoi(数组);的printf(请输入产品ID:);
scanf函数(%d个,&安培;的productID);如果(==的productID a.id)
    {
    与fgets(阵列,255,(FILE *)FP);
    的printf(%d个%S%d个%F \\ n \\ n,a.id,a.name,a.quantity,a.price);    }    }
返回(0);
}


解决方案

 而(的fscanf(FP,%D%S%D%F,&安培; a.id,一.name和&安培; a.quantity,&安培; a.price)== 4)
{
    的fscanf(FP,%D%S%D%F,&安培; a.id,a.name,&安培; a.quantity,&安培; a.price);
    的printf(%d个%S%d个%F \\ n \\ n,a.id,a.name,a.quantity,a.price);
}

您在呼唤的fscanf 两次,它会跳过每2行。

 与fgets(阵列,255,(FILE *)FP);
的printf(%d个%S%d个%F \\ n \\ n,a.id,a.name,a.quantity,a.price);

这部分是读线路中的文本。那么它应该使用的sscanf strtok的来解析线。

您可能还需要刷新标准输入,否则 scanf函数(%C,&安培;检查)可能无法正常工作。它使事情变得更容易中的功能,打破这件事。例如:

  INT find_item_by_id(为const char * FNAME,INT find_id)
{
    INT发现= 0;
    结构产品中;
    FILE * FP = FOPEN(FNAME,R);
    如果(FP)
    {
        而(的fscanf(FP,%D%S%D%F,&安培; a.id,a.name,&安培; a.quantity,&安培; a.price)== 4)
        {
            如果(a.id == find_id)
            {
                找到= 1;
                打破;
            }
        }
        FCLOSE(FP);
    }    返回找到;
}无效add_item(为const char * FNAME)
{
    结构产品中;
    的printf(请输入产品编号:);
    scanf函数(%d个,&安培; a.id);    的printf(请输入产品名称:);
    scanf函数(%S,a.name);    的printf(请输入产品数量:);
    scanf函数(%d个,&安培; a.quantity);    的printf(请输入产品价格:);
    scanf函数(%F,&安培; a.price);    如果(find_item_by_id(FNAME,a.id)!= 0)
    {
        输出(项目已经存在\\ n);
        返回;
    }    FILE * FP = FOPEN(FNAME,A +);
    如果(FP)
    {
        fprintf中(FP,%D%S%D%.2f \\ n,a.id,a.name,a.quantity,a.price);
        FCLOSE(FP);
    }
}诠释的main()
{
    为const char *文件名=C:\\\\ \\\\测试_的test.txt;    的printf(列表:\\ n);
    结构产品中;
    FILE *计划生育=的fopen(文件名,R);
    如果(FP)
    {
        而(的fscanf(FP,%D%S%D%F,&安培; a.id,a.name,&安培; a.quantity,&安培; a.price)== 4)
            的printf(%d个%S%d个%F \\ N,a.id,a.name,a.quantity,a.price);
        FCLOSE(FP);
    }    而(1)
    {
        add_item(文件名);        的printf(添加诺特尔项目(Y / N)\\ n吗?);
        INT结果;
        而(1)
        {
            结果=的getchar();
            如果(结果=='Y'||结果=='N')
                打破;
        }
        如果(结果!=Y)
            打破;
    }    返回0;
}

My code is supposed to find the product ID, name, quantity and price. When the product ID is entered, it should print the whole line where the product ID is located.

The text file contains this input:

1 CADBURY 999 1.900000
2 PEPSI 999 2.500000
3 IPHONE 976 2500.000000
4 SPIRULINA 100 50.000000

The code had been compiled with input of '4'. But the output was looping:

Please enter product ID :

It's not reading the whole line of '4'.

Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>

int addProduct();

struct product
{
    int quantity, reorder, i, id;
    char name[20];
    float price;
};

int secondmain()
{
    FILE * fp;

    int i=0;
    struct product a;
    system("cls");

    char checker;



    do
    {
        fp = fopen("addproduct.txt","a+t");
        system("cls");

        printf("Enter product ID : ");
        scanf(" %d", &a.id);

        printf("Enter product name : ");
        scanf(" %s", a.name);

        printf("Enter product quantity : ");
        scanf(" %d", &a.quantity);

        printf("Enter product price : ");
        scanf(" %f", &a.price);


        fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
        printf("Record saved!\n\n");

        fclose(fp);

        printf("Do you want to enter new product? Y / N : ");

        scanf(" %c", &checker);
        checker = toupper(checker);

        i++;

        system("cls");
    }
    while(checker=='Y');



    if(checker == 'N')
        {
        fp = fopen("addproduct.txt","r");

        while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
            {
        fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);
        printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
            }

        fclose(fp);

        }


return(0);
}

/*-------------- ERROR START HERE-----------------*/

int main()
{
FILE * fp;
system("cls");
struct product a;
char array[255];
int productID;

fp = fopen("addproduct.txt","r");

while(1){
fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);

productID = atoi(array);

printf("Please enter product ID : ");
scanf(" %d", &productID);

if(productID == a.id)
    {
    fgets(array, 255, (FILE*)fp);
    printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);

    }

    }
return(0);
}

解决方案

while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4)
{
    fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);
    printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}

You are calling fscanf twice, it skips every 2nd line.

fgets(array, 255, (FILE*)fp);
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);

This part is reading the line in to text. It should then use sscanf or strtok to parse line.

You may also have to flush stdin otherwise scanf(" %c", &checker) may not work. It makes things easier to break this up in to functions. Example:

int find_item_by_id(const char* fname, int find_id)
{
    int found = 0;
    struct product a;
    FILE *fp = fopen(fname, "r");
    if (fp)
    {
        while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4)
        {
            if (a.id == find_id)
            {
                found = 1;
                break;
            }
        }
        fclose(fp);
    }

    return found;
}

void add_item(const char* fname)
{
    struct product a;
    printf("Enter product ID : ");
    scanf(" %d", &a.id);

    printf("Enter product name : ");
    scanf(" %s", a.name);

    printf("Enter product quantity : ");
    scanf(" %d", &a.quantity);

    printf("Enter product price : ");
    scanf(" %f", &a.price);

    if (find_item_by_id(fname, a.id) != 0)
    {
        printf("item already exists\n");
        return;
    }

    FILE *fp = fopen(fname, "a+");
    if (fp)
    {
        fprintf(fp, "%d %s %d %.2f\n", a.id, a.name, a.quantity, a.price);
        fclose(fp);
    }
}

int main()
{
    const char* filename = "c:\\test\\_test.txt";

    printf("list:\n");
    struct product a;
    FILE *fp = fopen(filename, "r");
    if (fp)
    {
        while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4)
            printf("%d %s %d %f\n", a.id, a.name, a.quantity, a.price);
        fclose(fp);
    }

    while(1)
    {
        add_item(filename);

        printf("Add nother item? (y/n)\n");
        int result;
        while (1)
        {
            result = getchar();
            if (result == 'y' || result == 'n')
                break;
        }
        if (result != 'y')
            break;
    }

    return 0;
}

这篇关于在文件c程序搜索的I / O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆