sql - 解析 .sql 文件时输出错误

标签 sql c parsing

我需要编写一个简单的 C 程序,它将表的特定转储作为输入,并在输出中以特定方式排列它。我正在尝试解析我需要的信息并将它们放入内存中,但我有一个问题。我已经输出了数据,并且可以看到“奇怪的字符”作为输出。代码如下:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

typedef struct entityTail
{
    char entity[50];
    struct entityTail* next;
} entityTail;

typedef struct keyTail
{
    char key[300];
    struct keyTail* next;
} keyTail;

typedef struct eventTail
{
    char event[5];
    struct eventTail* next;
} eventTail;

void setATuple(entityTail* entityNew, entityTail* entityNext, keyTail* keyNew, keyTail* keyNext, eventTail* eventNew, eventTail* eventNext, char* startOfParam);

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        printf("Only 1 param needed: the filename");
        return -1;
    }
    char* buffer = NULL; //this buffer will contain all our conf file
    long size = 0;  //in bytes
    long length = 0; //in chars, must match the "size" variable
    FILE* hFile;
    bool exit = false;
    char* startOfTuple = NULL;
    bool headsSet = false;  //used within a cycle

    entityTail* entityHead = (entityTail*) malloc(sizeof(entityTail));
    keyTail* keyHead = (keyTail*) malloc(sizeof(keyTail));
    eventTail* eventHead = (eventTail*) malloc(sizeof(eventTail));
    entityTail* theEntityTail = NULL;
    keyTail* theKeyTail = NULL;
    eventTail* theEventTail = NULL;

    hFile = fopen( argv[1], "r");        //opening the file in "read-only" mode
    if (hFile == NULL)
    {
        printf("Error: the input file doesn't exist\n");
        return -2;     //error opening the file
    }

    //retrieving the size (in bytes) of my text file
    fseek (hFile , 0 , SEEK_END);   //point to the end of the file
    size = ftell (hFile);   //retrieving the size of the file
    rewind (hFile);     //point to the start

    // allocate memory to contain the whole file:
    buffer = malloc (sizeof(char) * size);
    if (buffer == NULL)
    {
        printf("Error: memory error\n");
        return -3; //memory error
    }
    // copy the file into the buffer:
    length = fread (buffer,1,size,hFile);   //reading "size" elements of 1 byte, stored in hFile
    if (length != size)
    {
        printf("Error: read error\n");
        return -4; //reading error, length and size doesn't match
    }
    /* the whole file is now loaded in the memory buffer. */

    // terminate
    fclose (hFile);

    //now we look for the position where our data starts
    //our entity param is just an int, so it will be not enclosed by the ' symbol
    startOfTuple = strstr(buffer, "VALUES"); //finding "VALUES" inside our table dump
    startOfTuple = strstr(startOfTuple + 1, "("); //now we point to our first tuple
    //now we process the rest of our file
    while(!exit)
    {
        entityTail* entityNew = (entityTail*) malloc(sizeof(entityTail));
        keyTail* keyNew = (keyTail*) malloc(sizeof(keyTail));
        eventTail* eventNew = (eventTail*) malloc(sizeof(eventTail));
        if(!headsSet)   //we need to set our heads
        {
            setATuple(entityHead, NULL, keyHead, NULL, eventHead, NULL, startOfTuple);
            theEntityTail = entityHead;
            theKeyTail = keyHead;
            theEventTail = eventHead;
            headsSet = true;
            printf("\n%s %s %s\n", entityHead->entity, keyHead->key, eventHead->event);
        }

        else
        {
            setATuple(entityNew, theEntityTail, keyNew, theKeyTail, eventNew, theEventTail, startOfTuple);
            theEntityTail = entityNew;
            theKeyTail = keyNew;
            theEventTail = eventNew;
            printf("\n%s  %s  %s\n", entityNew->entity, keyNew->key, eventNew->event);
             exit = true; //debugging purpose
        }
        startOfTuple = strchr(startOfTuple + 1 , '('); //pointing to the next tuple
        if(startOfTuple == NULL) exit = true;
    }
    return 0;
}

