【问题标题】:Search and write data from one file to another从一个文件搜索数据并将其写入另一个文件
【发布时间】:2020-05-05 16:06:26
【问题描述】:

我需要编写一个 C 程序来从一个文件中获取数据并将其写入另一个文件,而不使用用户定义的函数。我的要求是:

  • 按名称搜索客户详细信息。
  • 将交易数据(支付金额)存储在另一个文本文件中。

我编写了按名称搜索的代码。但它不起作用,

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

int main () {
   char name[10], nic[10], mobile[10];
   char fname[10], fnic[10], fmobile[10];
   char choice;
   int amount;
   FILE *cfptr;


   printf("Enter search type  - \n 1. NAME \n 2. NIC \n 3.MOBILE \n ----> ");
   scanf("%c", &choice);

        printf("Enter search text : ");
        scanf("%s", &name);

        cfptr = fopen ("customer.dat", "r");

        while (!feof(cfptr)){

            fscanf(cfptr, "%s %s %s", fname, fnic, fmobile);

            printf("Read Name |%s|\n", fname );
            printf("Read NIC |%s|\n", fnic );
            printf("Read Mobile |%s|\n", fmobile );
   }
   fclose(cfptr);
   scanf("%d", &amount);

   return(0);
}

customer.dat 文件

Shan    100012  200202
Marsh   121213  667675
Kim     126573  663412

此代码不完整,因为我无法过滤单个名称分配

if(name == fname)

现在

赋值给数组类型错误的表达式

谁能完成我的代码以搜索并保存到另一个文件,以便我可以进行金额计算部分?

【问题讨论】:

  • if(choice = 1) 有什么奇怪的吗?
  • while (!feof(cfptr)) don't 这样做。
  • @user12986714 这段时间我应该使用什么?我还删除了 if 子句
  • 您可能应该使用while (fscanf(…) == 3) 来检查您是否返回了三个值。
  • 嗯,看起来你定义了main()——这不是系统提供的功能。是的,我在挑剔,但“不要使用函数”是一个愚蠢的规则。即使是老师强加的,这仍然是一个愚蠢的规则。正确使用函数是学习用 C 编写代码的核心。如果不使用系统提供的和用户定义的函数,就无法用 C 编写好代码。

标签: c clang


【解决方案1】:
int Search_in_File(char *fname, char *str) {
    FILE *fp;
    int line_num = 1;
    int find_result = 0;
    char temp[512];

    //gcc users
    //if((fp = fopen(fname, "r")) == NULL) {
    //  return(-1);
    //}

    //Visual Studio users
    if((fopen_s(&fp, fname, "r")) != NULL) {
        return(-1);
    }

    while(fgets(temp, 512, fp) != NULL) {
        if((strstr(temp, str)) != NULL) {
            printf("A match found on line: %d\n", line_num);
            printf("\n%s\n", temp);
            find_result++;
        }
        line_num++;
    }

    if(find_result == 0) {
        printf("\nSorry, couldn't find a match.\n");
    }

    //Close the file if still open.
    if(fp) {
        fclose(fp);
    }
    return(0);
}

【讨论】:

    【解决方案2】:

    几个cmets:

    1. 扫描选项时,将其读取为整数而不是字符。
        scanf("%c", &choice); // change to scanf("%d", &choice);
    
    1. 单 '=' 是一个分配,你的意思是比较是双 '=='
        if(name = fname) // comparison is if(name == fname)
    
    1. 为了比较字符串,不要使用'==' 运算符。使用 strcmp 或实现 strcmp 的等效项。

    【讨论】:

      【解决方案3】:

      感谢您的努力,与更改一样,我已将我的代码更改如下并使其正常工作。没有检查名称,我交替检查了 nic。

      #include <stdio.h>
      int main(void){
          int nic, n, mobile;
          char name[30]; 
          FILE *aPtr;
      
          aPtr = fopen("Details.txt","w"); 
          if(aPtr == NULL){
              printf("File cannot be opened");
              return -1;
          }
          printf("Enter nic to search - "); 
          scanf("%d", &n); 
          fscanf(aPtr, "%d %-s %d", &nic, name, &mobile); 
      
          while(!feof(aPtr)){
              if(nic == n){
                  Printf("%d %s %d \n", nic, name, mobile); 
              }
              fscanf(aPtr, "%d %s %d", &nic, name, &mobile); 
          }
          fclose(aPtr); 
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-29
        • 2022-01-14
        • 2021-01-23
        • 2020-01-25
        • 2018-06-27
        • 1970-01-01
        相关资源
        最近更新 更多