【问题标题】:How can I copy some strings from file to another using c programming如何使用 c 编程将一些字符串从文件复制到另一个文件
【发布时间】:2021-12-30 22:57:26
【问题描述】:

我有这个代码:

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

int main()
{

    FILE* ptr = fopen("data.txt","r");
    char filename[100];
    if (ptr==NULL)
    {
        printf("no such file.");
        return 0;
    }
 
    char buf[100];
    while (fscanf(ptr,"%*s %*s %s ",buf)==1)
        printf("%s\n", buf);



printf("Create a file \n");
    scanf("%s", filename);
  
    fptr2 = fopen(filename, "w");
    if (fptr2 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }
 


    c = fgetc(fptr1);
    while (c != EOF)
    {
        fputc(c, fptr2);
        c = fgetc(fptr1);
    }
  
    printf("\nContents copied to %s", filename);
  
    fclose(fptr1);
    fclose(fptr2);
    return 0;
}



}

它将完整内容从一个文件复制到另一个文件。我只需要复制以5 作为最后一个字符的字符串(3 列)

例如 Data.txt 看起来像这样:

Alex 10B 4
John 10A 3
Kate 10C 5

在我将在执行期间创建的文件中只能复制Kate 10C 5 字符串。我已经尝试了几个小时,但我不知道该怎么做。你能帮帮我吗?

【问题讨论】:

    标签: c string file copy multiple-columns


    【解决方案1】:

    在每一行的末尾有一个换行符,(\n) 你可以用它来逐行读取并只复制你想要的:

    FILE* dest = fopen("out.txt", "w+"); // supressed null check for simplicity
    
    char buf[100];
    
    char* char_to_find;
    
    // parse line by line
    while (fscanf(ptr, " %99[^\n]", buf) == 1){
         
        char_to_find = buf;
        
        // reach the end of the line
        while(*char_to_find){
            char_to_find++;
        }
        
        //move one back
        char_to_find--;
        
        // if it's 5 save, if not move on
        if(*char_to_find == '5' && *(char_to_find - 1) == ' '){
                        
            fputs(buf, dest);
        }
    }
    

    Live demo

    【讨论】:

    • 我需要复制整个字符串,其中第三个 == 5
    • @stack 我添加了一个可能的解决方案。
    • 谢谢!我已经编辑了您的代码,以便有机会创建用于复制的新文件,而不是常量 out.txt
    • 我已经发布了完整答案
    • @stack 做得好,我只是做了一个工作示例,您可以并且应该根据您的需要调整代码并尽可能改进它。
    【解决方案2】:

    问题在于函数调用

    while (fscanf(ptr,"%*s %*s %s ",buf)==1)

    使用来自输入流的输入,使其不再可用于复制。您只保存了最后一个字段的内容,但所有其他数据都丢失了。

    我建议您通过在循环中调用函数fgets 一次将一行读入内存缓冲区。这样,您将在每次循环迭代中处理一行输入,并将保存整行的内容。

    在每次循环迭代中,您可以在此内存缓冲区上使用sscanf 来确定第三个字段是否具有所需的值,如果是,则将整行复制到输出文件。否则,你什么也不做,继续下一行(即下一个循环迭代)。

    char line[100];
    
    //process one line of input per loop iteration
    while ( fgets( line, sizeof line, input_file ) != NULL )
    {
        char third_field[20];
    
        if (
            //third field was successfully extracted
            sscanf( line, "%*s%*s%19s", third_field ) == 1
    
            &&
    
            //third field contains the string "5"
            strcmp( third_field, "5" ) == 0
        )
        {
            //copy entire line to output file
            fputs( line, output_file );
        }
    }
    

    【讨论】:

      【解决方案3】:
      #include <stdio.h>
      #include <stdlib.h>
        
      int main()
      {
      
          FILE* ptr = fopen("data.txt","r");
          char filename[100];
          if (ptr==NULL)
          {
              printf("no such file.");
              return 0;
          }
      
          printf("Create a file \n");
          scanf("%s", filename);
        
       
          FILE* dest = fopen(filename, "w+"); // check for null like above
          
      
      
      
          char buf[100];
          
          char* char_to_find;
          
          while (fscanf(ptr,"%99[^\n] ", buf) == 1){
               
              char_to_find = buf;
              
              while(*char_to_find != 0){
                  char_to_find++;
              }
              
              char_to_find--;
              
              if(*char_to_find == '5'){
                  
                  printf("%s\n", buf); // test ptint
                  fputs(buf, dest);
              }
          }
      }
      

      【讨论】:

      • 请注意,检查 5 之前的空间很重要,否则 55 或 65 或其他任何内容也会被保存。 if(*char_to_find == '5' &amp;&amp; *(char_to_find - 1) == ' ') 除非你确定这些总是个位数。
      猜你喜欢
      • 2023-01-16
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多