void setATuple(entityTail* entityNew, entityTail* entityNext, keyTail* keyNew, keyTail* keyNext, eventTail* eventNew, eventTail* eventNext, char* startOfParam)
{
    int i = 0;
    char* endOfParam = NULL;
    //first we reach the "entity" param (an integer)
    for(i = 0; i < 3; i++)
    {
        startOfParam = strchr(startOfParam + 1, ',');
        endOfParam = strchr(startOfParam + 1, ',');
    }
    //now we set the entityHead
    if(entityNext != NULL)
        entityNew->next = entityNext;
    else    //we're setting the heads
        entityNew->next = NULL;
    strncpy(entityNew->entity, startOfParam + 1, endOfParam - startOfParam - 1);
    //now we set the keyHead; that param is enclosed by two ' symbols
    startOfParam = strchr(endOfParam + 1, '\''); //we look now for the ' symbol, it's easier that look for a comma and then remove the ' symbol
    endOfParam = strchr(startOfParam + 1, '\''); //it should work even if that field in the database is empty
    if(keyNext != NULL)
        keyNew->next = keyNext;
    else
        keyNew->next = NULL;
    strncpy(keyNew->key, startOfParam + 1, endOfParam - startOfParam - 1);
    //now we set the eventHead
    startOfParam = strchr(endOfParam + 1, '\''); //we look now for the ' symbol, it's easier that look for a comma and then remove the ' symbol
    endOfParam = strchr(startOfParam + 1, '\''); //it should work even if that field in the database is empty
    if(eventNext != NULL)
        eventNew->next = eventNext;
    else
        eventNew->next = NULL;
    strncpy(eventNew->event, startOfParam + 1, endOfParam - startOfParam - 1);
}

我认为问题与函数setATuple有关,但我不知道出了什么问题。

由于它是一个特定的与文件相关的程序,因此我将复制它应该处理的文件。这是:

-- MySQL dump 10.13  Distrib 5.5.24, for debian-linux-gnu (i686)
--
-- Host: localhost    Database: tesi
-- ------------------------------------------------------
-- Server version   5.5.24-0ubuntu0.12.04.1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `log`
--

DROP TABLE IF EXISTS `log`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `log` (
  `eid` int(11) NOT NULL AUTO_INCREMENT,
  `system` int(11) NOT NULL,
  `host` varchar(15) NOT NULL,
  `entity` int(30) NOT NULL,
  `key` varchar(40) NOT NULL,
  `event` varchar(5) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`eid`),
  KEY `event` (`event`),
  CONSTRAINT `log_ibfk_1` FOREIGN KEY (`event`) REFERENCES `events` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=238 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `log`
--

