使用fgets()从文件读取然后使用另一个函数中的信息

时间:2013-06-22 10:23:46

标签: c file pointers fgets

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

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()逐行检索此信息。当我读它时,我想调用另一个使用行读取的函数来处理信息并将其放在列表中。问题是,当我尝试调用函数并将字符串作为参数传递时,它给出了seg错误: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;
}

3 个答案:

答案 0 :(得分:2)

您的代码崩溃了:

string1 = (char) *a;

你会在任何调试器中看到这一点。首先,请注意此处(char)投射是不必要的。但真正的问题是a有时是NULL,因此无法解除引用。在这种情况下,你需要弄清楚你想做什么。

答案 1 :(得分:2)

首先致电divideString(line, i, parametro1);

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

返回值为'1' - > 49

'-'的{​​{1}} line替换'\0'的{​​{1}}

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

第二次致电"123\055555-1 10000 0"

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!! )每个成员。

E.g。

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! 
}

E.g。改为

    //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");

答案 2 :(得分:-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;
}