使用fscanf将浮点数和字符串存储到C中的结构数组中

时间:2016-08-08 01:25:11

标签: c arrays file struct scanf

我正在处理的程序应从预先存在的文本文件中提取数据,编辑数据并将其重写到新的文本文件中。我已经完成了大部分工作,但它没有将任何扫描数据存储到我的阵列中。当我调用我的打印到文件功能(我相当肯定工作正常)时,没有任何内容打印到新文件。

整个代码文件相当长,所以我只包含了一些内容。如果您需要任何澄清,请告诉我们;任何建议或想法将是一个很大的帮助!提前谢谢!

它应该从中提取的文件的默认格式为

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;};

1 个答案:

答案 0 :(得分: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);
}

(以换行结束!)

代码

{{1}}

不完美,但你应该有所建议。

相关问题