简体   繁体   中英

Reading a csv file into a struct in C using scanf

I'm having an issue reading in from a csv data file into in to a node for a binary search tree in C. It seems as though none of the data is actually being read into the struct. The code I am using now is just trying to read a single line of data from the csv file before I can scale it up to read the whole thing, but even with this I am not getting the results. I am aware there are probably many large issues in this code as I am not extremely competent with the language however any insight would be appreciated.

typedef struct{
  struct bst_t *left;
  struct bst_t *right;
  data_t data;
} bst_t;

Here is my read function

void readdata(bst_t node){
  while(
  scanf("%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],
   %[^,],%[^,],%[^,],%[^,] ,[^,],%[^,],%[^,] ... ) == 14);
}

Here is my print function

void printdata(bst_t node){
  printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s \n"...);
}

however my output is simply:

-bash-4.1$ print

,,,,,,,,,,.,(@,,▒

Another additional problem that I am yet to face is that some of the data within the file will have commas within an entry, how would I go about 'ignoring' these commas so they appear as data and not a separator in the file?

Once again any help at all would be much appreciated.

EDIT: This is where I call the functions: (ie main)

int main(int argc, char *argv[]) {
bst_t node;
readdata(*node);
printdata(node);
return 0;

}

New Compiler code

print.c: In function 'main':
print.c:37: error: invalid type argument of 'unary *' (have 'bst_t')
print.c: In function 'readdata':
print.c:56: error: request for member 'node' in something not a structure     or union

Here is the complete code :

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


#define MAXSTRING 128


typedef struct{
  struct bst_t *left;
  struct bst_t *right;
  struct data_t data;
} bst_t;

void readdata(bst_t *node);
void printdata(bst_t node);

int main(int argc, char *argv[]) {
    bst_t node;
    readdata(&node);
    printdata(node);
    return 0;
}


void readdata(bst_t *node){
  while(
  scanf("%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,] \n",...) == 14)

}

The read function only updates the local variable node it receives by value as an argument. You must pass a pointer to a newly allocated structure:

void readdata(bst_t *node) {
    while (scanf(" %[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],[^,],%[^,],%[^,]",
                 node->data.ID, node->data.name, node->data.sex,
                 node->data.height, node->data.weight, node->data.team,
                 node->data.NOC, node->data.games, node->data.year,
                 node->data.season, node->data.city, node->data.sport,
                 node->data.event, node->data.medal) == 14) {
        continue;
    }
} 

You would call this function this way from main :

int main(int argc, char *argv[]) {
    bst_t node;
    readdata(&node);
    printdata(node);
    return 0;
}

Note however that this function is unsafe: it does not protect against buffer overflows.

Note also that it cannot handle empty fields, nor fields with embedded commas.

To parse the input correctly, you need a hand coded parser that handles special cases and provides precise error reports.

EDIT: The source code you posted has a syntax error:

               node->data.event->node.data.medal) == 14)

It should read:

               node->data.event, node->data.medal) == 14)
        continue;

You should format your code more readably, by indenting statements by 4 spaces and inserting spaces around binary operators and after , .

Try this version of readdata : (copy and paste from the web page)

void readdata(bst_t *node) {
    while (scanf(" %[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],[^,],%[^,],%[^,]",
                 node->data.ID, node->data.name, node->data.sex,
                 node->data.height, node->data.weight, node->data.team,
                 node->data.NOC, node->data.games, node->data.year,
                 node->data.season, node->data.city, node->data.sport,
                 node->data.event, node->data.medal) == 14) {
        continue;
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM