Программа в C не записывает в файл и имеет предупреждение с -std = c99 - PullRequest
1 голос
/ 09 июля 2020

У меня проблема. Я пишу программу, которая читает и записывает матрицы, векторы и скаляры, но у меня проблема. Это мой код zaliczenie_ c. c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "operacje_io.h"
#define MAX_NAME 20
#define MAX_ARRAY_DIM 50


typedef struct Vector {
    char keyWordV;
    char name[MAX_NAME];
    int dim;
    double data[MAX_ARRAY_DIM];
} Vector;

typedef struct Scalar {
    char keyWordS;
    char name[MAX_NAME];
    double data;
} Scalar;

typedef struct Matrix {
    char keyWordM;
    char name[MAX_NAME];
    int dimX;
    int dimY;
    double data[MAX_ARRAY_DIM][MAX_ARRAY_DIM];
} Matrix;

typedef struct Element {
    char type;
    union {
        Scalar s;
        Vector v;
        Matrix m;
    } elem;
} Element;

int main (int argc, char *argv[])
{    
    FILE *wz, *wc;  

    if (argc != 3) {                                
    printf("Wrong arguments number\n");
    printf("I should run this way:\n");
    printf("%s source\n",argv[0]);
    exit(1);
    }
    
    if( (wz= fopen(argv[1],"r")) == NULL) {
        printf("Open error %s\n", argv[1]);
        exit(1);
    }
    if( (wc= fopen(argv[2], "w")) == NULL) {
    printf("Open error %s\n", argv[2]);
    exit(2);
    }


    Element elements[10];
    for (int i = 0; i < 6; i++) {
        readElement(wz, &elements[i]);
    }
    fclose(wz);
    fclose(wz);
    
    return 0;

}

operation_io. c

#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME 20
#define MAX_ARRAY_DIM 50

typedef struct Vector {
    char keyWordV;
    char name[MAX_NAME];
    int dim;
    double data[MAX_ARRAY_DIM];
} Vector;

typedef struct Scalar {
    char keyWordS;
    char name[MAX_NAME];
    double data;
} Scalar;

typedef struct Matrix {
    char keyWordM;
    char name[MAX_NAME];
    int dimX;
    int dimY;
    double data[MAX_ARRAY_DIM][MAX_ARRAY_DIM];
} Matrix;

typedef struct Element {
    char type;
    union {
        Scalar s;
        Vector v;
        Matrix m;
    } elem;
} Element;

void readScalar(FILE *file, Scalar* s) {
    fscanf(file, " %s ", &s->name[0]);
    fscanf(file, "%10lf", &s->data);
    }
    
void writeScalar(FILE *file, Scalar* s) {
    fprintf(file, "S");
    fprintf(file, " %s \n", s->name);
    fprintf(file, "%10.2lf \n", s->data);
    }

void showScalar(Scalar* s) {
    printf("S");
    printf(" %s \n", s->name);
    printf("%10.2lf \n", s->data);
    }
    
void readVector(FILE *file, Vector* v) {
    fscanf(file, " %s %d ", &v->name[0], &v->dim);
    for (int j = 0; j < v->dim; ++j) {
        fscanf(file, "%10lf", &v->data[j]);
    }
}

void writeVector(FILE *file, Vector* v) {
    fprintf(file, "V");
    fprintf(file, " %s %d \n", v->name, v->dim);
    for (int j = 0; j < v->dim; ++j) {
        fprintf(file, "%10.2lf ", v->data[j]);
    }
    fprintf(file, "\n");
}

void showVector(Vector* v) {
    printf("V");
    printf(" %s %d \n", v->name, v->dim);
    for (int j = 0; j < v->dim; ++j) {
        printf("%10.2lf ", v->data[j]);
    }
    printf("\n");
}

void readMatrix(FILE *file, Matrix* m) {
    fscanf(file, " %s %d %d", &m->name[0], &m->dimX, &m->dimY);
    for (int i = 0; i < m->dimX; ++i)
        for (int j = 0; j < m->dimY; ++j)
            fscanf(file, "%10lf", &m->data[i][j]);
}

