尝试将多个信息存储到文本文件时出错

时间:2013-08-21 08:46:59

标签: c io text-files

我正在尝试编写一个数据库来存储员工的信息。这是我有问题的代码:

#include<stdio.h> 
int main(){
      char name[100], gender[100], email[100], id[10], phone[20];
      FILE *fPtr;
      fPtr=fopen("staffinfo.txt","w");
      FILE *fPtr1;
      fPtr1=fopen("staffinfo.txt","a+");

      if (fPtr == NULL)
      printf("Error in opening file\n");
      if (fPtr1 == NULL)
      printf("Error in opening file\n");

      printf("\n===Add New Staff Profile===");
      printf("\n\nPlease enter the following staff information.");

      printf("\n\nStaff ID: ");
      scanf("%s", &id);

      fflush(stdin);
      printf("Name\t: ");
      fgets(name,100,stdin);

      printf("Gender\t: ");
      scanf("%s", &gender);

      printf("Phone\t: ");
      scanf("%s", &phone);

      printf("Email\t: ");
      scanf("%s", &email);

      fprintf(fPtr, "Staff ID\t Name\t\t Gender\t\t Phone\t\t Email");
      fprintf(fPtr1, "\n%s\t\t %s\t\t %s\t\t %s\t %s", id, name, gender, phone, email);

      printf("\nSYSTEM: New Staff Profile is Added Successfully.");

      fclose(fPtr);
      fclose(fPtr1);
      return 0;
}

输出结果:

===Add New Staff Profile===

Please enter the following staff information.

Staff ID: 1
Name    : Carmen Gray
Gender  : Female
Phone   : 123-4567890
Email   : carmen@live.com

SYSTEM: New Staff Profile is Added Successfully.
--------------------------------
Process exited with return value 0
Press any key to continue . . .

但是,text.file中的输出不符合预期:

Staff ID      Name        Gender         Phone       Email
1             Carmen Gray
              Female      123-4567890    carmen@live.com

问题出在这个代码上:

fflush(stdin);
printf("Name\t: ");
fgets(name,100,stdin);

如果我使用scanf而不是fgets,我不能用空格存储字符串。 任何人都可以建议我如何使它工作?

2 个答案:

答案 0 :(得分:0)

好吧,你有整个字符串,你只需要在字符串的末尾加上一个null终止符或将它传递给一个新的字符串:

int i = 0;
int lastPlace = 0;

for(i=0 ; i < 100 ; i++)
{
  if(name[i] == '\n')
    name[i] = '\0';
}

正如zubergu所说:

fgets Reads characters from stream and stores them as a C string into str until 
(num-1) characters have been read or either a newline or the end-of-file is 
reached, whichever happens first.

A newline character makes fgets stop reading, but it is considered a valid 
character by the function and included in the string copied to str.

A terminating null character is automatically appended after the characters 
copied to str.

这意味着您的代码中的问题是您将换行符字符写入文件。只是删除它!

完整代码:

#include<stdio.h> 
int main(){
      char name[100], gender[100], email[100], id[10], phone[20];
      FILE *fPtr;
      fPtr=fopen("staffinfo.txt","w");
      FILE *fPtr1;
      fPtr1=fopen("staffinfo.txt","a+");

      if (fPtr == NULL)
      printf("Error in opening file\n");
      if (fPtr1 == NULL)
      printf("Error in opening file\n");

      printf("\n===Add New Staff Profile===");
      printf("\n\nPlease enter the following staff information.");

      printf("\n\nStaff ID: ");
      scanf("%s", &id);

      fflush(stdin);
      printf("Name\t: ");
      fgets(name,100,stdin);

      // MY CODE EXAMPLE HERE
      int k = 0;
      int lastPlace = 0;

      for(k=0 ; k < 100 ; k++)
      {
        if(name[k] == '\n')
        name[k] = '\0';
      }

      printf("Gender\t: ");
      scanf("%s", &gender);

      printf("Phone\t: ");
      scanf("%s", &phone);

      printf("Email\t: ");
      scanf("%s", &email);

      fprintf(fPtr, "Staff ID\t Name\t\t Gender\t\t Phone\t\t Email");
      fprintf(fPtr1, "\n%s\t\t %s\t\t %s\t\t %s\t %s", id, name, gender, phone, email);

      printf("\nSYSTEM: New Staff Profile is Added Successfully.");

      fclose(fPtr);
      fclose(fPtr1);
      return 0;
}

答案 1 :(得分:0)

问题是fgets读取'\ n'并将其输入到char数组中,然后将'\ 0'放在最后。所以你基本上在你的字符串中加入了新行字符。您需要做的是删除'\ n',因为它将被放置在您的输出文件中并弄乱结果。