C 搜索并将数据从一个文件写入另一个文件

C 搜索并将数据从一个文件写入另一个文件,c,clang,C,Clang,我需要编写一个C程序,从一个文件中提取数据并将其写入另一个文件,而不使用用户定义的函数。我的要求是: 按姓名搜索客户详细信息 将交易数据(已付金额)存储在另一个文本文件中 我做了按名字搜索的代码。但它不起作用 #include <stdio.h> #include <stdlib.h> int main () { char name[10], nic[10], mobile[10]; char fname[10], fnic[10], fmobile[1

我需要编写一个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);
}
此代码不完整,因为无法过滤指定的单个名称

if(name == fname)
我越来越

指定给具有数组类型错误的表达式

有人能帮我完成搜索代码并保存到另一个文件中,这样我就可以进行金额计算部分了吗?

几点意见:

  • 扫描选项时,请将其作为整数而不是字符读取
  • 单'='是一个赋值,您指的是双'='的比较
  • 为了比较字符串,请不要使用“==”运算符。使用strcmp或实施等效的strcmp

  • 感谢您的努力,就像更改一样,我已经更改了下面的代码及其工作方式。没有检查名称,我交替检查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;
    }
    
    #包括
    内部主(空){
    int nic,n,移动电话;
    字符名[30];
    档案*aPtr;
    aPtr=fopen(“Details.txt”,“w”);
    如果(aPtr==NULL){
    printf(“无法打开文件”);
    返回-1;
    }
    printf(“输入nic以搜索-”;
    scanf(“%d”和“&n”);
    fscanf(aPtr、%d%-s%d、&nic、名称和移动设备);
    而(!feof(aPtr)){
    如果(nic==n){
    Printf(“%d%s%d\n”,nic,名称,手机);
    }
    fscanf(aPtr、“%d%s%d”、&nic、名称和移动设备);
    }
    fclose(aPtr);
    返回0;
    }
    
    如果(choice=1)
    有什么奇怪的事情吗?
    while(!feof(cfptr))
    这样做。@user12986714 while中应该使用什么?我还删除了if子句,您可能应该使用
    while(fscanf(…)==3)
    检查是否返回了三个值。看起来您定义了
    main()
    ——这不是系统提供的函数。是的,我在吹毛求疵,但“不要使用函数”是一条愚蠢的规则。即使是老师强加的,这仍然是一个愚蠢的规则。正确地使用函数是学习用C编写代码的核心。如果不使用系统提供的和用户定义的函数,就无法用C编写好代码。
        scanf("%c", &choice); // change to scanf("%d", &choice);
    
        if(name = fname) // comparison is if(name == fname)
    
    #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;
    }
    
    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);
    }