void writeMatrix(FILE *file, Matrix* m) {
    fprintf(file, "M");
    fprintf(file, " %s %d %d \n", m->name, m->dimX, m->dimY);
    for (int i = 0; i < m->dimX; ++i) 
    {
        for (int j = 0; j < m->dimY; ++j)
        {
            fprintf(file, "%10.2lf", m->data[i][j]);
        }
        fprintf(file, "\n");
    }
}

void showMatrix(Matrix* m) {
    printf("M");
    printf(" %s %d %d \n", m->name, m->dimX, m->dimY);
    for (int i = 0; i < m->dimX; ++i)
    {
        for (int j = 0; j < m->dimY; ++j)
        {
            printf("%10.2lf", m->data[i][j]);
        }
        printf("\n");
    }
}
void readElement(FILE *file, Element* e) {
    char type;
    fscanf(file, " %c ", &type);
    switch (type) {
    case 'S':
        e->type = 'S';
        readScalar(file, &e->elem.s);
        writeScalar(file, &e->elem.s);
        showScalar(&e->elem.s);
        break;
    case 'V':
        e->type = 'V';
        readVector(file, &e->elem.v);
        writeVector(file, &e->elem.v);
        showVector(&e->elem.v);
        break;
    case 'M':
        e->type = 'M';
        readMatrix(file, &e->elem.m);
        writeMatrix(file, &e->elem.m);
        showMatrix(&e->elem.m);
        break;
    default:
        fputs("Error: unknown token!\n", stderr);
        exit(0);
    }
}

operation_io.h


При компиляции вот так

gcc -c -std=c99 -pedantic -c zaliczenie_c.c

Получаю ошибки

zaliczenie_c.c:14:3: warning: redefinition of typedef ‘Vector’ [-Wpedantic]
 } Vector;
   ^~~~~~
In file included from zaliczenie_c.c:4:0:
operacje_io.h:5:23: note: previous declaration of ‘Vector’ was here
 typedef struct Vector Vector;
                       ^~~~~~
zaliczenie_c.c:20:3: warning: redefinition of typedef ‘Scalar’ [-Wpedantic]
 } Scalar;
   ^~~~~~
In file included from zaliczenie_c.c:4:0:
operacje_io.h:4:23: note: previous declaration of ‘Scalar’ was here
 typedef struct Scalar Scalar;
                       ^~~~~~
zaliczenie_c.c:28:3: warning: redefinition of typedef ‘Matrix’ [-Wpedantic]
 } Matrix;
   ^~~~~~
In file included from zaliczenie_c.c:4:0:
operacje_io.h:6:23: note: previous declaration of ‘Matrix’ was here
 typedef struct Matrix Matrix;
                       ^~~~~~
zaliczenie_c.c:37:3: warning: redefinition of typedef ‘Element’ [-Wpedantic]
 } Element;
   ^~~~~~~
In file included from zaliczenie_c.c:4:0:
operacje_io.h:7:24: note: previous declaration of ‘Element’ was here
 typedef struct Element Element;
                        ^~~~~~~

Но когда компилирую вот так

gcc -c -pedantic -c zaliczenie_c.c

Все ок.

Вдобавок . Моя программа показывает результаты на экране очень хорошо, но моя программа не записывает в файл (fprintf), и я не знаю, почему

1 Ответ

1 голос
/ 09 июля 2020

Переместите определения общих типов в файл .h.

файл .h

#ifndef __MY_H 
#define __MY_H

#define MAX_NAME 20
#define MAX_ARRAY_DIM 50


typedef struct Vector {
    char keyWordV;
    char name[MAX_NAME];
    int dim;
    double data[MAX_ARRAY_DIM];
} Vector;

typedef struct Scalar {
    char keyWordS;
    char name[MAX_NAME];
    double data;
} Scalar;

typedef struct Matrix {
    char keyWordM;
    char name[MAX_NAME];
    int dimX;
    int dimY;
    double data[MAX_ARRAY_DIM][MAX_ARRAY_DIM];
} Matrix;

typedef struct Element {
    char type;
    union {
        Scalar s;
        Vector v;
        Matrix m;
    } elem;
} Element;


#endif

и удалите их из файла. c.

struct и Объявления объединений не похожи на прототипы функций.

...