LOCK TABLES `log` WRITE;
/*!40000 ALTER TABLE `log` DISABLE KEYS */;
INSERT INTO `log` VALUES (1,1,'127.000.000.001',0,'10','CMP','2012-10-09 15:13:37'),(2,1,'127.000.000.001',0,'19','CMP','2012-10-09 15:13:37'),(3,1,'127.000.000.001',0,'38','CMP','2012-10-09 15:13:37'),(4,1,'127.000.000.001',0,'77','CMP','2012-10-09 15:13:37'),(5,1,'127.000.000.001',0,'108','CMP','2012-10-09 15:13:37'),(6,1,'127.000.000.001',0,'119','CMP','2012-10-09 15:13:37'),(7,1,'127.000.000.001',0,'149','CMP','2012-10-09 15:13:37'),(8,1,'127.000.000.001',0,'154','CMP','2012-10-09 15:13:38'),(9,1,'127.000.000.001',1,'34','CMP','2012-10-09 15:13:38'),(10,1,'127.000.000.001',1,'143','CMP','2012-10-09 15:13:38'),(11,1,'127.000.000.001',2,'10','CMP','2012-10-09 15:13:38'),(12,1,'127.000.000.001',2,'30','CMP','2012-10-09 15:13:38'),(13,1,'127.000.000.001',2,'87','CMP','2012-10-09 15:13:38'),(14,1,'127.000.000.001',2,'102','CMP','2012-10-09 15:13:39'),(15,1,'127.000.000.001',2,'125','CMP','2012-10-09 15:13:39'),(16,1,'127.000.000.001',2,'161','CMP','2012-10-09 15:13:39'),(17,1,'127.000.000.001',2,'163','CMP','2012-10-09 15:13:39'),(18,1,'127.000.000.001',2,'181','CMP','2012-10-09 15:13:39'),(19,1,'127.000.000.001',2,'182','CMP','2012-10-09 15:13:39'),(20,1,'127.000.000.001',2,'183','CMP','2012-10-09 15:13:39'),(21,1,'127.000.000.001',3,'0','CMP','2012-10-09 15:13:39'),(22,1,'127.000.000.001',3,'40','CMP','2012-10-09 15:13:39'),(23,1,'127.000.000.001',3,'43','CMP','2012-10-09 15:13:39'),(24,1,'127.000.000.001',3,'101','CMP','2012-10-09 15:13:39'),(25,1,'127.000.000.001',3,'126','CMP','2012-10-09 15:13:40'),(26,1,'127.000.000.001',3,'190','CMP','2012-10-09 15:13:40'),(27,1,'127.000.000.001',4,'21','CMP','2012-10-09 15:13:40'),(28,1,'127.000.000.001',4,'22','CMP','2012-10-09 15:13:40'),(29,1,'127.000.000.001',4,'75','CMP','2012-10-09 15:13:40'),(30,1,'127.000.000.001',4,'88','CMP','2012-10-09 15:13:40'),(31,1,'127.000.000.001',4,'110','CMP','2012-10-09 15:13:40'),(32,1,'127.000.000.001',4,'182','CMP','2012-10-09 15:13:40'),(33,1,'127.000.000.001',5,'66','CMP','2012-10-09 15:13:40'),(34,1,'127.000.000.001',5,'90','CMP','2012-10-09 15:13:41'),(35,1,'127.000.000.001',5,'193','CMP','2012-10-09 15:13:41'),(36,1,'127.000.000.001',6,'57','CMP','2012-10-09 15:13:41'),(37,1,'127.000.000.001',6,'67','CMP','2012-10-09 15:13:41'),(38,1,'127.000.000.001',6,'71','CMP','2012-10-09 15:13:41'),(39,1,'127.000.000.001',6,'73','SER','2012-10-09 15:13:41'),(40,1,'127.000.000.001',6,'90','CMP','2012-10-09 15:13:41'),(41,1,'127.000.000.001',6,'91','CMP','2012-10-09 15:13:41'),(42,1,'127.000.000.001',6,'112','CMP','2012-10-09 15:13:41'),(43,1,'127.000.000.001',6,'126','CMP','2012-10-09 15:13:41'),(44,1,'127.000.000.001',6,'173','CMP','2012-10-09 15:13:41'),(45,1,'127.000.000.001',7,'20','CMP','2012-10-09 15:13:41'),(46,1,'127.000.000.001',7,'22','CMP','2012-10-09 15:13:42'),(47,1,'127.000.000.001',7,'33','CMP','2012-10-09 15:13:42'),(48,1,'127.000.000.001',7,'73','CMP','2012-10-09 15:13:42'),(49,1,'127.000.000.001',7,'97','CMP','2012-10-09 15:13:42'),(50,1,'127.000.000.001',7,'126','CMP','2012-10-09 15:13:42'),(51,1,'127.000.000.001',7,'127','CMP','2012-10-09 15:13:42'),(52,1,'127.000.000.001',7,'170','CMP','2012-10-09 15:13:42'),(53,1,'127.000.000.001',7,'189','CMP','2012-10-09 15:13:42'),(54,1,'127.000.000.001',8,'5','CMP','2012-10-09 15:13:42'),(55,1,'127.000.000.001',8,'80','CMP','2012-10-09 15:13:43'),(56,1,'127.000.000.001',8,'90','CMP','2012-10-09 15:13:43'),(57,1,'127.000.000.001',8,'102','CMP','2012-10-09 15:13:43'),(58,1,'127.000.000.001',8,'158','CMP','2012-10-09 15:13:43'),(59,1,'127.000.000.001',8,'185','CMP','2012-10-09 15:13:43'),(60,1,'127.000.000.001',9,'22','CMP','2012-10-09 15:13:43'),(61,1,'127.000.000.001',9,'34','CMP','2012-10-09 15:13:43'),(62,1,'127.000.000.001',9,'50','CMP','2012-10-09 15:13:43'),(63,1,'127.000.000.001',9,'51','CMP','2012-10-09 15:13:43'),(64,1,'127.000.000.001',9,'89','CMP','2012-10-09 15:13:43'),(65,1,'127.000.000.001',9,'110','CMP','2012-10-09 15:13:43'),(66,1,'127.000.000.001',9,'137','CMP','2012-10-09 15:13:43'),(67,1,'127.000.000.001',9,'141','CMP','2012-10-09 15:13:44'),(68,1,'127.000.000.001',9,'186','CMP','2012-10-09 15:13:44'),(69,1,'127.000.000.001',9,'194','CMP','2012-10-09 15:13:44'),(70,1,'127.000.000.001',10,'23','CMP','2012-10-09 15:13:44'),(71,1,'127.000.000.001',10,'42','CMP','2012-10-09 15:13:44'),(72,1,'127.000.000.001',10,'51','CMP','2012-10-09 15:13:44'),(73,1,'127.000.000.001',10,'52','CMP','2012-10-09 15:13:44'),(74,1,'127.000.000.001',11,'44','CMP','2012-10-09 15:13:44'),(75,1,'127.000.000.001',11,'57','CMP','2012-10-09 15:13:44'),(76,1,'127.000.000.001',11,'123','CMP','2012-10-09 15:13:45'),(77,1,'127.000.000.001',11,'125','CMP','2012-10-09 15:13:45'),(78,1,'127.000.000.001',11,'145','CMP','2012-10-09 15:13:45'),(79,1,'127.000.000.001',12,'6','CMP','2012-10-09 15:13:45'),(80,1,'127.000.000.001',12,'26','CMP','2012-10-09 15:13:45'),(81,1,'127.000.000.001',12,'28','CMP','2012-10-09 15:13:45'),(82,1,'127.000.000.001',12,'61','CMP','2012-10-09 15:13:45'),(83,1,'127.000.000.001',12,'117','CMP','2012-10-09 15:13:45'),(84,1,'127.000.000.001',12,'134','CMP','2012-10-09 15:13:45'),(85,1,'127.000.000.001',12,'139','CMP','2012-10-09 15:13:45'),(86,1,'127.000.000.001',12,'158','CMP','2012-10-09 15:13:45'),(87,1,'127.000.000.001',12,'186','CMP','2012-10-09 15:13:45'),(88,1,'127.000.000.001',12,'189','CMP','2012-10-09 15:13:45'),(89,1,'127.000.000.001',12,'191','CMP','2012-10-09 15:13:46'),(90,1,'127.000.000.001',12,'193','CMP','2012-10-09 15:13:46'),(91,1,'127.000.000.001',13,'31','CMP','2012-10-09 15:13:46'),(92,1,'127.000.000.001',13,'93','CMP','2012-10-09 15:13:46'),(93,1,'127.000.000.001',13,'109','CMP','2012-10-09 15:13:46'),(94,1,'127.000.000.001',13,'149','CMP','2012-10-09 15:13:46'),(95,1,'127.000.000.001',14,'5','CMP','2012-10-09 15:13:46'),(96,1,'127.000.000.001',14,'24','CMP','2012-10-09 15:13:46'),(97,1,'127.000.000.001',14,'128','CMP','2012-10-09 15:13:47'),(98,1,'127.000.000.001',14,'142','CMP','2012-10-09 15:13:47'),(99,1,'127.000.000.001',14,'161','CMP','2012-10-09 15:13:47'),(100,1,'127.000.000.001',15,'17','CMP','2012-10-09 15:13:47'),(101,1,'127.000.000.001',15,'19','CMP','2012-10-09 15:13:47'),(102,1,'127.000.000.001',15,'27','CMP','2012-10-09 15:13:47'),(103,1,'127.000.000.001',15,'71','CMP','2012-10-09 15:13:47'),(104,1,'127.000.000.001',15,'74','CMP','2012-10-09 15:13:47'),(105,1,'127.000.000.001',15,'104','CMP','2012-10-09 15:13:47'),(106,1,'127.000.000.001',15,'126','CMP','2012-10-09 15:13:48'),(107,1,'127.000.000.001',15,'143','CMP','2012-10-09 15:13:48'),(108,1,'127.000.000.001',15,'155','CMP','2012-10-09 15:13:48'),(109,1,'127.000.000.001',15,'185','CMP','2012-10-09 15:13:48'),(110,1,'127.000.000.001',16,'69','CMP','2012-10-09 15:13:48'),(111,1,'127.000.000.001',16,'103','CMP','2012-10-09 15:13:48'),(112,1,'127.000.000.001',16,'139','CMP','2012-10-09 15:13:48'),(113,1,'127.000.000.001',16,'169','CMP','2012-10-09 15:13:48'),(114,1,'127.000.000.001',17,'51','CMP','2012-10-09 15:13:48'),(115,1,'127.000.000.001',17,'67','CMP','2012-10-09 15:13:49'),(116,1,'127.000.000.001',17,'138','CMP','2012-10-09 15:13:49'),(117,1,'127.000.000.001',17,'162','CMP','2012-10-09 15:13:49'),(118,1,'127.000.000.001',17,'173','CMP','2012-10-09 15:13:49'),(119,1,'127.000.000.001',17,'185','CMP','2012-10-09 15:13:49'),(120,1,'127.000.000.001',18,'17','CMP','2012-10-09 15:13:49'),(121,1,'127.000.000.001',18,'57','CMP','2012-10-09 15:13:49'),(122,1,'127.000.000.001',18,'107','CMP','2012-10-09 15:13:49'),(123,1,'127.000.000.001',18,'163','CMP','2012-10-09 15:13:50'),(124,1,'127.000.000.001',18,'193','CMP','2012-10-09 15:13:50'),(125,1,'127.000.000.001',19,'3','CMP','2012-10-09 15:13:50'),(126,1,'127.000.000.001',19,'11','CMP','2012-10-09 15:13:50'),(127,1,'127.000.000.001',19,'36','CMP','2012-10-09 15:13:50'),(128,1,'127.000.000.001',19,'109','CMP','2012-10-09 15:13:50'),(129,1,'127.000.000.001',19,'126','CMP','2012-10-09 15:13:50'),(130,1,'127.000.000.001',19,'194','CMP','2012-10-09 15:13:50'),(131,1,'127.000.000.001',19,'196','CMP','2012-10-09 15:13:50'),(132,1,'127.000.000.001',0,'19','IER','2012-10-09 15:13:55'),(133,1,'127.000.000.001',0,'20','SER','2012-10-09 15:13:55'),(134,1,'127.000.000.001',0,'174','SER','2012-10-09 15:13:55'),(135,1,'127.000.000.001',0,'199','IER','2012-10-09 15:13:55'),(136,1,'127.000.000.001',1,'140','SER','2012-10-09 15:13:55'),(137,1,'127.000.000.001',1,'147','SER','2012-10-09 15:13:56'),(138,1,'127.000.000.001',1,'164','IER','2012-10-09 15:13:56'),(139,1,'127.000.000.001',1,'167','IER','2012-10-09 15:13:56'),(140,1,'127.000.000.001',1,'183','IER','2012-10-09 15:13:56'),(141,1,'127.000.000.001',2,'29','IER','2012-10-09 15:13:56'),(142,1,'127.000.000.001',2,'33','IER','2012-10-09 15:13:56'),(143,1,'127.000.000.001',2,'129','IER','2012-10-09 15:13:56'),(144,1,'127.000.000.001',2,'155','SER','2012-10-09 15:13:56'),(145,1,'127.000.000.001',2,'180','IER','2012-10-09 15:13:56'),(146,1,'127.000.000.001',3,'8','SER','2012-10-09 15:13:57'),(147,1,'127.000.000.001',3,'25','SER','2012-10-09 15:13:57'),(148,1,'127.000.000.001',3,'33','SER','2012-10-09 15:13:57'),(149,1,'127.000.000.001',3,'109','SER','2012-10-09 15:13:57'),(150,1,'127.000.000.001',3,'144','IER','2012-10-09 15:13:57'),(151,1,'127.000.000.001',4,'13','SER','2012-10-09 15:13:57'),(152,1,'127.000.000.001',4,'56','SER','2012-10-09 15:13:57'),(153,1,'127.000.000.001',4,'94','SER','2012-10-09 15:13:58'),(154,1,'127.000.000.001',4,'125','IER','2012-10-09 15:13:58'),(155,1,'127.000.000.001',4,'171','SER','2012-10-09 15:13:58'),(156,1,'127.000.000.001',4,'199','SER','2012-10-09 15:13:58'),(157,1,'127.000.000.001',5,'4','IER','2012-10-09 15:13:58'),(158,1,'127.000.000.001',5,'93','SER','2012-10-09 15:13:58'),(159,1,'127.000.000.001',5,'94','SER','2012-10-09 15:13:58'),(160,1,'127.000.000.001',6,'8','IER','2012-10-09 15:13:58'),(161,1,'127.000.000.001',6,'103','IER','2012-10-09 15:13:59'),(162,1,'127.000.000.001',6,'107','SER','2012-10-09 15:13:59'),(163,1,'127.000.000.001',6,'154','IER','2012-10-09 15:13:59'),(164,1,'127.000.000.001',6,'167','SER','2012-10-09 15:13:59'),(165,1,'127.000.000.001',7,'14','SER','2012-10-09 15:13:59'),(166,1,'127.000.000.001',7,'42','SER','2012-10-09 15:13:59'),(167,1,'127.000.000.001',7,'109','IER','2012-10-09 15:13:59'),(168,1,'127.000.000.001',7,'115','SER','2012-10-09 15:13:59'),(169,1,'127.000.000.001',7,'132','IER','2012-10-09 15:14:00'),(170,1,'127.000.000.001',7,'148','IER','2012-10-09 15:14:00'),(171,1,'127.000.000.001',7,'162','IER','2012-10-09 15:14:00'),(172,1,'127.000.000.001',8,'13','IER','2012-10-09 15:14:00'),(173,1,'127.000.000.001',8,'33','SER','2012-10-09 15:14:00'),(174,1,'127.000.000.001',8,'57','IER','2012-10-09 15:14:00'),(175,1,'127.000.000.001',8,'65','IER','2012-10-09 15:14:00'),(176,1,'127.000.000.001',8,'84','SER','2012-10-09 15:14:00'),(177,1,'127.000.000.001',8,'90','IER','2012-10-09 15:14:00'),(178,1,'127.000.000.001',9,'9','IER','2012-10-09 15:14:01'),(179,1,'127.000.000.001',9,'78','IER','2012-10-09 15:14:01'),(180,1,'127.000.000.001',9,'92','SER','2012-10-09 15:14:01'),(181,1,'127.000.000.001',9,'114','SER','2012-10-09 15:14:01'),(182,1,'127.000.000.001',10,'54','SER','2012-10-09 15:14:01'),(183,1,'127.000.000.001',10,'79','SER','2012-10-09 15:14:01'),(184,1,'127.000.000.001',10,'86','SER','2012-10-09 15:14:01'),(185,1,'127.000.000.001',10,'91','SER','2012-10-09 15:14:01'),(186,1,'127.000.000.001',10,'97','SER','2012-10-09 15:14:01'),(187,1,'127.000.000.001',10,'158','SER','2012-10-09 15:14:02'),(188,1,'127.000.000.001',11,'56','IER','2012-10-09 15:14:02'),(189,1,'127.000.000.001',11,'132','SER','2012-10-09 15:14:02'),(190,1,'127.000.000.001',11,'136','IER','2012-10-09 15:14:02'),(191,1,'127.000.000.001',11,'163','SER','2012-10-09 15:14:02'),(192,1,'127.000.000.001',11,'165','IER','2012-10-09 15:14:02'),(193,1,'127.000.000.001',12,'55','IER','2012-10-09 15:14:02'),(194,1,'127.000.000.001',12,'137','SER','2012-10-09 15:14:02'),(195,1,'127.000.000.001',12,'142','IER','2012-10-09 15:14:02'),(196,1,'127.000.000.001',12,'192','IER','2012-10-09 15:14:03'),(197,1,'127.000.000.001',13,'53','IER','2012-10-09 15:14:03'),(198,1,'127.000.000.001',13,'56','SER','2012-10-09 15:14:03'),(199,1,'127.000.000.001',13,'135','IER','2012-10-09 15:14:03'),(200,1,'127.000.000.001',13,'139','SER','2012-10-09 15:14:03'),(201,1,'127.000.000.001',13,'143','IER','2012-10-09 15:14:03'),(202,1,'127.000.000.001',13,'155','IER','2012-10-09 15:14:03'),(203,1,'127.000.000.001',13,'178','IER','2012-10-09 15:14:03'),(204,1,'127.000.000.001',13,'181','IER','2012-10-09 15:14:03'),(205,1,'127.000.000.001',13,'182','SER','2012-10-09 15:14:03'),(206,1,'127.000.000.001',14,'25','SER','2012-10-09 15:14:04'),(207,1,'127.000.000.001',14,'55','IER','2012-10-09 15:14:04'),(208,1,'127.000.000.001',14,'66','SER','2012-10-09 15:14:04'),(209,1,'127.000.000.001',14,'70','SER','2012-10-09 15:14:04'),(210,1,'127.000.000.001',14,'86','SER','2012-10-09 15:14:04'),(211,1,'127.000.000.001',14,'93','SER','2012-10-09 15:14:04'),(212,1,'127.000.000.001',14,'108','IER','2012-10-09 15:14:04'),(213,1,'127.000.000.001',14,'183','SER','2012-10-09 15:14:04'),(214,1,'127.000.000.001',15,'24','IER','2012-10-09 15:14:04'),(215,1,'127.000.000.001',15,'65','IER','2012-10-09 15:14:04'),(216,1,'127.000.000.001',15,'153','IER','2012-10-09 15:14:05'),(217,1,'127.000.000.001',15,'157','SER','2012-10-09 15:14:05'),(218,1,'127.000.000.001',16,'60','IER','2012-10-09 15:14:05'),(219,1,'127.000.000.001',16,'108','IER','2012-10-09 15:14:05'),(220,1,'127.000.000.001',16,'148','IER','2012-10-09 15:14:05'),(221,1,'127.000.000.001',16,'170','IER','2012-10-09 15:14:05'),(222,1,'127.000.000.001',16,'181','SER','2012-10-09 15:14:05'),(223,1,'127.000.000.001',17,'20','IER','2012-10-09 15:14:05'),(224,1,'127.000.000.001',17,'134','SER','2012-10-09 15:14:06'),(225,1,'127.000.000.001',17,'200','IER','2012-10-09 15:14:06'),(226,1,'127.000.000.001',18,'4','SER','2012-10-09 15:14:06'),(227,1,'127.000.000.001',18,'63','IER','2012-10-09 15:14:06'),(228,1,'127.000.000.001',18,'74','SER','2012-10-09 15:14:06'),(229,1,'127.000.000.001',18,'84','SER','2012-10-09 15:14:06'),(230,1,'127.000.000.001',18,'88','IER','2012-10-09 15:14:06'),(231,1,'127.000.000.001',18,'94','SER','2012-10-09 15:14:06'),(232,1,'127.000.000.001',18,'108','IER','2012-10-09 15:14:06'),(233,1,'127.000.000.001',19,'24','SER','2012-10-09 15:14:07'),(234,1,'127.000.000.001',19,'88','SER','2012-10-09 15:14:07'),(235,1,'127.000.000.001',19,'137','IER','2012-10-09 15:14:07'),(236,1,'127.000.000.001',19,'142','SER','2012-10-09 15:14:07'),(237,1,'127.000.000.001',19,'187','SER','2012-10-09 15:14:07');
/*!40000 ALTER TABLE `log` ENABLE KEYS */;

