Given a struct:
struct Info {
char name[70];
char address[120];
char email[100];
} info[100];
How would you transfer a .csv
file into this structure?
EXAMPLE
Take this .csv
file
name,address,email
Carl March,3112 Agriculture Lane,[email protected]
Annie Scrims,1773 Sycamore Lake Road,[email protected]
How would you transfer all of this into the info
structure? The white spaces are causing an issue and using a for loop seems way too clunky.
CodePudding user response:
Instead of using scanf("%s,%s,%s", ...)
you should use %[^,\n]
.
Here is an example:
#include <stdio.h>
struct Info {
char name[70];
char address[120];
char email[100];
} info[100];
int main() {
char buf[1024], eol[2];
int i = 0;
while (i < 100 && fgets(buf, sizeof buf, stdin)) {
if (sscanf(buf, "i[^,\n],9[^,\n],