我在這里要做的是從 CSV 檔案中讀取第 3 到 32 行和第 35 到 44 行。當我閱讀這些行時,我使用 strtok 來分隔值并用每個標記填充一個結構。問題是當我運行代碼時沒有任何反應。我能夠將錯誤追溯到這部分代碼,因為當我評論這部分代碼時,程式運行完美。這是代碼:
#include <stdio.h>
#include <stdlib.h>
#include "carregar_files.h"
#include <string.h>
#include "mylib.h"
void necessidades_de_producao()
{
FILE *fp = fopen("OP_2022_02_01.csv", "r");
int i = 1, cont = 0;
char buff[1000];
ordens_compra *necessidades;
ordens *op;
necessidades = (ordens_compra*)malloc(1 * sizeof(ordens_compra));
op = (ordens*)malloc(1 * sizeof(ordens));
if(!fp)
{
printf("Erro ao abrir o ficheiro!!!!!!\n");
}
else
{
while(fgets(buff, 1000, fp) != NULL)
{
char *token;
int count;
if(i>=3 && i<=32)//linhas de operacao
{
op = (ordens*)realloc(op, sizeof(ordens) * (cont 1));// atualiza o tamanho da struct para o numero de ordens
token = strtok(buff,";"); // seprara a string buff em vários tokens
count = 0;
while(token != NULL){// percorrer os tokens, e verificar qual a posi??o do token para preencher corretamente
if(count == 0){op[cont].id = atoi(token);}//atoi recebe string e converte em int
if(count == 1){strcpy(op[cont].tipo,token);}
if(count == 2){op[cont].tamanho = atoi(token);}
if(count == 3){op[cont].quantidade = atoi(token);}
count ;
}
cont ;
}
if(i>=35 && i<=44)//linhas de necessidade
{
necessidades = (ordens_compra*)realloc(necessidades, sizeof(ordens_compra) * (cont 1));
token = strtok(buff,";");
count = 0;
while(token != NULL){
if(count == 0){necessidades[cont].OP = atoi(token);}//atoi recebe string e converte em int
if(count == 1){necessidades[cont].materia_prima = atoi(token);}
if(count == 2){necessidades[cont].quantidade = atoi(token);}
count ;
}
cont ;
}
i ;
}
}
}
這是結構頭檔案:
#ifndef CARREGAR_FILES_H
#define CARREGAR_FILES_H
#ifdef __cplusplus
extern "C" {
#endif
#define SUCESS "\nOs dados foram carregados com sucesso!\n"
typedef struct
{
int code;
char produto[10];
int min_stock;
int atual_stock;
} MP_couro;
typedef struct
{
int code;
char produto[10];
int min_stock;
int atual_stock;
} MP_tecido;
typedef struct
{
int code;
char produto[10];
int min_stock;
int atual_stock;
} MP_borracha;
typedef struct
{
int code;
char produto[10];
int min_stock;
int atual_stock;
} MP_cordoes;
typedef struct
{
int code;
char produto[10];
int min_stock;
int atual_stock;
} MP_palmilhas;
//------------------------------------------------------//
typedef struct
{
MP_couro *couro;
MP_tecido *tecido;
MP_borracha *borracha;
MP_cordoes *cordoes;
MP_palmilhas *palmilhas;
} tabela_stock;//estrutura para interligar as estruturas das matérias-Primas
typedef struct key_value
{
char col0[50];//coluna 1 da tabela de stocks
char col1[50];//coluna 2 da tabela de stocks
char col2[50];//coluna 3 da tabela de stocks
char col3[50];//coluna 4 da tabela de stocks
} row_structure;
typedef struct
{
int id;
char tipo[50];
int tamanho;
int quantidade;
} ordens;//estrutura para as ordens de produ??o
typedef struct
{
int OP;
int materia_prima;
int quantidade;
}ordens_compra;//estrutura para as necessidades de compra
void carregar_stocks(MP_couro *c,MP_tecido *t,MP_borracha *b, MP_cordoes *cs, MP_palmilhas *p);
void editar_stock_min(MP_couro *c,MP_tecido *t,MP_borracha *b, MP_cordoes *cs, MP_palmilhas *p);
void necessidades_de_producao();//!!!!
void free_memoria (tabela_stock *stock);
#ifdef __cplusplus
}
#endif
#endif
我試圖從中讀取的檔案:

uj5u.com熱心網友回復:
你必須在回圈中再次呼叫 strtok
while(token != NULL){// percorrer os tokens, e verificar qual a posi??o do token para preencher corretamente
if(count == 0){op[cont].id = atoi(token);}//atoi recebe string e converte em int
if(count == 1){strcpy(op[cont].tipo,token);}
if(count == 2){op[cont].tamanho = atoi(token);}
if(count == 3){op[cont].quantidade = atoi(token);}
count ;
token = strtok(NULL, ";"); <<<=====
}
否則你會永遠回圈
見https://www.geeksforgeeks.org/strtok-strtok_r-functions-c-examples/
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/427711.html
上一篇:使用Python格式化大文本檔案
下一篇:拆分檔案的內容
