Почему эта программа не записывает в result.txt? - PullRequest
0 голосов
/ 19 февраля 2019
#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 голосов
/ 19 февраля 2019

В этой части:

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

Возможно, "cont" не увеличивается, потому что у "test_suite.txt" нет требований к формату "fscanf".

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...