Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

I have a problem with storing data in to my text file using C

I'm new to stack overflow as well as C programming. I was trying to create txt .file to store my telephone number as a list in to the text file. I will first ask them to enter the details and that details will then store into the txt file. I having trouble with an error, saying my variable is "uninitialized". What should I do?

here's my code

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
void assignEmpTelNum();
void searchEmpTelNum();
void updateEmpTelNum();
void empTelNumByDept();
void main()
{
    system("tds");
    printf("*****Telephone Directory System*****
");
    printf("1. Assign Telephone Number to New Employee
");
    printf("2. Search Employee's Telephone Number
");
    printf("3. Update Employee's Telephone Number
");
    printf("4. View Employee's Telephone Number by Department
");
    printf("5. End System
");
    printf("Enter your option: ");
    int option;
    scanf_s("%d", &option);
    switch (option)
    {
    case 1:
        assignEmpTelNum();
        break;
    case 2:
        searchEmpTelNum();
        break;
    case 3:
        updateEmpTelNum();
        break;
    case 4:
        empTelNumByDept();
        break;
    case 5:
        exit(0);
    default:
        main();
    }
}

void assignEmpTelNum()
{
    FILE* fp;
    fp = fopen("tel.txt", "w");
    system("tds");
    printf("*****Employee Details and Assign Telephone Number*****");

    printf("
Employee Name: ");
    char emp_name[30];
    scanf_s("%s", emp_name);

    printf("
Employee ID: ");
    char emp_id[5];
    scanf_s("%s", emp_id);

    printf("
Employee's Telephone Number: ");
    int emp_num; 
    scanf_s("%d", emp_num);

    printf("
Employee's Department: ");
    char emp_dept[50];
    scanf_s("%s", emp_dept);

    fprintf(fp, "%s %s %d %s
", emp_name, emp_id, emp_num, emp_dept);
    fclose(fp);
    printf("
*** PRESS ANY KEY TO CONTINUE ***");
    getchar();
    main();
}

The error list

question from:https://stackoverflow.com/questions/65887454/i-have-a-problem-with-storing-data-in-to-my-text-file-using-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...