1

i'm trying to write a database to store information of staff. Here is my problematic code:

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

The output give:

===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 . . .

However, the output in text.file is not as expected:

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

The problem lies with this code:

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

If I use scanf instead of fgets, I cannot store strings with spaces. Can anyone suggest me how to make it work?

Nabil Fikri
  • 27
  • 1
  • 1
  • 3

2 Answers2

0

well, you have the whole string, you just need to put a null terminator on the end of the string or pass it to a new string:

int i = 0;
int lastPlace = 0;

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

as zubergu said:

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.

this means the problem in your code is that you write the newline charecter to the file. just remove it!

full code:

#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;
}
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
0

The problem is fgets reads '\n' and inputs it in your char array, and then puts '\0' at the end. So you basically include new line character to your string. What you have to do is to remove '\n', because it will be placed in your output file and mess up the results.

zubergu
  • 3,646
  • 3
  • 25
  • 38
  • remove the \n in the fprintf right? after trying without \n, the first 2 output will be in line with the "staff id, name, ...". – Nabil Fikri Aug 21 '13 at 09:30