标题:C语言初学者求助!关于从键盘读入姓名和电话号码,将它们写入一个文件的问题 ...
只看楼主
zmf876870302
Rank: 3Rank: 3
来 自:福建
等 级:论坛游侠
威 望:3
帖 子:50
专家分:199
注 册:2017-3-26
结帖率:100%
已结贴  问题点数:20 回复次数:4 
C语言初学者求助!关于从键盘读入姓名和电话号码,将它们写入一个文件的问题。
    程序运行后,找不到文件。不知道哪里出错。代码也不知道怎么改!这样写代码有什么问题?要怎么解决?
    具体代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

#define NAME_LEN        21
#define TEL_LEN         11

typedef struct Record Record;

struct Record
{
    char name[NAME_LEN];
    char Tel[TEL_LEN];
    Record *next;
};

Record *get_person(Record *pcurrent);
void get_name(char *pname, size_t size);
void write_file(Record *first, char *filename, FILE *pfile);
void free_record(Record *first);

int main(void)
{
    Record *first = NULL;
    Record *current = NULL;
    Record *previous = NULL;
    char more = '\0';
   
    printf("这是一个从键盘读入姓名和电话号码,将它们写入一个文件的程序!\n");
   
    while(true)
    {
        printf("现在请确认是否有从键盘读入姓名和电话号码:Y:是!N:否! ");
        scanf(" %c", &more);
        fflush(stdin);
        
        if(toupper(more) == 'N')
          break;
         
        if(first == NULL)
          first = current;
         
        if(previous != NULL)
          previous -> next = current;
         
        get_person(current);
        previous = current;
    }
   
    FILE *pfile = NULL;
    char *filename = "myTel.txt";
   
    write_file(first, filename, pfile);
   
    free_record(first);
   
    return 0;
}

Record *get_person(Record *pcurrent)
{
    pcurrent = (Record*)malloc(sizeof(Record));
    if(!pcurrent)
    {
        printf("没有分配内存!\a\a\a\n");
        exit(1);
    }
   
    printf("现在从键盘读入不超过 %d 字节姓名: ", NAME_LEN - 1);
    get_name(pcurrent -> name, NAME_LEN);
    printf("现在从键盘读入%s的电话号码: ", pcurrent -> name);
    get_name(pcurrent -> Tel, TEL_LEN);
    pcurrent -> next = NULL;
   
    return pcurrent;
}

void get_name(char *pname, size_t size)
{
    fflush(stdin);
    fgets(pname, size, stdin);
   
    size_t len = strlen(pname);
   
    if(pname[len - 1] == '\n')
      pname[len - 1] = '\0';
}

void write_file(Record *first, char *filename, FILE *pfile)
{
    Record *pcurrent = NULL;
    for(pcurrent = first; pcurrent != NULL; pcurrent = pcurrent -> next)
    {
        if(!(pfile = fopen(filename, "a")))
        {
            fprintf(stderr, "打开%s文件不成功!\a\a\a\n", filename);
            exit(1);
        }
        setvbuf(pfile, NULL, _IOFBF, BUFSIZ);
        
        size_t len_name = strlen(pcurrent -> name);
        
        if(EOF == fputs(pcurrent -> name, pfile))
        {
            fprintf(stderr, "%s写入%s文件不成功!\a\a\a\n", pcurrent -> name, filename);
            exit(1);
        }
        
        int i = 0;
        for(i = 0; i < NAME_LEN - len_name; ++i)
        {
            char ch1 = ' ';
            if(EOF == fputc(ch1, pfile))
            {
                fprintf(stderr, "写入%s文件不成功!\a\a\a\n", filename);
                exit(1);
            }
        }
        
        if(EOF == fputs(pcurrent -> Tel, pfile))
        {
            fprintf(stderr, "电话号码写入%s文件不成功!\a\a\a\n", filename);
            exit(1);
        }
        
        char ch2 = '\n';
        if(EOF == fputc(ch2, pfile))
        {
            fprintf(stderr, "写入%s文件不成功!\a\a\a\n", filename);
            exit(1);
        }
        
        fclose(pfile);
        pfile = NULL;
    }
}

void free_record(Record *first)
{
    Record *pcurrent = NULL;
    Record *previous = NULL;
   
    pcurrent = first;
   
    while(pcurrent != NULL)
    {
        previous = pcurrent;
        pcurrent = pcurrent -> next;
        
        free(previous);
        previous = NULL;
    }
   
    free(first);
    first = NULL;
}
搜索更多相关主题的帖子: include C语言 电话 键盘 
2017-04-23 13:08
炎天
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:桃花岛
等 级:贵宾
威 望:29
帖 子:1218
专家分:4986
注 册:2016-9-15
得分:0 


程序代码:
int main(void)
{
    Record *first = NULL;
    Record *current = NULL;
    Record *previous = NULL;
    char more = '\0';
    
    printf("这是一个从键盘读入姓名和电话号码,将它们写入一个文件的程序!\n");
    
    while(true)
    {
        printf("现在请确认是否有从键盘读入姓名和电话号码:Y:是!N:否! ");
        scanf(" %c", &more);
        fflush(stdin);
        
        if(toupper(more) == 'N')
          break;
         current= get_person(current);                 //get_person()这个函数会返回一个指针, 里面有指针空间的申请
                                                        //为指针申请空间后,地址将会发生改变,malloc,realloc之流
        if(first == NULL)
          first = current;
          
        if(previous != NULL)
          previous -> next = current;
          
      
        previous = current;
    }
    
    FILE *pfile = NULL;
    char *filename = "myTel.txt";
    
    write_file(first, filename, pfile);
    
    free_record(first);
    
    return 0;
}


程序代码:
void free_record(Record *first)
{
    Record *pcurrent = NULL;
    Record *previous = NULL;
    
    pcurrent = first;
    
    while(pcurrent != NULL)
    {
        previous = pcurrent;
        pcurrent = pcurrent -> next;
        
        free(previous);
        previous = NULL;
    }
    
    //free(first);        //重复释放
   // first = NULL;
}


[此贴子已经被作者于2017-4-24 02:08编辑过]


早知做人那么辛苦!  当初不应该下凡
2017-04-24 02:01
zmf876870302
Rank: 3Rank: 3
来 自:福建
等 级:论坛游侠
威 望:3
帖 子:50
专家分:199
注 册:2017-3-26
得分:0 
回复 2楼 炎天
    我已按照你说改过来。这回是找得到文件,可是文件只显示好几百条第一个记录,并且运行程序后不能正常结束!程序是不是还有其它更重要的问题?

还在学习过程中,欢迎指教!
2017-04-24 08:08
炎天
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:桃花岛
等 级:贵宾
威 望:29
帖 子:1218
专家分:4986
注 册:2016-9-15
得分:0 
回复 3楼 zmf876870302
是么,

早知做人那么辛苦!  当初不应该下凡
2017-04-24 12:38
炎天
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:桃花岛
等 级:贵宾
威 望:29
帖 子:1218
专家分:4986
注 册:2016-9-15
得分:20 
程序代码:
#include <stdio.h>
#include <stdlib.h>
//#include <stdbool.h>
#include <string.h>
#include <ctype.h>

#define NAME_LEN        21
#define TEL_LEN         11

typedef struct Record Record;

struct Record
{
    char name[NAME_LEN];
    char Tel[TEL_LEN];
    Record *next;
};

Record *get_person(Record *pcurrent);
void get_name(char *pname, size_t size);
void write_file(Record *first, char *filename, FILE *pfile);
void free_record(Record *first);

