首页 > 解决方案 > 如何修改文件中的内容?

问题描述

我是C的完全初学者。我的问题是修改文件中的内容。

我正在编写两个文件,然后将两个文件的内容合并到另一个文件中。这另一个文件是我需要修改的文件。

要修改什么?

myfile1.txt 值为 199112345671273,myfile2.txt 值为 24AUS2024MED712。

合并文件(myfile3.txt)有 19911234567127324AUS2024MED712

我需要修改的是 myfile2.txt 的值。我想将其值隐藏在星号中,因此在读取 myfile3.txt 时,我得到以下信息

199112345671273****************

我的逻辑搞砸了。我只想存储 myfile1 和 myfile2 的值。然后显示 myfile3,条件是读取时必须将 myfile2 隐藏在星号中。

我的 write.c 程序 - 在两个文件中写入数据

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

#define MAX_SIZE 100

int main (int argc, char **argv) {
    char registration[MAX_SIZE], location[MAX_SIZE], faculty[MAX_SIZE];
    int birthOfYear, birthOfMonth, birthOfDate, layerArch1, layerArch2, levelOfStudy, graduatingYear;

    FILE *fptr, *anotherfptr;
    fptr = fopen("myfile01.txt","w");
    anotherfptr = fopen("myfile02.txt", "w");
    if(fptr == NULL) {
            printf("Error!");   
            exit(1);             
    }

    printf("Enter a registration number (XXXXXX): ");
    scanf("%s", registration); //read as a string

    printf("Enter location (location as in currency, AUS CND SIN: ");
    scanf("%s", location); //read as a string

    printf("Enter faculty (ENG BUS SCI MED): ");
    scanf("%s", faculty); //read as a string

    printf("Enter birth of year (19XX 200X): ");
    scanf("%d", &birthOfYear);

    printf("Enter birth of month (XX): ");
    scanf("%d", &birthOfMonth);

    printf("Enter birth of date (XX): ");
    scanf("%d", &birthOfDate);

    printf("Enter level of study (1 -first, 2- second, 3- third, 4-fourth, 5 - other): ");
    scanf("%d", &levelOfStudy);

    printf("Enter graduating year (XXXX): ");
    scanf("%d",&graduatingYear);

    printf("Enter layer of Architecture 1 (0-sensing, 1-network, 2-smart(hidden), 3-devices): ");
    scanf("%d",&layerArch1);

    printf("Enter layer of Architecture 2 (0-sensing, 1-network, 2-smart(hidden), 3-devices): ");
    scanf("%d",&layerArch2);

    fprintf(fptr,"%d%s%d%d%d", birthOfYear, registration, birthOfMonth, birthOfDate, layerArch1); //writing into file with some formatting 
    fclose(fptr);

    fprintf(anotherfptr,"%d%d%s%d%s%d%d", layerArch2, levelOfStudy, location, graduatingYear, faculty, birthOfDate, birthOfMonth); 
    //writing into file with some formatting

    fclose(anotherfptr);

    return 0;
  }

我的 merge.c 程序 - 合并两个文件

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

int main(int argc, char **argv)
{
   FILE *fs1, *fs2, *ft;

   char ch, file1[200], file2[200], file3[200];

   printf("Enter name of first file\n");
   gets(file1);

   printf("Enter name of second file\n");
   gets(file2);

   printf("Enter name of file which will store contents of the two files\n");
   gets(file3);

   fs1 = fopen(file1, "r");
   fs2 = fopen(file2, "r");

   if(fs1 == NULL || fs2 == NULL)
   {
      perror("Error ");
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }

   ft = fopen(file3, "w"); // Opening in write mode

   if(ft == NULL)
   {
      perror("Error ");
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }

   while((ch = fgetc(fs1)) != EOF)
      fputc(ch,ft);

   while((ch = fgetc(fs2)) != EOF)
      fputc(ch,ft);

   printf("The two files were merged into %s file successfully.\n", file3);

   fclose(fs1);
   fclose(fs2);
   fclose(ft);

   return 0;
}

我的 read.c - 读取文件

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

int main(int argc, char **argv) {
    char c[1000];
    FILE *fptr, anotherfptr;

    if ((fptr = fopen("myfile1.txt", "r")) == NULL) {
        printf("Error! opening file");
        exit(1);         
    }

    // reads text until newline 
    fscanf(fptr,"%[^\n]", c);
    printf("Data from the file:\n%s", c);
    fclose(fptr);

    if ((fptr = fopen("myfile2.txt", "r")) == NULL) {
        printf("Error! opening file");
        exit(1);         
    }

    // reads text until newline 
    fscanf(anotherfptr,"%[^\n]", c);
    printf("Data from the file:\n%s", c);
    fclose(anotherfptr);
    return 0;
}

我的问题是我如何解决这个简单程序的逻辑。我真的被卡住了。

任何帮助/澄清将不胜感激。

标签: clogic

解决方案


在这种情况下,您需要创建一个程序,该程序应该知道“myfile1.txt”或“myfile2.txt”的内容/大小,以便在读取“myfile3.txt”时为第二个内容显示*。

我不喜欢为每个任务创建单独的 c 程序,而是将其用作单个程序中的函数。

进入逻辑:掩蔽是您正在搜索的内容。基本上它被用作密码屏蔽。(您可能在任何网站上输入密码时看到 *。)。在您的情况下,您希望在不实际更改文件内容的情况下将内容显示为 *。

在以下文档中了解如何对密码进行屏蔽:

https://www.geeksforgeeks.org/print-in-place-of-characters-for-reading-passwords-in-c/


推荐阅读