我正在使用的程序应该从预先存在的文本文件中提取数据,编辑数据,然后将其重写到新的文本文件中。我已经完成了大部分工作,但是它没有将任何扫描数据存储到我的阵列中。当我调用“打印到文件”功能时(我肯定可以正常工作),没有任何内容打印到新文件中。
整个代码文件相当长,因此我只包含其中一部分。让我知道您是否需要任何澄清;任何建议或想法都会有很大帮助!先感谢您!
它应从中提取文件的默认格式为
Name:\n\t Hours Worked: \n\tWeekly Pay: \n\tTaxes Paid: \n\tTake-home Wage:
码
// 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;
}
编辑:添加了我的打印到文件功能
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);
}
编辑2:添加了结构定义
struct Employee {
char name[40];
float weeklyPay;
float hoursWorked;
float taxesPaid;};
最佳答案
我试图聚集在一起。
我的测试文件(您提供的格式很难理解(更可能是:我太愚蠢而无法理解),只需进行相应调整即可)
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
(以换行符结尾!)
代码
#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);
}
并不完美,但您应该有所建树。
关于c - 使用fscanf将浮点数和字符串存储到C语言中的结构数组中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38820111/