int main(void)
{
    Record *first = NULL;
    Record *current = NULL;
    Record *previous = NULL;
    char more = '\0';
    
    printf("这是一个从键盘读入姓名和电话号码,将它们写入一个文件的程序!\n");
    
    while(true)
    {
        printf("现在请确认是否有从键盘读入姓名和电话号码:Y:是!N:否! ");
        scanf(" %c", &more);
        fflush(stdin);
        
        if(toupper(more) == 'N')
          break;
         current= get_person(current);                 
                                           //这一句如果按你之前放的位置,会造成2个问题
                                           //1. 如果只有一条数据,将不会产生文件(如果没有文件的话),
                                           //2. 如果有多条数据,则会只记录第二条数据,并且有进入死循环
                                           //第一次while循环, first=current=NULL, previous=current;
                                           //第二次while循环, first=current=previous, previous->next=current(死循环了)
        if(first == NULL)
          first = current;
          
        if(previous != NULL)
          previous -> next = current;
          
      
        previous = current;
    }
    
    FILE *pfile = NULL;
    char *filename = "myTel.txt";
    
    write_file(first, filename, pfile);
    
    free_record(first);
    
    return 0;
}

Record *get_person(Record *pcurrent)
{
    pcurrent = (Record*)malloc(sizeof(Record));
    if(!pcurrent)
    {
        printf("没有分配内存!\a\a\a\n");
        exit(1);
    }
    
    printf("现在从键盘读入不超过 %d 字节姓名: ", NAME_LEN - 1);
    get_name(pcurrent -> name, NAME_LEN);
    printf("现在从键盘读入%s的电话号码: ", pcurrent -> name);
    get_name(pcurrent -> Tel, TEL_LEN);
    pcurrent -> next = NULL;
    
    return pcurrent;
}

void get_name(char *pname, size_t size)
{
    fflush(stdin);
    fgets(pname, size, stdin);
    
    size_t len = strlen(pname);
    
    if(pname[len - 1] == '\n')
      pname[len - 1] = '\0';
}

void write_file(Record *first, char *filename, FILE *pfile)
{
    Record *pcurrent = NULL;
    for(pcurrent = first; pcurrent != NULL; pcurrent = pcurrent -> next)
    {
        if(!(pfile = fopen(filename, "a")))
        {
            fprintf(stderr, "打开%s文件不成功!\a\a\a\n", filename);
            exit(1);
        }
        setvbuf(pfile, NULL, _IOFBF, BUFSIZ);
        
        size_t len_name = strlen(pcurrent -> name);
        
        if(EOF == fputs(pcurrent -> name, pfile))
        {
            fprintf(stderr, "%s写入%s文件不成功!\a\a\a\n", pcurrent -> name, filename);
            exit(1);
        }
        
        int i = 0;
        for(i = 0; i < NAME_LEN - len_name; ++i)
        {
            char ch1 = ' ';
            if(EOF == fputc(ch1, pfile))
            {
                fprintf(stderr, "写入%s文件不成功!\a\a\a\n", filename);
                exit(1);
            }
        }
        
        if(EOF == fputs(pcurrent -> Tel, pfile))
        {
            fprintf(stderr, "电话号码写入%s文件不成功!\a\a\a\n", filename);
            exit(1);
        }
        
        char ch2 = '\n';
        if(EOF == fputc(ch2, pfile))
        {
            fprintf(stderr, "写入%s文件不成功!\a\a\a\n", filename);
            exit(1);
        }
        
        fclose(pfile);
        pfile = NULL;
    }
}

void free_record(Record *first)
{
    Record *pcurrent = NULL;
    Record *previous = NULL;
    
    pcurrent = first;
    
    while(pcurrent != NULL)
    {
        previous = pcurrent;
        pcurrent = pcurrent -> next;
        
        free(previous);
        previous = NULL;
    }
    
    //free(first);        //重复释放
   // first = NULL;
}


[此贴子已经被作者于2017-4-24 22:56编辑过]

收到的鲜花
  • zmf8768703022017-04-25 12:27 送鲜花  3朵   附言:我很赞同

早知做人那么辛苦!  当初不应该下凡
2017-04-24 12:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-476358-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.339149 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved