【问题标题】:C program to put the output into a new file将输出放入新文件的 C 程序
【发布时间】:2020-09-04 03:47:59
【问题描述】:

所以我已经解决了一个问题,我必须从输入文件中找到素数并将这些素数保存在名为 output.txt 的输出文件中。但是如果输入文件中没有素数,我'必须在输出文件中写“没有找到素数”。所以当我完成没有素数的代码时,它显示没有找到素数 5-6 次,我只希望它出现 1 次。我的错了吗?我完全是菜鸟

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

int Primecheck(const int number);

int main()
{
    FILE* Number, 
        * Prime_N;

    int num;
    char sentence[50] = "No prime numbers found";
    int length = strlen(sentence);
    int i;

    Number = fopen("input.txt", "r");
    Prime_N = fopen("output.txt", "w");

    if (Number == NULL || Prime_N == NULL)
    {
        printf("Unable to open file.\n");
        exit(EXIT_FAILURE);
    }

    printf("File opened and Reading Done \n\n");

    while (fscanf(Number, "%d", &num) != -1)
    {
        if (Primecheck(num) == 1)
            fprintf(Prime_N, "%d\n", num);
        else
            for (i = 0; i < length; i++)
            {
                fputc(sentence[i], Prime_N);
            }
    }

    fclose(Number);
    fclose(Prime_N);

    printf("Overwrite Success.");

    return 0;
}

int Primecheck(const int number)
{
    int i;

    if (number < 0)
        return 0;

    for (i = 2; i <= number / 2; i++)
    {

        if (number % i == 0)
        {
            return 0;
        }
    }
    return 1;
}

【问题讨论】:

  • 复习一下你的逻辑。这将清楚你错在哪里。或者使用调试器来单步调试程序。
  • 如果你读到一个数字但它不是质数会发生什么?
  • 不要在循环中写入该消息,而是在输出结果时设置一个标志。然后循环之后,检查flag是否有结果,输出fclose之前的消息。
  • sentence 定义为char sentence[] = "No prime numbers found";。那么length 将是sizeof sentence - 1。并将其存储为fprintf 而不是循环和fputc

标签: c file pointers


【解决方案1】:

而不是

else
    for(i=0;i<length;i++)
    {
        fputc(sentence[i] ,Prime_N);
    }

你可以写

else
    printf("No prime numbers found");

【讨论】:

    【解决方案2】:

    好吧,正如我从您的代码中看到的那样,每次您从文件中读取一个数字时,您都会检查素数是写它还是写sentence。所以当然你会得到多个输出, 您应该将数字写入char* 然后检查从Number 读取后是否更改并检查素数是否在循环后将数字写入char* 只需检查您存储素数的char* 的长度是否如果改变了就写吧,否则写你的句子...完成

    类似的东西

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    
    typedef struct {
        char* data;
        size_t size;
        size_t capacity;
    } string;
    
    void init_string(string* s, size_t size){
        s->data = (char*)malloc(sizeof(char) * size);
        s->capacity = size;
        s->size = 0;
    }
    
    void append(string* s,const char* str){
        if(s->capacity - s->size < strlen(str)){
            s->data = (char*)realloc(s->data, sizeof(char)* 4 * strlen(str));
        }strcat(s->data, str);
    }
    
    void free_string(string* s){
        free(s->data);
    }
    
    
    int is_odd(int n){
        return n%2;
    }
    
    int main(){
        int num[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
    
        string str;
        init_string(&str,1);
        char* snum;
        for(int i=0; i<18; i++){
            if(!is_odd(i)){
                sprintf(snum,"%d",num[i]);
                append(&str, snum);
                append(&str, "\n");
            }
            //printf(snum);
            //strcat(str, snum);
        }
        printf("%s",str.data);    
        free_string(&str);
        //printf(str);
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-03
      • 1970-01-01
      • 2011-04-25
      • 2014-12-21
      • 2010-10-09
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多