2016-08-08 58 views
0

Das Programm, an dem ich gerade arbeite, sollte Daten aus einer bereits vorhandenen Textdatei abrufen, die Daten bearbeiten und sie in eine neue Textdatei umschreiben. Ich habe das meiste davon zum Laufen gebracht, aber es speichert keine der gescannten Daten in meinem Array. Wenn ich meine print-to-file-Funktion aufruft (was meiner Meinung nach ziemlich gut funktioniert), wird nichts in die neue Datei gedruckt.Verwenden von fscanf zum Speichern von Floats und Strings in einem Array von Strukturen in C

Die ganze Code-Datei ist ziemlich lang, so dass ich nur ein bisschen davon einschließe. Lassen Sie es mich wissen, wenn Sie eine Klärung benötigen; Jede Beratung oder Ideen wäre eine große Hilfe! Vielen Dank im Voraus!

Die Datei aus einem Strang ziehen soll, hat einen Standardformat von

Name:\n\t Hours Worked: \n\tWeekly Pay: \n\tTaxes Paid: \n\tTake-home Wage: 

-Code

// in main (already declared and initialized all variables 
    printf("Please enter the name of the text file you'd like to read from: "); 
    scanf(" %s",fileName); 
    fp= (fopen(&fileName, "r+")); 
    if (fp ==NULL) 
    { 
     printf("Unable to open %s", fileName); 
     exit(1); 
    } 

    for(int i=0; i<10; i++){ 
     loadFromFile(&employeeInfo[i],fp); 
    } 
    fclose(fp); 
    break; 

int loadFromFile(struct Employee *employee,FILE *fp){ 
    static int employeeNumb; 
    fscanf(fp, " %*[^:]: %s", employee->name); 
    if ((*employee).name==feof(fp)) 
     printf("fscanf did not store properly"); 
    fscanf(fp," %*[^:]: %f", &employee->hoursWorked); 
    fscanf(fp," %*[^$]$ %f", &employee->weeklyPay); 
    fscanf(fp, " %*[^$]$ %f", &employee->taxesPaid); 
    fscanf(fp, " %*[^$]$ %*s"); 
    employeeNumb++; 
    return employeeNumb; 
} 

EDIT: hinzugefügt meine Druckfunktion

void saveToFile(struct Employee employee[], int employeeNumb){ 
FILE *fp; 
char newFile[40]; 
printf("Please enter the name of the text file you'd like to create or overwrite: "); 
scanf(" %s",newFile); 
fp= fopen(newFile, "w+");//r+ if do NOT want to overwrite or a+ if append 
if (fp ==NULL) 
{ 
    printf("Unable to open %s", newFile); 
    exit(1); 
} 
for(int i=0; i<employeeNumb; i++){ 
    printToFile(fp,&employee[i]); 
} 
fclose(fp);} 

void printToFile(FILE *fp,struct Employee *employee){ 
fprintf(fp, "\nName: %s", (*employee).name); 
fprintf(fp,"\n\tHours Worked: %g", (*employee).hoursWorked); 
fprintf(fp,"\n\tWeekly Wage: $%.2f", (*employee).weeklyPay); 
fprintf(fp, "\n\tTaxes Paid: $%.2f", (*employee).taxesPaid); 
fprintf(fp, "\n\tTake-home Wage: $%.2f", ((*employee).weeklyPay)-(*employee).taxesPaid); 

} 

EDIT Datei 2: Hinzugefügt s truct definition

struct Employee { 
char name[40]; 
float weeklyPay; 
float hoursWorked; 
float taxesPaid;}; 
+0

'fopen (& fileName," r + ")' -> 'fopen (Dateiname," r + ")' – BLUEPIXY

+0

Können Sie den Teil des Codes zeigen, wo Sie tatsächlich in Ihre Datei schreiben (fwrite oder fput stuff)? – technico

+0

@BLUEPIXY Danke für das Zeigen! Es gab mir eine Warnung, aber die Datei wurde immer noch erfolgreich geöffnet, also glaube ich nicht, dass das das Hauptproblem ist. Ich werde es aber reparieren! – Mytz

Antwort

0

Ich habe versucht, alle zusammen zu sammeln. Mein testdat (das Format, das Sie gab eine schwer zu verstehen (eher: ich zu dumm bin zu verstehen), nur entsprechend anpassen)

Name: 6ZjQPZJniP 
     Hours Worked: 35.0 
     Weekly Wage: $12.0 
     Taxes Paid: $65.0 
Name: pOmMgjgPHD 
     Hours Worked: 20.0 
     Weekly Wage: $49.0 
     Taxes Paid: $85.0 
Name: TRTxUptiD2 
     Hours Worked: 43.0 
     Weekly Wage: $23.0 
     Taxes Paid: $42.0 
Name: q5mQ5Yy0QA 
     Hours Worked: 22.0 
     Weekly Wage: $97.0 
     Taxes Paid: $67.0 
Name: saiZAKNJSy 
     Hours Worked: 59.0 
     Weekly Wage: $99.0 
     Taxes Paid: $70.0 
Name: lh3ZnB81jI 
     Hours Worked: 19.0 
     Weekly Wage: $36.0 
     Taxes Paid: $49.0 
Name: H8mw8eckfB 
     Hours Worked: 35.0 
     Weekly Wage: $24.0 
     Taxes Paid: $45.0 
Name: t9E4sQnmlG 
     Hours Worked: 64.0 
     Weekly Wage: $14.0 
     Taxes Paid: $63.0 
