简体   繁体   English

C 中的程序不写入文件并使用 -std=c99 发出警告

[英]Program in C dont write into the file and have warning with -std=c99

I have a problem.我有个问题。 I write a program that reads and writes matrices, vectors and scalars, but I have a problem.我编写了一个读写矩阵、向量和标量的程序,但是我遇到了一个问题。 This is my code zaliczenie_c.c这是我的代码 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 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 operation_io.h


When I compile like that当我这样编译时

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

I get errors我收到错误

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;
                        ^~~~~~~

But when I compile like that但是当我这样编译时

gcc -c -pedantic -c zaliczenie_c.c

Everything is ok.一切都好。

In addition.此外。 My program show results on the screen very good, but my program dont write into the file (fprintf) and I dont know why我的程序在屏幕上显示结果很好,但我的程序没有写入文件(fprintf),我不知道为什么

Move common types definitions to the.h file.将常用类型定义移至 .h 文件。

.h file .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

and remove them from the.c file.并将它们从 .c 文件中删除。

struct and unions declarations are not like functions prototypes. struct 和 unions 声明不像函数原型。

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

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