我是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;
}


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

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

最佳答案

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

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

顺理成章:屏蔽是您要寻找的。基本上,它用作密码屏蔽。 (您可能在任何站点中输入密码时都看到*。)。在您的情况下,您希望将内容显示为*,而无需实际更改文件中的内容。

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

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

关于c - 如何修改文件内容?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58004898/

10-11 21:57
查看更多