简体   繁体   English

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

[英]Search and write data from one file to another

I need to write a C program to fetch data from one file and write it to another file, without using user defined functions.我需要编写一个 C 程序来从一个文件中获取数据并将其写入另一个文件,而不使用用户定义的函数。 My requirements are to:我的要求是:

  • Search customer details by Name.按名称搜索客户详细信息。
  • Store the transaction data (paid amount) in another text file.将交易数据(支付金额)存储在另一个文本文件中。

I did the code to search by name.我做了按名称搜索的代码。 But its not working,但它不起作用,

#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 File客户.dat 文件

Shan    100012  200202
Marsh   121213  667675
Kim     126573  663412

This code is not complete asI cant filter the single name assigning此代码不完整,因为我无法过滤单个名称分配

if(name == fname)

as am getting正如我得到的

assignment to expression with array type error赋值给带有数组类型错误的表达式

Can any one complete me the code to search and save to another file so I can do the amount calculation part?谁能完成我的代码以搜索并保存到另一个文件,以便我可以进行金额计算部分?

few comments:几条评论:

  1. when scanning the choice, read it as an integer and not as a character.扫描选项时,将其读取为 integer 而不是字符。
    scanf("%c", &choice); // change to scanf("%d", &choice);
  1. single '=' is an assigment, you meant comparison which is double '=='单 '=' 是一个分配,你的意思是比较是双 '=='
    if(name = fname) // comparison is if(name == fname)
  1. in order to compare string, do not use '==' operator.为了比较字符串,不要使用 '==' 运算符。 use strcmp or implement an equivalent of strcmp.使用 strcmp 或实现 strcmp 的等效项。
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);
}

Thanks for the effort, As with changes, I have changed my code as below and its working.感谢您的努力,与更改一样,我已将我的代码更改为如下所示及其工作。 Without checking with the name, I alternately checked with the nic.没有检查名称,我交替检查了 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM