1

我有一个这样的文本文件:

123-55555-1 10000 0   
123-55533-3 12300 500 
123-99971-3 50000 0 
123-38951-2 350 10  
120-39888-0 4910 100   
121-12345-3 50000 150 
121-xptoz-3 1000 100  
150-23857-1 350000 20000 
521-71750-4 500000 25000 
191-11999-7 1200

我想要的是能够使用fgets(). 当我阅读它时,我想调用另一个函数,该函数使用 read 行处理信息并将其放在列表中。问题是当我尝试调用函数并将字符串作为参数传递时,它给了我 seg error:11。这是我的代码。

typedef struct identificador_s
{
  int a;
  int b;
  int c;

} identificador;

typedef struct contaBancaria_s
{
  identificador id;
  int saldo;
  unsigned short int credito;
  struct contaBancaria_s * proximo;

} contaBancaria;

contaBancaria * contaP = NULL;
char contas[] = "contas.txt";
char movimentos[] = "movimentos.txt";

char divideString(char line[], int contagem, char parametro[])
{
  int i = 0;
  char *a = NULL;
  char string1;
  a = strtok(line, parametro);
  while (i != contagem)
  {
    a = strtok(NULL, parametro);
    i++;
  }
  string1 = (char) *a;
  return string1;

}

void contaFill(char line[])
{
  printf("passo -2");
  contaBancaria * p = malloc(sizeof(contaBancaria));
  printf("passo 0");
  int i = 0;
  char parametro1[] = "-";
  char parametro2[] = " ";
  p->id.a = (int) divideString(line, i, parametro1);
  printf("passo 1");
  p->id.b = (int) divideString(line, i += 1, parametro1);
  printf("passo2");
  p->id.c = (int) divideString(line, i += 1, parametro1);
  printf("passo 3");
  /*if(!(validaIdentificador(p-id.a,p->id.b,p-id.c))){
   return;
   }*/
  p->saldo = (int) divideString(line, i += 1, parametro2);
  printf("passo 4");
  p->credito = (int) divideString(line, i += 1, parametro2);
  printf("passo 5");
  printf("%d - %d - %d %d %d", p->id.a, p->id.b, p->id.c, p->saldo, p->credito);
}

void loadFile(char fileType[])
{
  FILE * fp;
  fp = fopen(fileType, "r");
  int size = 100;
  char buffer[100];
  char string1[100];
  if (fp)
  {
    while (fgets(buffer, 100, fp) != NULL)
    {
      puts(buffer);
      if (strcmp(fileType, "contas.txt") == 0)
      {
        contaFill(buffer);
      }
    }
    fclose(fp);
  }
}

int main(int argc, char* argv[])
{
  loadFile(contas);
  return 0;
}
4

3 回答 3

2

您的代码在这里崩溃:

string1 = (char) *a;

您会在任何调试器中看到这一点。首先,请注意(char)这里的演员表是不必要的。但真正的问题是a有时NULL不能取消引用。你需要弄清楚在这种情况下你想做什么。

于 2013-06-22T11:18:34.440 回答
2

第一次通话divideString(line, i, parametro1);

p->id.a = (int) divideString(line, i, parametro1);

返回值为 '1' -> 49

首先替换为'-'_line'\0'strtok

(例如"123-55555-1 10000 0"->"123\055555-1 10000 0"表示“123”

第二次通话divideString(line, i += 1, parametro1);

a = strtok(line, parametro);//not find parametro return `a=line`(top)
...
a = strtok(NULL, parametro);//a=`NULL`
...
string1 = (char) *a;//*(NULL) seg fault!!

更新

集合在一起并停止调用(divideString)一个的每个成员。

例如

void stringToContaBancaria(char line[], contaBancaria *p, char para1[], char para2[]){
    char *a, *endp;
    //To convert to int from numeric strings for example it use strtol
    p->id.a    = strtol(a=strtok(line, para1), &endp, 10);
    if(*endp) fprintf(stderr, "id.a not number : %s\n", a);
    p->id.b    = strtol(a=strtok(NULL, para1), &endp, 10);
    if(*endp) fprintf(stderr, "id.b not number : %s\n", a);
    p->id.c    = strtol(a=strtok(NULL, para2), &endp, 10);
    if(*endp) fprintf(stderr, "id.c not number : %s\n", a);
    p->saldo   = strtol(a=strtok(NULL, para2), &endp, 10);
    if(*endp) fprintf(stderr, "saldo not number : %s\n", a);
    p->credito = strtoul(a=strtok(NULL, para2), &endp, 10);
    if(*endp) fprintf(stderr, "credito not number : %s\n", a);
}

void contaFill(char line[]){
    contaBancaria * p = malloc(sizeof(contaBancaria));
    char parametro1[] = "-";
    char parametro2[] = " ";//" \n"?
    stringToContaBancaria(line, p, parametro1, parametro2);
    printf("%d - %d - %d %d %hu\n", p->id.a, p->id.b, p->id.c, p->saldo, p->credito);
    //free(p);//deallocate! 
}

它给了我段错误

    //less a(=strtok return value) becomes to NULL element when reading is less than expected.
    p->credito = strtoul(a=strtok(NULL, para2), &endp, 10);
    if(*endp) fprintf(stderr, "credito not number : %s\n", a);

例如更改为

    if(NULL!=(a=strtok(NULL, para2))){
        p->credito = strtoul(a, &endp, 10);
        if(*endp)fprintf(stderr, "credito not number : %s\n", a);
    } else
        fprintf(stderr, "credito not exist\n");

但我认为检查之前的时间而不是阅读这些必要的项目之间是否有。


做一个简单的预检查

#include <ctype.h>

int isInvalidRecord(char line[]){
//[number]-[number]-[number][space][number][space][number][space*]
//521-71750-4 500000 25000[newline]
    int i=0, j;
    while(isdigit(line[i]))++i;
    if(i==0 || line[i]!='-') return 1;//1st item bad
    j=++i;
    while(isdigit(line[i]))++i;
    if(i==j || line[i]!='-') return 2;
    j=++i;
    while(isdigit(line[i]))++i;
    if(i==j || line[i]!=' ') return 3;
    j=++i;
    while(isdigit(line[i]))++i;
    if(i==j || line[i]!=' ') return 4;
    j=++i;
    while(isdigit(line[i]))++i;
    if(i==j || (line[i]!='\0' && !isspace(line[i]))) return 5;
    return 0;//ALL OK
}

void contaFill(char line[]){
    contaBancaria * p = malloc(sizeof(contaBancaria));
    char parametro1[] = "-";
    char parametro2[] = " ";//" \n"?
    int check = isInvalidRecord(line);
    if(check == 0)//0 is OK
        stringToContaBancaria(line, p, parametro1, parametro2);
    else
        fprintf(stderr, "%s %d item(near) is bad\n", line, check);
    printf("%d - %d - %d %d %hu\n", p->id.a, p->id.b, p->id.c, p->saldo, p->credito);
    //free(p);//deallocate! 
}
于 2013-06-22T11:21:35.297 回答
-1

好吧,您的代码中有很多问题。我已尝试修复所有错误并且工作正常,不,这是我所做的改进:

这里

该函数strtok();返回您分配给它的指针类型,a而它应该被分配给*a

我在评论(//)中写了任何其他改进。

#include<stdio.h>
#include<stdlib.h>
typedef struct identificador_s
{
  int a;
  int b;
  int c;

} identificador;

typedef struct contaBancaria_s
{
  identificador id;
  int saldo;
  unsigned short int credito;
  struct contaBancaria_s * proximo;

} contaBancaria;

contaBancaria * contaP = NULL;
char contas[] = "contas.txt";
char movimentos[] = "movimentos.txt";

char divideString(char line[], int contagem, char parametro[])
{
  int i = 0;
  char *a = NULL;
  char string1;
  *a = strtok(line, parametro);
  while (i != contagem)
  {
    *a = strtok(NULL, parametro);
    i++;
  }
  string1 = (char) *a;
  return string1;

}

void contaFill(char line[])
{
  printf("passo -2");
  contaBancaria *p=malloc(sizeof(contaBancaria));
  printf("passo 0");
  int i = 0;
  char parametro1[] = "-";
  char parametro2[] = " ";
  p->id.a = (int) divideString(line, i, parametro1);
  printf("passo 1");
  p->id.b = (int) divideString(line, i += 1, parametro1);
  printf("passo2");
  p->id.c = (int) divideString(line, i += 1, parametro1);
  printf("passo 3");
  /*if(!(validaIdentificador(p-id.a,p->id.b,p-id.c))){
   return;
   }*/
  p->saldo = (int) divideString(line, i += 1, parametro2);
  printf("passo 4");
  p->credito = (int) divideString(line, i += 1, parametro2);
  printf("passo 5");
  printf("%d - %d - %d %d %d", p->id.a, p->id.b, p->id.c, p->saldo, p->credito);
}

void loadFile(char fileType[])
{
  FILE * fp;
  fp = fopen(fileType, "r");
  int size = 100;
  char buffer[100];
  char string1[100];
  if (fp)
  {
    while (fgets(buffer, 100, fp) != NULL)
    {
      puts(buffer);
      if (strcmp(fileType, "contas.txt") == 0)
      {
        contaFill(buffer);
      }
    }
      }
  fclose(fp);// file should close outside the if statement 
}

int main(int argc, char* argv[])
{
  loadFile(contas);
  return 0;
}
于 2013-06-22T11:19:20.800 回答