为什么该程序不写入result.txt?

时间:2019-02-19 16:40:17

标签: c

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

int main(int argc, char *argv[]){
    FILE *result = fopen("result.txt", "w");
    int cont = 0, i, n;
    char *s1;
    if(argc != 1)
        printf("Numero parametri non corretto\n");
    else{
        FILE *fp = fopen("test_suite.txt", "r");
        while(fscanf(fp, "%s %d", s1, &n) == 2)
            cont++;
        rewind(fp);
        for(i=0; i<cont; i++){
            fscanf(fp, "%s %d", s1, &n);
            int *a = (int*) calloc(n, sizeof(int));
            char *s2;
            strcpy(s2, s1);
            finput_array(strcat(s1,"_input.txt"), a, n);
            strcpy(s1,s2);
            bubblesort(a, n);
            foutput_array(strcat(s1, "_output.txt"), a, n);
            strcpy(s1,s2);
            int *oracle = (int*) calloc(n, sizeof(int));
            finput_array(strcat(s1, "_oracle.txt"), oracle, n);
            strcpy(s1,s2);

            if(confronta_array(a, oracle, n))
                fprintf(result, "%s PASS\n", s1);
            else
                fprintf(result, "%s FAIL\n", s1);
            free(a);
            free(oracle);
        }
        fclose(fp);
    }
    fclose(result);
}

这是“ vettore.c”功能:

void bubblesort(int a[], int n){
int i, j;
  for(i = 0 ; i < n - 1; i++)
  {
    for(j = 0 ; j < n - i - 1; j++)
    {
      if (a[j] > a[j+1]) /* For decreasing order use < */
      {
        scambia(&a[j], &a[j+1]);
       }
      }
     }
}

void finput_array(char *file_name, int a[], int n){
    FILE *fd = fopen(file_name, "r");
    if(fd == NULL)
        printf("Errore in apertura del file %s\n", file_name);
    else{
        for(int i=0; i<n; i++)
            fscanf(fd, "%d", &a[i]);
        fclose(fd);
    }
}

void foutput_array(char *file_name, int a[], int n){
    int i;
    FILE *fd;

    fd = fopen(file_name, "w");
    if(fd == NULL)
        printf("Errore in apertura del file %s\n", file_name);
    else{
        for(i=0; i<n; i++)  
            fprintf(fd, "%d\n", a[i]);
        fclose(fd);
    }
}

int confronta_array(int a[], int b[], int n){
    int i=0;

    while(i<n && a[i] == b[i])
        i++;

    return (i==n) ? 1 : 0;
}

程序未在“ result.txt”中写入任何内容。为什么?

它应该写那些句子。

1 个答案:

答案 0 :(得分:1)

在这一部分:

while(fscanf(fp, "%s %d", s1, &n) == 2) cont++;

也许“续”没有增加,因为“ test_suite.txt”没有“ fscanf”格式要求。