一旦到达第二个元组,它就会“死亡”。抱歉我的英语不好,我来自意大利。感谢您的帮助!

最佳答案

我无法调试你的代码; :-(,不过我重写了这段代码,看看你是否可以使用它:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

/*
 * Apparently only interested in 4,5 & 6th comma separated values
 * strtok_r() should be preferred over strtok(); Windows MINGW doesn't have it
 */
int process_record(char *recordPtr/* and , your DataStructure Ptr if necessary */)
{
    unsigned count;
    char* fieldPtr = NULL;

    fprintf(stderr, "\nFields :");
    for (count = 1; NULL != (fieldPtr = strtok_r(NULL, ",", &recordPtr)); count++)
    {

        if (count > 3 && count < 7)
        {// required fields range

            /*TODO Trim unwanted characters */

            switch (count)
            {

            case 4: // copy to relevant DS
                break;
            case 5: // copy to relevant DS
                break;
            case 6: // copy to relevant DS
                break;
            }

            fprintf(stderr, "\t %s", fieldPtr);
        }

    }

    return 0;
}



int main(int argc, char* argv[])
{
    int status;
    unsigned count;
    unsigned readBytes = 0;
    struct stat fStat;
    unsigned fileSize ;
     FILE* hFile;
    char*   fileData = NULL;
    char* dataPtr =NULL;
    char* recordPtr = NULL;

    fprintf (stderr, "\n");
    if (argc != 2)
    {
        fprintf(stderr, "\nERROR: Only <filename> required");
        return -1;
    }

    /*
     * Get the file size for Memory Load
     */
    status = stat(argv[1], &fStat);
    if (status < 0) {
        fprintf(stderr, "\nERROR: Line[%d] ErroNo[%d]", __LINE__, errno);
        return -2;
    }

    fileSize = fStat.st_size;
    fprintf (stderr, "\nDump File size[%u]", fileSize);

    /*
     * Allocate sufficient memory to load file data on to memory
     */
    fileData = (char*) malloc (sizeof(char) * fileSize);
    if (NULL == fileData) {
        fprintf(stderr, "\nERROR: Allocating memory");
        return -3;
    }

    /* Open file and read data on to memory
     */
    hFile = fopen( argv[1], "r");        //opening the file in "read-only" mode
    if (hFile == NULL)
    {
        fprintf(stderr, "ERROR: Opening file[%s] ErroNo[%d]\n", argv[1], errno);
        return -4;     //error opening the file
    }
    /* assuming file will be read within 3 attempts */
    for (status =0; fileSize != readBytes && status < 3; status ++) {
        readBytes += fread (fileData + readBytes, 1, fileSize - readBytes, hFile);
        fprintf (stderr, "\nData read Size [%u]", readBytes);
    }

    fclose (hFile);
    //fprintf (stderr, "\nNOTE: Data read[\n%s\n]", fileData);

    /*
     * Chunk it into manageable records
     */
    dataPtr = strstr (fileData, "VALUES (") + strlen ("VALUES ("); //handle error checks

    for(count =1; NULL != (recordPtr = strtok_r(NULL, "()", &dataPtr)); count ++) {

        if (strlen(recordPtr) > 1) { // NOTE: "," between each record will be tokenised
            //fprintf (stderr, "\nRecord[%d] : [%s]", count, recordPtr);

            /*  Process Record
             */
            process_record (recordPtr /* and , your DataStructure Ptr if necessary */);
        }
    }

    free (fileData);
    return 0;
}

关于sql - 解析 .sql 文件时输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12824552/

相关文章:

python - 将 FILE * 传递给 Python/ctypes 中的函数

sql - 从 SQL Server 中的 xml,获取下一个兄弟值

sql - 有向图 SQL

c - 在 C 二进制文件中将 int 编码并组合为 32 位 int

C Socket/Client fork(),共享结构体内存

Python解析csv文件——用冒号代替逗号

c - 用 C 构建一个非常简单的解析器

java - 从网站获取数据到 Google Glass

sql - 保持 SQL 干燥

php - 通过 PHP 脚本 : connection doesn't seem to work, 获取 SQL 数据但没有错误,if 语句将变量视为非假