如何正确将元素打印到文件中?

问题描述 投票:0回答:1

当程序进入yazici函数时,我无法将urun的元素正确打印到文件中。

我可以打印satin_alim_kg,toplam_ucret和barkodsorgu。我曾尝试在全球范围内制作结构,但不能这样做。

/*
** So overwhelming. Make your code attractive and much more readable. No one will get his hands in it. Use proper spacing in between statements in your program.
** Missed closing brace at the end after return 0; statement.
*/

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

// Structure templates
struct urun_bilgileri{
    int barkod;
    char urun_adi[50];
    char kategori[50];
    float kilogram_fiyati;
};


struct fatura_bilgileri{
    char* satin_alim_tarihi;
    float alinan_kg_miktari;
    float odenen_ucret;
};

// Global Variables
struct fatura_bilgileri *fatura;
struct urun_bilgileri *urun;
int i, barkodsorgu, faturasayac = 0;
char alinan_urun, alinan_urun_kategorisi;
float satin_alim_kg, toplam_ucret;

// Function Declarations & Definitions
void tarayici(){

    int c;
    FILE *fpbarkod = fopen("barkod.txt", "r");

    if (fpbarkod)
    {
        while ((c = getc(fpbarkod)) != EOF)
            putchar(c);
        rewind(fpbarkod);
        fclose(fpbarkod);
    }
}

void yazici(){

    FILE *fpfatura;
    time_t t;

    // Why are you allocating memory twice to fatura and urun, you haven't freed it yet in the main()?

    time(&t);
    (fatura+faturasayac)->satin_alim_tarihi = ctime(&t);

    if( (fpfatura = fopen("fatura.txt", "w+") ) == NULL)
    {
        perror("fatura.txt");
        exit(3);
    }

    fprintf(fpfatura, "Islem tarihi:%sAlinan urun:%s Barkod Numarasi:%d Urunun kategorisi:%s Alinan miktar:%.3fKG Odenen ucret:%.2f\n\n", (fatura+faturasayac)->satin_alim_tarihi, (urun+i)->urun_adi,barkodsorgu, (urun+i)->kategori, satin_alim_kg, toplam_ucret);
    printf("%f", toplam_ucret);
    fclose(fpfatura);
    faturasayac++;
}

// main()
int main()
{
    FILE *fpbarkod;
    int urun_sayac = 0, ana_menu_secim;

    if( ( fatura = (struct fatura_bilgileri *)malloc( sizeof(struct fatura_bilgileri) ) ) == NULL) // Why are you redeclaring struct fatura_bilgileri *fatura
    {
        printf("Memory Unavailable.\n"); // Check for the errors while allocating memory.
        exit(4);
    }
    if( ( urun = (struct urun_bilgileri*)malloc( sizeof(struct urun_bilgileri) ) ) == NULL) // Why are you redeclaring struct urun_bilgileri *urun
    {
        printf("Memory Unavailable.\n"); // Check for the errors while allocating memory.
        exit(5);
    }

    printf("MARKET OTOMATIK ODEME SISTEMI\n");
    baslangic:
        printf("------------------\n");
        printf("1-URUN EKLE\n");
        printf("2-URUN KATALOGU\n");
        printf("3-CIKIS\n");
        printf("------------------\n");
        printf("Lutfen islem yapmak istediginiz secenegi seciniz:");
        scanf("%d", &ana_menu_secim);
        switch(ana_menu_secim){
            case 1:
                printf("\nLutfen urunun adini giriniz:");
                scanf("%s", (urun+urun_sayac)->urun_adi);
                printf("\nLutfen urunun kategorisini giriniz:");
                scanf("%s", (urun+urun_sayac)->kategori);
                printf("\nLutfen urunun kilogram fiyatini giriniz(TL):");
                scanf("%f", &(urun+urun_sayac)->kilogram_fiyati);
                srand(time(NULL));
                (urun+urun_sayac)->barkod = rand() % (1000 + 1 - 100) + 100;
                sleep(1);
                printf("\n\nUrun Adi:%s\nUrun kategorisi:%s\nUrun Kilogram fiyati:%.2f TL\nUrun barkod numarasi:%d\n",(urun+urun_sayac)->urun_adi,(urun+urun_sayac)->kategori,(urun+urun_sayac)->kilogram_fiyati,(urun+urun_sayac)->barkod);
                if( (fpbarkod = fopen("barkod.txt","a+") ) == NULL)
                {
                    perror("barkod.txt");
                    exit(6);
                }
                fprintf(fpbarkod, "Urun Adi:%s Kategori:%s Fiyati(kg):%.2f TL Barkod No:%d\n", (urun+urun_sayac)->urun_adi, (urun+urun_sayac)->kategori, (urun+urun_sayac)->kilogram_fiyati, (urun+urun_sayac)->barkod);
                rewind(fpbarkod);
                fclose(fpbarkod);
                sleep(1);
                printf("\n\nUrun ekleme basarili\n\n");
                urun_sayac++;
                goto baslangic; // Avoid use of goto as far as possible. It makes your program unreadable, ambiguous and unstructured.
            case 2:
                printf("-------------------KATALOG-------------------\n");
                tarayici();
                printf("---------------------------------------------\n");
                geribarkodsorgu:
                    printf("\nLutfen satin almak istediginiz urunun barkod numarasini giriniz:");
                    scanf("%d" , &barkodsorgu);
                    sleep(1);
                    for(i = 0; i <= urun_sayac; i++){
                        if(barkodsorgu == (urun+i)->barkod){
                            printf("\n%s kilogram Fiyati:%.2f\n", (urun+i)->urun_adi, (urun+i)->kilogram_fiyati);
                            goto satinalimkg;
                        }
                        else if(i == urun_sayac){
                            printf("\n\nYANLIS BARKOD NUMARASI\n");
                            goto geribarkodsorgu;
                        }
                        else
                            continue;
                    }
                satinalimkg:
                    sleep(1);
                    printf("\nLutfen satin almak istediginiz miktari giriniz(kg):");
                    scanf("%f" , &satin_alim_kg);
                    if(satin_alim_kg <= 0){
                        printf("\n\nLutfen satin almak istediginiz miktari dogru giriniz\n");
                        goto satinalimkg;
                    }
                toplam_ucret = (satin_alim_kg) * ((urun+i)->kilogram_fiyati);
                printf("Toplam ucret:%.2f", toplam_ucret);
                yazici();
                break;
            case 3:
                return 0;
            default:
                printf("\nLutfen menudeki seceneklerden birini seciniz.\n\n");
                sleep(1);
                goto baslangic;
                break;
        }
    // If you are done with fatura and urun free them.
    free(fatura);
    free(urun);
    return 0;
}

如何正确将元素打印到文件中?我应该在打印机功能中重新定义结构吗?

c pointers structure
1个回答
0
投票

如果需要在文件中插入一些内容,则需要定义一个char *值变量,并在其中填充一些内容。

fd = open("barkod.txt",O_CREAT|O_RDWR,0666);
    file = fdopen(fd,"r+");
    ret = fprintf(file,"%s ",values);
    if(ret == -1 ) {
    fprintf(stderr,"Error in fprintf");
    exit(-1)}
    fflush(file);

相反,如果您需要从文件中读取一些信息,则可以使用以下功能:

#define SIZE 4096

char * malloc_buff(int dim){
    char *buf;
    buf=malloc(sizeof(char)*dim);
    if(buf==NULL){
        perror("Errore in malloc");
    }
    return buf;
}

char * lecture_file(){
    FILE* fd;
    char* disponibili;
    disponibili=malloc_buff(SIZE);
    errno=0;
    fd=fopen("bardok.txt","r+");
    if(errno!=0){
        fprintf(stderr,"error in open file\n");
        exit(-1);
    }
    fread(disponibili,sizeof(char),SIZE,fd);
    if(fclose(fd)){
        fprintf(stderr,"error in close\n");
        exit(-1);
    }
    return disponibili;
}
© www.soinside.com 2019 - 2024. All rights reserved.