简体   繁体   English

想要一个 C 程序写入文件,读取它并将偶数和奇数存储在单独的文件中

[英]Want a C program to write into a file, read it and store even and odd numbers in a separate file

I tried to make the following code.我试图制作以下代码。

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

void main()
{
  FILE *fp1,*fp2,*fp3;
  int n,i,size;
  printf("Enter no.of digits: ");
  scanf("%d",&size);
  fp1=fopen("NUMBER.txt","w");
  printf("Enter the numbers: \n");
  for(i=0;i<size;i++)
  {
    fflush(stdin);
    scanf(" %d",&n);
    fputc(n,fp1);
  }
  fclose(fp1);
  
  fp1=fopen("NUMBER.txt","r");
  fp2=fopen("EVEN.txt","w");
  fp3=fopen("ODD.txt","w");
  while((n=fgetc(fp1))!=EOF)
  {
    if(n%2==0)
    fputc(n,fp2);
    else
    fputc(n,fp3);
  }
  fclose(fp1);
  fclose(fp2);
  fclose(fp3);

  fp1=fopen("NUMBER.txt","r");
  fp2=fopen("EVEN.txt","r");
  fp3=fopen("ODD.txt","r");
  
  printf("The content of number file are: ");
  while((n=fgetc(fp1))!=EOF)
  printf(" %d",n);
  
  printf("\nThe content of even file are: ");
  while((n=fgetc(fp2))!=EOF)
  printf(" %d",n);
  
  printf("\nThe content of odd file are: ");
  while((n=fgetc(fp3))!=EOF)
  printf("  %d",n);

  fclose(fp1);
  fclose(fp2);
  fclose(fp3);
}

The problem I face is that the contents of the files are in hex or binary.我面临的问题是文件的内容是十六进制或二进制。 I want it to be readable with text editor not a hex editor.我希望它可以用文本编辑器而不是十六进制编辑器来阅读。 The other problem I face is the scanf() doesn't accept 3 digit numbers.我面临的另一个问题是 scanf() 不接受 3 位数字。 The output is given below. output 如下所示。

Enter no.of digits: 5 Enter the numbers: 123 34 456 67 789 The content of number file are: 123 34 200 67 21 The content of even file are: 34 200 The content of odd file are: 123 67 21输入位数:5 输入数字:123 34 456 67 789 数字文件内容为:123 34 200 67 21 偶数文件内容为:34 200 奇数文件内容为:123 67 21

I tried to write the following code:我尝试编写以下代码:

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

int main()
{
    FILE *fp1,*fp2,*fp3;
    int size,n,a,b;
    printf("Enter the size: ");
    scanf("%d",&size);
    fp1=fopen("numbers1.txt","w");
    fp2=fopen("even1.txt","w");
    fp3=fopen("odd1.txt","w");
    
    printf("Enter the integers: ");
    
    for(int i=0;i<size;i++)
    {
      scanf("  %d",&n);
      //putc(n,fp1); reads as 12, 234 etc but digits in file is not visible
      fprintf(fp1," %d\n",n);//reads 12, 234 as 12234 but digits in file are visible
      
    }
    
    fclose(fp1);
    fp1=fopen("numbers1.txt","r");
    
    n=getc(fp1);
    
    while(n!=EOF)
    {
        if(n%2==0)
        putc(n,fp2);
        
        else if(n%2==1)
        putc(n,fp3);
        
        n=getc(fp1);
    }

    
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);
    
    
    return 0;
}

In the above code the content in the files are in text, but the odd and even files read char by char.在上面的代码中,文件中的内容是文本,但是奇偶文件是逐字符读取的。 The contents of odd, even and number files are given blow.奇数、偶数和数字文件的内容都给出了打击。

odd file: 133577奇数档案:133577

even file: 2 4 46 6 8偶数文件:2 4 46 6 8

Number file: 123 34 456 67 78号码文件:123 34 456 67 78

Please help me请帮我

It is almost always a bad idea to require the number of data points to be specified before the data is given.在给出数据之前要求指定数据点的数量几乎总是一个坏主意。 Generally, the only reason to do that is to simplify the code so that you don't need to grow data structures.通常,这样做的唯一原因是简化代码,这样您就不需要增长数据结构。 In this case, however, you are writing all the data to a file so you don't even need to do that, since the file provides the storage for you.但是,在这种情况下,您正在将所有数据写入文件,因此您甚至不需要这样做,因为该文件为您提供了存储空间。

To write human readable integers, you need to format the data.要编写人类可读的整数,您需要格式化数据。 The most common way to do that is with printf .最常见的方法是使用printf There are a lot of ways to do what you're asking, and this is just one idea:有很多方法可以满足您的要求,这只是一个想法:

#include <limits.h>                                                                
#include <stdio.h>                                                                 
#include <stdlib.h>                                                                
                                                                                   
struct named_file {                                                                
        const char *name;                                                          
        FILE *fp;                                                                  
};                                                                                 
                                                                                   
void xfopen(struct named_file *f, const char *mode);                               
void xfclose(struct named_file *f);                                                
void display(const struct named_file *f);                                          
                                                                                   
int                                                                                
main(void)                                                                         
{                                                                                  
        struct named_file f[3];                                                    
        int n;                                                                     
                                                                                   
        f[0].name = "NUMBER.txt";                                                  
        f[1].name = "EVEN.txt";                                                    
        f[2].name = "ODD.txt";                                                     
                                                                                   
        xfopen(f, "w");                                                            
        while( scanf("%d", &n) == 1 ){                                             
                fprintf(f[0].fp, "%d\n", n);                                       
        }                                                                          
        xfclose(f);                                                                
                                                                                   
        xfopen(f + 0, "r");                                                        
        xfopen(f + 1, "w");                                                        
        xfopen(f + 2, "w");                                                        
                                                                                   
        while( fscanf(f[0].fp, "%d", &n) == 1 ){                                   
                FILE *out = n % 2 ? f[2].fp : f[1].fp;                             
                fprintf(out, "%d\n", n);                                           
        }                                                                          
        for( int i = 0; i < 3; i += 1 ){                                           
                xfclose(f + i);                                                    
                xfopen(f + i, "r");                                                
                display(f + i);                                                    
                xfclose(f + i);                                                    
        }                                                                          
}                                                                                  
     
void                                                                               
xfopen(struct named_file *f, const char *mode)                                     
{                                                                                  
        f->fp = fopen(f->name, mode);                                              
        if( f->fp == NULL ){                                                       
                perror(f->name);                                                   
                exit(EXIT_FAILURE);                                                
        }                                                                          
}                                                                                  
                                                                                   
void                                                                               
xfclose(struct named_file *f)                                                      
{                                                                                  
        if( fclose(f->fp) ){                                                       
                perror(f->name);                                                   
                exit(EXIT_FAILURE);                                                
        }                                                                          
}                                                                                  
                                                                                   
void                                                                               
display(const struct named_file *f)                                                
{                                                                                  
        int n;                                                                     
        printf("The contents of %s: ", f->name);                                   
        while( fscanf(f->fp, "%d", &n) == 1 ){                                     
                printf(" %d",n);                                                   
        }                                                                          
        putchar('\n');                                                             
}  

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

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