Name: KggqJYULf5 
     Hours Worked: 84.0 
     Weekly Wage: $80.0 
     Taxes Paid: $15.0 
Name: Emov5IhBce 
     Hours Worked: 24.0 
     Weekly Wage: $35.0 
     Taxes Paid: $74.0 

(! Endet mit Newline)

Der Code

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

    // ALL CHECKS OMMITTED! 

typedef struct Employee { 
    char name[40]; 
    float hoursWorked; 
    float weeklyPay; 
    float taxesPaid; 
} Employee; 
// all structs 
void saveToFile(Employee ** employee, int employeeNumb); 
// one struct 
void printToFile(FILE * fp, Employee * employee); 
// one struct 
int loadFromFile(Employee * employee, FILE * fp); 

static int employeeNumb; 

void saveToFile(Employee ** employee, int employeeNumb) 
{ 
    FILE *fp; 
    char newFile[128]; 
    printf 
     ("Please enter the name of the text file you'd like to create or overwrite: "); 
    scanf(" %s", newFile); 
    fp = fopen(newFile, "w+"); //r+ if do NOT want to overwrite or a+ if append 
    if (fp == NULL) { 
    printf("Unable to open %s", newFile); 
    exit(1); 
    } 
    for (int i = 0; i < employeeNumb; i++) { 
    printToFile(fp, employee[i]); 
    } 
    fclose(fp); 
} 

void printToFile(FILE * fp, Employee * employee) 
{ 
    fprintf(fp, "\nName: %s", employee->name); 
    fprintf(fp, "\n\tHours Worked: %f", employee->hoursWorked); 
    fprintf(fp, "\n\tWeekly Wage: $%.2f", employee->weeklyPay); 
    fprintf(fp, "\n\tTaxes Paid: $%.2f", employee->taxesPaid); 
    fprintf(fp, "\n\tTake-home Wage: $%.2f", 
     (employee->weeklyPay) - employee->taxesPaid); 
} 

int loadFromFile(Employee * employee, FILE * fp) 
{ 
    if (feof(fp)){ 
    return employeeNumb; 
    } 
    // I just c&p'd the format. Works because the format is fixed 
    // Keep it simple when you can! 
    fscanf(fp, "Name: %s\n", employee->name); 
    fscanf(fp, "\tHours Worked: %f\n", &employee->hoursWorked); 
    fscanf(fp, "\tWeekly Wage: $%f\n", &employee->weeklyPay); 
    fscanf(fp, "\tTaxes Paid: $%f\n", &employee->taxesPaid); 
    employeeNumb++; 
    return employeeNumb; 
} 

int main() 
{ 
    char fileName[128]; 
    FILE *fp; 
    // pointer to the pointers to the (10) Employee structs 
    Employee **employeeInfo; 
    int i; 

    printf("Please enter the name of the text file you'd like to read from: "); 
    scanf(" %s", fileName); 
    fp = (fopen(fileName, "r")); 
    if (fp == NULL) { 
    fprintf(stderr, "Unable to open %s", fileName); 
    exit(EXIT_FAILURE); 
    } 
    // we need enough memory to safe ten structs 
    // at first allocate memory for 10 pointers (to the structs) 
    employeeInfo = malloc(10 * sizeof(Employee*)); 
    for (i = 0; i < 10; i++) { 
    // for each pointer allocate memory for one struct 
    employeeInfo[i] = malloc(sizeof(Employee)); 
    } 
    for (i = 0; i < 10; i++) { 
    // loadFromFile() takes one struct 
    loadFromFile(employeeInfo[i], fp); 
    } 

    // clean up 
    fclose(fp); 
    // saveToFile() takes all structs 
    saveToFile(employeeInfo, employeeNumb); 
    // free memory of the individual structs 
    for (i = 0; i < 10; i++) { 
    free(employeeInfo[i]); 
    } 
    // free the memory holding the 10 pointers 
    free(employeeInfo); 
    // say Bye! 
    exit(EXIT_SUCCESS); 
} 

Nicht perfekt, aber Sie sollten etwas zu bauen haben.

+0

Wow, vielen Dank! Es tut mir leid, ich konnte nicht klarer sein; Ich bin ziemlich neu im Programmieren. Ich bin ein wenig verwirrt, warum Sie einen Zeiger auf das Array von Strukturen verwenden würden. Aber danke! Es wird mir definitiv eine große Hilfe sein! – Mytz

+0

Sie wollten Zeiger, Ihre Verwendung von '-> 'deutete darauf hin. Ich habe es nur ein wenig aufgeräumt und habe durchgehend Zeiger verwendet. Der einzige Nachteil ist, dass Sie sich selbst um das Gedächtnis kümmern müssen. Es ist auch, wie Sie es tun, wenn Sie eine größere Menge an Speicher benötigen und/oder nicht wissen, wie viel im Voraus. Stack-Speicher ist teuer, Sie verschwenden es nicht, Heap-Speicher auf der anderen Seite ist billig und flexibel. – deamentiaemundi

+0

Das '' '' in 'scanf ("% s ", newFile);' dient nur wenig dazu. '"% s "' verbraucht selbst und verwirft führenden Platz. Ohne eine Breite Grenze 'scanf ("% s ", newFile);' ist so sicher wie 'bekommt (newFile);'. Empfehlen Sie mit 'fgets()' und lop off sein potenzielles nachhängt '\ n''. – chux