Jump to content

Help With Writing to Text files

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
teensicle

teensicle

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
i've been trying for some time now but i just don't kno whatim doing wrong this is my code

int emp_details()
{
    FILE *fp;
    fp=fopen("EMP_DETAILS.txt", "w");

        char emp_name_f;
        char emp_name_l;
        char address;
        char email;
        int tel;
        int age;
        int trn;
        int v_id;

     printf("\n\t\t\t****Employee Information Screen****\n\n");


     printf("\n\nPlease Enter Employee's First Name: ");
     scanf("%s",&emp_name_f);
     printf("\n----------------------------------------------------\n");
     printf("\nPlease Enter Employee's Last Name: ");
     scanf("%s",&emp_name_l);
     printf("\n----------------------------------------------------\n");
     printf("\nPlease Enter Employee's Address: ");
     scanf("%s",&address);
     printf("\n----------------------------------------------------\n");
     printf("\nPlease Enter Employee's Email: ");
     scanf("%s",&email);
     printf("\n----------------------------------------------------\n");
     printf("\nPlease Enter Employee's Telephone #: ");
     scanf("%d",&tel);
     printf("\n----------------------------------------------------\n");
     printf("\nPlease Enter Emplyee's Age: ");
     scanf("%d",&age);
     printf("\n----------------------------------------------------\n");
     printf("\nPlease Enter Employee's TRN#: ");
     scanf("%d",&trn);

     fprintf(fp,"PRINT TEST",emp_name_f);

     fclose(fp);

all i get is a blank file... :(

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Perhaps your program crashes before attempting the write since you are likely trying to write a string into a single character? For example,
        char emp_name_f;
     scanf("%s",&emp_name_f);


#3
teensicle

teensicle

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Im really new to this programing thing :( i just started.. What am i doing wrong...

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Trying to put 10 pounds of **** in a 1-pound bag. :P

You need to know what a string is before attempting to read one. A string is an array of characters ending with a null terminator. To read one, you'll need a char array.

http://forum.codecal...-numbers-c.html

#5
teensicle

teensicle

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Dude thanks it works!!!!

#6
teensicle

teensicle

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
i think i need to get a new teacher...