C拆分字符串并将其用于结构 [英] C Split String and Use It For Struct

查看:46
本文介绍了C拆分字符串并将其用于结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用C创建带有结构和链接列表的电话簿.

I need to create a phonebook in C, with structs and linked-list.

数据将保存在文件中.我将像这样存储数据

Data will be on a file. I'll store data like

Name1#Surname1#Date1#Number1
Name2#Surname2#Date1#Number2
Name3#Surname3#Date1#Number3

但是我不能用定界符#"分割并在结构中使用数据.这是我的代码:

But I couldn't split by delimiter "#" and use datas in my struct. Here is my code:

#include <stdio.h>
#include <locale.h>

//Veri Yapısı:
typedef struct kisi
{
    char isim[50];
    char soyisim[50];
    int dYili;
    char telefon[16];
    struct kisi *sonraki; 
}Kisi;
int main()
{
    //Türkçe çıktılarda sorun yaşamamak için:
    setlocale(LC_ALL,"Turkish");
    //Dosya yolunu sabitle:
    char *dosyaYolu = "rehber.dat";
    //Dosyayı aç:
    FILE *dosya;
    //Okuma modunda
    dosya = fopen(dosyaYolu,"r");
    //Eğer dosya açılmazsa:
    if(dosya == NULL) 
    {
        printf("Dosya açılamadı. Yolu doğru girdiğinizden emin olun.\n===============================\n\n");
        main(); 
    }
    //Dosya açıldı.
    else
    {
        Kisi *liste = (Kisi*)malloc(sizeof(Kisi));
        Kisi *iter = liste;
        //Dosyanın sonuna kadar oku:
        while(!feof(dosya))
        {
             char *veri;
             fscanf(dosya,"%s",veri);
             char *tok = strtok(veri, "#");
             int sayac = 0;
            while(tok != NULL)
            {
             switch(sayac)
             {
              case 0: strcpy(iter->isim,tok);
                   break;
              case 1: strcpy(iter->soyisim,tok);
                   break;
              case 2: iter->dYili = (int)tok;
                   break;
              case 3: strcpy(iter->telefon,tok);
                   break;
             }
             sayac++;
             tok = strtok(NULL, "#");
            }
            iter->sonraki = (Kisi*)malloc(sizeof(Kisi));
            iter = iter->sonraki;
        }//while sonu
       //Dosyayla işimiz bitti. Kapat:
       fclose(dosya);
    }
    system("pause>null");
}

我需要做的是:

  1. 从文件中读取数据
  2. 将数据用于结构
  3. 链接结构
  4. 添加一个新的人,删除一个人等.(我还没有尝试过,我的代码对此一无所知)

我的问题是,当我运行此程序时,它停止工作.当我删除带有switch-case的while循环并仅打印我的令牌时,它就起作用了.

My problem is, when I run this program, it stops working. When I remove that while loop with switch-case and just printf my token, it works.

其他信息:

  • 我必须使用文件来存储我的数据
  • 我必须使用结构和链接列表
  • 每个人都必须使用自己的结构(1个结构= 1个人,当然是相同的结构)
  • 该程序不能包含C ++代码.

推荐答案

您有2个导致未定义行为的重要问题,因此也是程序中的问题

You have 2 important problems causing undefined behavior and hence the problem in your program

  1. 您不会在 while 循环的外部 veri 分配空间

char *veri;
fscanf(dosya,"%s",veri);

您至少应该做类似的事情

you should at least do something like

char veri[100];
fscanf(dosya,"%99s",veri);

  • 您将指针的值分配给 int

    iter->dYili = (int)tok
    

    您应该通过这种方式通过 strtol 将字符串转换为 int

    you should, convert the string to an int by means of strtol this way

    iter->dYili = strtol(tok, NULL, 10);
    

    如果要在循环的开始处检查转换错误delcare a char * endptr; ,并执行此操作

    if you want to check for conversion error delcare a char *endptr; at the beginig of the loop and do this

    iter->dYili = strtol(tok, &endptr 10);
    if (*endptr != '\0')
        anErrorOccurredHandleIt();
    

  • 此外,别忘了 free 所有 malloc 指针,在这种情况下,我认为 fgets 会做得更好

    Also, don't forget to free all the malloced pointers, and I think fgets would do a better job in this situation.

    这篇关于C拆分字符串并将其用于结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