Возникли проблемы с копированием информации из одного файла в другой в C

#c #copy #switch-statement #transfer #fgetc

#c #Копировать #switch-инструкция #передача #fgetc

Вопрос:

Был назначен проект для передачи информации из одного файла для отображения депозита или снятия, поскольку D представляет депозит, а C представляет C.Проблема, с которой я сталкиваюсь, заключается в файле «transactions.txt » равно НУЛЮ, и я не могу понять, почему.

Также правильна ли эта часть моей функции?

 float D(float balance, float deposit) {
    printf("and the amount is:");
    fputc(read, fpout, amp;deposit);
    printf("The balance is: %f", balance);
    return 0;
}
  

должен ли я использовать fputc или fscanf? Я хочу, чтобы он отображал выходные данные, а также сохранял в processed.txt досье.

 #include <stdio.h>

FILE *fpin;
FILE *fpout;

float deposit;
float withdraw;
float balance;
float read;

//Function that asks the user for deposit information then prints balance   deposit.
float D(float balance, float deposit) {
    printf("and the amount is:");
    fputc(read, fpout, amp;deposit);
    printf("The balance is: %f", balance);
    return 0;
}

//Function that asks user for the amount they want withdrawn, then prints balance - withdrawal amount.
float C(float balance, float withdraw) {
    printf("and the amount is:");
    fputc(read, fpout, amp;withdraw);
    printf("The balance is %f", balance);
    return 0;
}    



int main(void) {

    char code;

    deposit = 0;
    withdraw = 0;
    balance = 0;

    //formula for balance.
    balance = balance   deposit;
    balance = balance - withdraw;

    //opens documents for inputs.
    fpin = fopen("transactions.txt", "r");
    fpout = fopen("processed.txt", "w");


    printf("Welcome to Chris's Checking Account Tracer Program");
    printf("n------------------------------------------------------n");

    //If file is null displays cannot open file. Else runs program.
    if ((fpin == NULL)) {
        printf("Cannot open file.");
    }
    else {

        //Runs a loop until the end of the file is reached.
        while (fpin!=EOF) {



        printf("The transaction is a: ");
        //Brings in information from "transaction.txt" input of code to output.
        read = getc(fpin);

    //Writes the output from transaction.txt to processed.txt
    fputc(read, fpout);

    // If user enters D or d runs 'D' Function.
    switch (code) {
    case 'D': case 'd':
        D(balance, deposit);
        break;

        // If user enters C or c runs 'C' Function.
    case 'C': case 'c':
        C(balance, withdraw);
        break;
        // If user enters anything else other then D or C prints "Not responding correctly".
    default:
        printf("Not responding Correctly.");
        break;
    }
    }
}
//closes the text documents.
fclose(fpin);
fclose(fpout);
return (0);
}
  

Комментарии:

1. Предположительно, файл «transactions.txt » который вы открываете для чтения, уже существует, если это так, то он должен находиться по пути, отличному от текущего пути, известного исполняемому файлу.

2. Откуда он открывает файл?

3. IDK это может зависеть от того, как вы запускаете исполняемый файл. Почему бы сначала не открыть файл записи для вывода и выяснить, где он был создан (используя необычное имя)?

4. Повторите ваше последующее редактирование, fputc принимает 2 аргумента, вы указали 3, почему вы спрашиваете об ошибке, которую поймал компилятор? Кроме того, очень оптимистично передавать a float в функцию вывода символов.

5. «должен ли я использовать fputc или fscanf?» Ммм … fputc() для вывода, fscanf() для ввода… Они даже близко не взаимозаменяемы…