How to read a CSV file in C - c

I am trying to read a CSV file of the following format:
5,455,78,5
12245,4,78
1,455,4557,1,8,9
I have managed to open the file but I have no idea how to interpret the data. All the data is written in the first column, but I do not know how many rows there are or how many entries there is in each row.
This is my code for opening the file.
printf("File chosen is: %s",file);
int p=0;
FILE *myFile = NULL;
myFile = fopen(file,"r");
if (myFile == NULL)
{
exit(1);
}
if (myFile != NULL)
{
printf("\n\nFile read succesfully");
}

This should parse your csv. After opening you file, read each line using fgets. Loop through until fgets returns NULL which indicates no line could be read and you reached the end of your file. Use strtok to parse your line from fgets using the comma as your delimiter.
#include <stdio.h> // file handling functions
#include <stdlib.h> // atoi
#include <string.h> // strtok
...
char buffer[80];
while (fgets(buffer, 80, myFile)) {
// If you only need the first column of each row
char *token = strtok(buffer, ",");
if (token) {
int n = atoi(token);
printf("%d\n", n);
}
// If you need all the values in a row
char *token = strtok(buffer, ",");
while (token) {
// Just printing each integer here but handle as needed
int n = atoi(token);
printf("%d\n", n);
token = strtok(NULL, ",");
}
}
...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char* getfield(char* line, int num)
{
const char* tok;
for (tok = strtok(line, ",");
tok && *tok;
tok = strtok(NULL, ",\n"))
{
if (!--num)
return tok;
}
return NULL;
}
int main()
{
FILE *stream = fopen("yourfile.csv", "r");
int i = 0;
int j = 0;
printf("Choose a line to be given its elements: ");
scanf("%d", &j);
char line[1024];
while (fgets(line, 1024, stream))
{
char* tmp = _strdup(line);
i++;
printf("Element %d would be %s\n", i, getfield(tmp, j));
free(tmp);
}
}

Thank you for posting some code, but you don't mention what you wish to do with your data once you read it in.
I'll give you some pointers:
Use an array of known size to read your data into from the file and buffer it for processing. Run this in a loop. e.g.
char buffer[1000];
while (fgets(buffer, sizeof (buffer), myFile))
{
char *data = strtok(buffer, ",");
printf("Data %s\n", data);
/* Further processing of data */
data = strtok(NULL, ",");
}
fclose(myFile);
Process that buffer using strtok to separate out your strings. The token is the data delimiter which should be ',' but I'm not clear on whether you also have a newline character in there, but it needs to be consistent.
Handle your strings returned above.

Related

ascii file processing in C

I have a hard time understanding how you process ascii files in c. I have no problem opening files and closing them or reading files with one value on each line. However, when the data is separated with characters, I really don't understand what the code is doing at a lower level.
Example: I have a file containing names separated with comas that looks like this:
"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER"
I have created an array to store them:
char names[6000][20];
And now, my code to process it is while (fscanf(data, "\"%s\",", names[index]) != EOF) { index++; }
The code executes for the 1st iteration and names[0] contains the whole file.
How can I separate all the names?
Here is the full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char names[6000][20]; // an array to store 6k names of max length 19
FILE * data = fopen("./022names.txt", "r");
int index = 0;
int nbNames;
while (fscanf(data, "\"%s\",", names[index]) != EOF) {
index++;
}
nbNames = index;
fclose(data);
printf("%d\n", index);
for (index=0; index<nbNames; index++) {
printf("%s \n", names[index]);
}
printf("\n");
return 0;
}
PS: I am thinking this might also be because of the data structure of my array.
If you want a simple solution, you can read the file character by character using fgetc. Since there are no newlines in the file, just ignore quotation marks and move to the next index when you find a comma.
char names[6000][20]; // an array to store 6k names of max length 19
FILE * data = fopen("./022names.txt", "r");
int name_count = 0, current_name_ind = 0;
int c;
while ((c = fgetc(data)) != EOF) {
if (c == ',') {
names[name_count][current_name_ind] = '\0';
current_name_ind = 0;
++name_count;
} else if (c != '"') {
names[name_count][current_name_ind] = c;
++current_name_ind;
}
}
names[name_count][current_name_ind] = '\0';
fclose(data);
"The code executes for the 1st iteration and names[0] contains the whole file...., How can I separate all the names?"
Regarding the first few statements:
char names[6000][20]; // an array to store 6k names of max length 19
FILE * data = fopen("./022names.txt", "r");
What if there are there are 6001 names. Or one of the names has more than 20 characters?
Or what if there are way less than 6000 names?
The point is that with some effort to enumerate the tasks you have listed, and some time mapping out what information is needed to create the code that matches your criteria, you can create a better product: The following is derived from your post:
Process ascii files in c
Read file content that is separated by characters
input is a comma separated file, with other delimiters as well
Choose a method best suited to parse a file of variable size
As mentioned in the comments under your question there are ways to create your algorithms in such way as to flexibly allow for extra long names, or for a variable number of names. This can be done using a few C standard functions commonly used in parsing files. ( Although fscanf() has it place, it is not the best option for parsing file contents into array elements.)
The following approach performs the following steps to accomplish the user needs enumerated above
Read file to determine number of, and longest element
Create array sized to contain exact contents of file using count of elements and longest element using variable length array (VLA)
Create function to parse file contents into array. (using this technique of passing VLA as function argument.)
Following is a complete example of how to implement each of these, while breaking the tasks into functions when appropriate...
Note, code below was tested using the following input file:
names.txt
"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER",
"Joseph","Bart","Daniel","Stephan","Karen","Beth","Marcia",
"Calmazzothoulumus"
.
//Prototypes
int count_names(const char *filename, size_t *count);
size_t filesize(const char *fn);
void populateNames(const char *fn, int longest, char arr[][longest]);
char *filename = ".\\names.txt";
int main(void)
{
size_t count = 0;
int longest = count_names(filename, &count);
char names[count][longest+1];//VLA - See linked info
// +1 is room for null termination
memset(names, 0, sizeof names);
populateNames(filename, longest+1, names);
return 0;
}
//populate VLA with names in file
void populateNames(const char *fn, int longest, char names[][longest])
{
char line[80] = {0};
char *delim = "\",\n ";
char *tok = NULL;
FILE * fp = fopen(fn, "r");
if(fp)
{
int i=0;
while(fgets(line, sizeof line, fp))
{
tok = strtok(line, delim);
while(tok)
{
strcpy(names[i], tok);
tok = strtok(NULL, delim);
i++;
}
}
fclose(fp);
}
}
//passes back count of tokens in file, and return longest token
int count_names(const char *filename, size_t *count)
{
int len=0, lenKeep = 0;
FILE *fp = fopen(filename, "r");
if(fp)
{
char *tok = NULL;
char *delim = "\",\n ";
int cnt = 0;
size_t fSize = filesize(filename);
char *buf = calloc(fSize, 1);
while(fgets(buf, fSize, fp)) //goes to newline for each get
{
tok = strtok(buf, delim);
while(tok)
{
cnt++;
len = strlen(tok);
if(lenKeep < len) lenKeep = len;
tok = strtok(NULL, delim);
}
}
*count = cnt;
fclose(fp);
free(buf);
}
return lenKeep;
}
//return file size in bytes (binary read)
size_t filesize(const char *fn)
{
size_t size = 0;
FILE*fp = fopen(fn, "rb");
if(fp)
{
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
fclose(fp);
}
return size;
}
You can use the in-built strtok() function which is easy to use.
I have used the tok+1 instead of tok to omit the first " and strlen(tok) - 2 to omit the last ".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char names[6000][20]; // an array to store 6k names of max length 19
FILE * data = fopen("./022names.txt", "r");
int index = 0;
int nbNames;
char *str = (char*)malloc(120000*sizeof(char));
while (fscanf(data, "%s", str) != EOF) {
char *tok = strtok(str, ",");
while(tok != 0){
strncpy(names[index++], tok+1, strlen(tok)-2);
tok = strtok(0, ",");
}
}
nbNames = index;
fclose(data);
free(str); // just to free the memory occupied by the str variable in the heap.
printf("%d\n", index);
for (index=0; index<nbNames; index++) {
printf("%s \n", names[index]);
}
printf("\n");
return 0;
}
Also, the parameter 120000 is just the maximum number of characters that can be in the file. It is just 6000 * 20 as you mentioned.

C Programming - can't copy string from buffer to given char array using pointers

I'm not sure where I'm messing up so I've given a summary of each function so my logic can be checked!
The main program takes arguments from the command line and stores them in char pointer array.
The correct command to run program is ./re-do_hw4_prob6 filename. (filename is sears_kmart_stores_closing_2019.txt in this case)
After checking if argument number is correct, the file is opened.
A while loop copies strings of text from file to buffer until NULL is met.
Then the function getState() is called. The state is printed.
The file is closed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "redo_hw4_functs.h"
int main(int argc, char* argv[])
{
char** states;
FILE* pFile;
char buffer[80];
int i = 0;
if(argc < 2){
printf("Too few arguments! \n");
}
else if(argc > 2){
printf("Too many arguments! \n");
}
pFile = fopen(argv[1], "r");
states = malloc(50*sizeof(char));
for (i = 0; i < 50; i++)
{
states[i] = malloc(3*sizeof(char));
while(fgets(buffer, sizeof(buffer), pFile) != NULL)
{
getState(states[i], buffer);
printf("State: %s \n", states[i]);
}
}
fclose(pFile);
}
The getState() function takes in two char arrays. One to read from the other to copy too.
It tokenizes the string being read from using a comma, a tab, and a new line as the delimiters. -> ",\t\n"
On the last token it copies the last two chars to the empty string array.
//accepts a line of string formatted as expected and stores the store state in char file ¡OJO! This is the hardest one because you cant rely on delimeters alone to find state
void getState(char strState[], char strLine[])
{
int i;
char* token;
char delim[] = ",\t\n";
token = strtok(strLine, delim);
token = strtok(strLine, delim);
token = strtok(strLine, delim);
for(i = (strlen(token) - 2); i < strlen(token); i++)
{
strState[i] =token[i];
}
}
I have also included my other functions to see if there are any other mistakes to be corrected.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "redo_hw4_functs.h"
//accepts a line of string formatted as expected and stores the store name in char file
void getName(char strName[], char strLine[])
{
char* token;
char delim[] = " ,\t\n";
token = strtok(strLine, delim);
while(token != NULL)
{
if(strcmp(token, "sears") == 0 || strcmp(token, "kmart"))
{
strcpy(strName, token);
break;
}
token = strtok(NULL, delim);
}
}
//accepts a line of string formatted as expected and stores the store address in char file
void getAddress(char strAddress[], char strLine[])
{
char* token;
char delim[] = ",\t\n";
token = strtok(strLine, delim);
while(token != NULL)
{
if(isdigit(token[0]) && isalpha(token[sizeof(token)-1]))
{
strcpy(strAddress, token);
break;
}
token = strtok(NULL, delim);
}
}
//accepts a line of string formatted as expected and stores the store city in char file
void getCity(char strCity[], char strLine[])
{
int i;
char* token;
char delim[] = ",\t\n";
token = strtok(strLine, delim);
token = strtok(strLine, delim);
token = strtok(strLine, delim);
for(i = 0; i < (strlen(token) - 3); i++)
{
strcpy(strCity[i], token[i]);
}
}
//accepts a line of string formatted as expected and stores the store state in char file ¡OJO! This is the hardest one because you cant rely on delimeters alone to find state
void getState(char strState[], char strLine)
{
int i;
char* token;
char delim[] = ",\t\n";
token = strtok(strLine, delim);
token = strtok(strLine, delim);
token = strtok(strLine, delim);
for(i = (strlen(token) - 2); i < strlen(token); i++)
{
strcpy(strState[i], token[i]);
}
}
Here is an example of input text that is to be read:
Kmart, 217 Forks Of River Pkwy, Sevierville TN
Kmart, 4110 E Sprague Ave, Spokane WA
Kmart, 1450 Summit Avenue, Oconomowoc WI
Sears, 2050 Southgate Rd, Colorado Spgs CO
Sears, 1650 Briargate Blvd, Colorado Spgs CO
Sears, 3201 Dillon Dr, Pueblo CO
Here is an example of what the program is expected to be outputting:
State:TN
State:WA
State:WI
State:CO
State:CO
State:CO
Here is an example of what the program is outputting:
I assume that you want not only the status but also all the other fields so that you can deal with them later.
The code below may be quite different from yours, but I think that it is easier to use a single function to read each record.
The function read_data() reads data from the file pointer fp and store them in data, which is a pointer to a predefined struct data_t.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
typedef struct {
char name[64];
char addr[64];
char city[64];
char state[8];
} data_t;
int read_data(FILE *fp, data_t *data) {
char buffer[BUFFER_SIZE];
// Read a record. If end-of-file is read, return -1.
if (fgets(buffer, BUFFER_SIZE, fp) == NULL) {
return -1;
}
char delim[] = ",\t\n";
// Find the name of the record.
char *token = strtok(buffer, delim);
strcpy(data->name, token);
// Find the address of the record.
token = strtok(NULL, delim);
while (*token == ' ') {
++token;
}
strcpy(data->addr, token);
// Find the city and status of the record.
// We cannot split them by strtok() easily, so we handle it later.
token = strtok(NULL, delim);
while (*token == ' ') {
++token;
}
// Find the position of the state.
char *ptr = token;
while (*ptr != '\0') {
++ptr;
}
ptr -= 2;
strcpy(data->state, ptr);
// Use NULL to separate the city and the state so that we can use strcpy().
while (*(ptr - 1) == ' ') {
--ptr;
}
*ptr = '\0';
// Copy the city field.
strcpy(data->city, token);
return 0;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "The number of arguments is incorrect.\n");
fprintf(stderr, "Usage: %s filename\n", argv[0]);
return -1;
}
FILE *fp = fopen(argv[1], "r");
data_t *data = malloc(sizeof(data_t));
while (read_data(fp, data) == 0) {
printf("State: %s\n", data->state);
}
free(data);
fclose(fp);
return 0;
}
The way to read data is hard-coded, so if the input format is different, you may need to change the content of read_data(), but it works well for your sample input.

C - Read from .INI file and pass values to vars

I'm doing a project for school and I need to read from an .INI file to start my vars for the game. Problem is, I cannot seem to understand how strtok works and I'm really confused at this point.
I know I'm returning an empty struct because I have no idea how to put the specific values into the vars!
Here's my read_from_config.h
#ifndef READ_FROM_CONFIG_H
#define READ_FROM_CONFIG_H
#define MAXSTR 500
typedef struct {
unsigned int xdim;
unsigned int ydim;
unsigned int nzombies;
unsigned int nhumans;
unsigned int nzplayers;
unsigned int nhplayers;
unsigned int turns;
} CONFIG;
CONFIG read_config(char *argv[]);
#endif
And here is my read_from_config.c
#include "read_from_config.h"
#include "example.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
CONFIG read_config(char *argv[]) {
char str[MAXSTR];
FILE *fp = NULL;
char *filename = argv[1];
char *token;
fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "Não foi possível abrir ficheiro de configuração.");
fprintf(stderr, "\nModo de uso: ./program_name config.ini");
}
while (fgets(str, MAXSTR, fp) != NULL) {
for (int i = 0; i != '\0'; i++) {
char *equal = strpbrk (str, "=");
if (equal) {
token = strtok(str, "=");
}
}
}
printf("%d", token[0]);
CONFIG config;
return config;
}
CONFIG.INI
; Dimension of the grid
xdim=20
ydim=20
; Inicial number of zombies and humans
nzombies=20
nhumans=20
; Number of zombies and humans controlled by players
nzplayers=0
nhplayers=1
; Number of maximum turns
maxturns=1000
The function strtok take a string only the first time it gets called. All
subsequent calls must be passed with NULL
man strtok
#include <string.h>
char *strtok(char *str, const char *delim);
DESCRIPTION
The strtok() function breaks a string into a sequence of zero or more nonempty tokens.
On the first call to strtok(), the string to be parsed should be specified in str.
In each subsequent call that should parse the same string, str must be NULL.
Example:
char line[] = "a,b,c,d,e,f\n"; // to simulate an fgets line
char *token = strtok(line, ",\n"); // you can add \n to the separator
// to get rid of the \n at the end
puts(token); // prints a
while(token = strtok(NULL, ",\n"))
puts(token); // prints b then c etc..
Keep in mind that strtok modifies the source, this is going to fail:
strtok("a,b,c", ",");
because string literals are not modifiable. In that case you have to make a copy
to a char[] or a dynamic allocated char*.
If you need to have the source intact after the parsing, then you definitively
need to make a copy.
In your code you do:
printf("%d", token[0]);
That's not incorrect but perhaps not what you want to do. This line doesn't
print the first character, it prints the ascii value of the first character.
This
printf("%c", token[0]);
will print the first character.
Also you are doing
CONFIG read_config(char *argv[]) {
...
CONFIG config;
return config;
}
You are returning an uninitialized CONFIG object, you are ignoring the parsing
and nothing is set in your config object.
Your parsing is also a little bit strange.
for (int i = 0; i != '\0'; i++)
The loop exits immediately because 0 == '\0'! I don't understand what you are
trying to do with it.
I would first create a helper function to populate the values of the config, see
set_config_val. Then you can parse it like this:
CONFIG read_config(char *argv[]) {
...
const char *delim = "=\n";
CONFIG config;
while (fgets(str, MAXSTR, fp) != NULL) {
if(strchr(str, '='))
{
char varname[100];
int value;
token = strtok(line, delim);
strcpy(varname, token);
token = strtok(NULL, delim);
value = atoi(token);
set_config_val(&config, varname, value);
} else {
fprintf(stderr, "Skipping line, no = found");
}
}
fclose(fp);
return config;
}
void set_config_val(CONFIG *config, const char *key, int val)
{
if(config == NULL)
return;
if(strcmp(key, "xdim") == 0)
config->xdim = val;
else if(strcmp(key, "ydim") == 0)
config->ydim = val;
...
}

How do I read a comma separated line in a text file and insert its fields into an array of struct pointers?

I've been trying to figure this one out for a while now, and I feel like I have to be close. Basically, I have a data file containing various country records separated by new lines. Each record contains comma separated fields, of which I am trying to extract certain ones.
For example (as a single line):
60,AFG,Afghanistan,Asia,Southern and Central Asia,652090,1919,22720000,45.9,5976,Afganistan/Afqanestan,Islamic Emirate,Mohammad Omar,1,AF
Each one of these lines will make up a struct. Essentially, I want to read each one of these lines and insert it into an array of struct pointers (so dynamically). I also only want specific fields. When I "tokenize" the line I want the fields for code, name, population, and life expec. respectively:
AFG, Afghanistan, 22720000, 45.
My thought was to use fgets() to read each line in the file, and in a loop malloc() some memory for the pointers, tokenize on the fields I want, then insert. However, something that I'm doing must be wrong, as various tests don't seem to show anything in my output.
Here is my work thus far. I would appreciate any and all help.
#include "allheaders.h" // contains all common headers for personal use
#define BUF_SIZE 512
#define NUM_RECS 238
typedef struct {
char code[4];
char name[40];
int population;
float lifeExpectancy;
} Country;
typedef Country *countryPtr;
int main( int argc, const char* argv[] ) {
/* Opening the file */
FILE *filePtr; // pointer to file
if ((filePtr = fopen("AllCountries.dat", "r")) == NULL) { // if couldn't open file
printf("Error opening file\n"); // error message
exit(1);
}
/* Reading the file */
char buffer[BUF_SIZE]; // buffer to read
int index = 0;
char *token;
countryPtr *myCountries = malloc(sizeof(*myCountries) * NUM_RECS);
for(int i = 0; i < NUM_RECS; ++i) {
myCountries[i] = malloc(sizeof(*myCountries[i]));
}
while (fgets(buffer, BUF_SIZE, filePtr) != NULL) {
token = strtok(buffer,",");
token = strtok(NULL, ",");
strcpy(myCountries[index]->code, token);
token = strtok(NULL, ",");
strcpy(myCountries[index]->name, token);
token = strtok(NULL, ",");
token = strtok(NULL, ",");
token = strtok(NULL, ",");
token = strtok(NULL, ",");
token = strtok(NULL, ",");
myCountries[index]->population = atoi(token);
token = strtok(NULL, ",");
myCountries[index]->lifeExpectancy = atof(token);
//printf("%s", buffer);
index++;
}
printf("%s", myCountries[1]->code); // test?
free(myCountries);
}
Have a look at the following.
In the first instance you will need to do some work to improve the areas marked NYI
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#define BUF_SIZE 512
#define NUM_RECS 238
typedef struct {
char code[4]; // NYI - magic numbers
char name[41]; // NYI - magic numbers
int population; // NYI - what if atoi fails?
float lifeExpectancy; // NYI - what if atof fails?
} Country;
typedef Country* countryPtr;
int main( int argc, const char* argv[] ) {
/* Opening the file */
FILE *filePtr; // pointer to file
if ((filePtr = fopen("a.txt", "r")) == NULL) { // if couldn't open file
printf("Error opening file\n"); // error message
exit(1);
}
/* Reading the file */
char buffer[BUF_SIZE]; // buffer to read
int index=0;
char *token; // NYI - initial value
countryPtr* myCountries = calloc(NUM_RECS, sizeof(countryPtr));
for(int i = 0; i < NUM_RECS; ++i) {
myCountries[i] = calloc(1, sizeof(Country));
}
while (fgets(buffer, BUF_SIZE, filePtr) != NULL) {
// NYI - magic lengths / overflow strcpy targets
token = strtok(buffer,","); // NYI - This is probably not the best way to do this. At least fold into a loop.
token = strtok(NULL, ",");
strcpy(myCountries[index]->code, token);
token = strtok(NULL, ",");
strcpy(myCountries[index]->name, token);
token = strtok(NULL, ",");
token = strtok(NULL, ",");
token = strtok(NULL, ",");
token = strtok(NULL, ",");
token = strtok(NULL, ",");
myCountries[index]->population = atoi(token); // NYI - atoi failure
token = strtok(NULL, ",");
myCountries[index]->lifeExpectancy = atof(token); // NYI - atof failure
printf("%s", buffer);
index++;
}
printf("%s\n", myCountries[0]->code); // test? NYI - need more proof
free(myCountries); // NYI - this is a sequence - need to free each of the new elements
}
I took a different approach to solving it based on your code and data file. I tested it. It works with a file of the record type you showed. Hopefully it will explain some things and make your work easier and give you a good place to work from.
I don't like to write programs in a way that has to pre-count (time consuming) or pre-know the number of records in a file on general principles except maybe in rare cases. So when reading files I prefer to allocate memory as I go. Now if there's a big file and a lot of data, then you have to come up with a better memory management scheme than to keep it all in memory. At some point you're better off going with a canned db solution of some sort. MySQL, an API, library, parser, etc... but this should work for small files.
Usually in C on UNIX, exit(0) means success, exit(-1) means failure. Also since your country codes were 3 characters, the field to hold it has to be at least 4 characters for the trailing '\0'
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#define MAXRECL 512
#define MAXFIELDS 100
#define MAXFIELDL 80
// Field indicies
#define COUNTRY_CODE 1
#define COUNTRY_NAME 2
#define POPULATION 7
#define LIFE_EXPECTANCY 8
#define CCMAX 3
#define CNMAX 40
typedef struct Country {
struct Country *next;
char code[CCMAX + 1]; // (Need room for trailing '\0')
char name[CNMAX + 1]; // (Need room for trailing '\0')
int population;
float lifeExpectancy;
} country_t;
country_t *countryRecords;
int main( int argc, const char* argv[] ) {
FILE *fp;
if ((fp = fopen("AllCountries.dat", "r")) == NULL) {
printf("Error opening file\n");
exit(-1);
}
int totalCountries = 0;
char buf[MAXRECL];
char fields[MAXFIELDS][MAXFIELDL];
country_t *prev_country = NULL;
while (fgets(buf, MAXRECL, fp) != NULL) {
++totalCountries;
country_t *country = calloc(sizeof(struct Country), 1);
if (country == NULL) {
fprintf(stderr, "Out of memory\n");
exit(-1);
}
char *field = strtok(buf, ",");
int i = 0;
while(field != NULL) {
strncpy(fields[i++], field, MAXFIELDL);
field = strtok(NULL, ",");
}
strcpy(country->code, fields[COUNTRY_CODE]);
strcpy(country->name, fields[COUNTRY_NAME]);
country->population = atoi(fields[POPULATION]);
country->lifeExpectancy = atof(fields[LIFE_EXPECTANCY]);
if (countryRecords == NULL)
countryRecords = country;
else
prev_country->next = country;
prev_country = country;
}
printf("Total countries: %d\n", totalCountries);
country_t *country = countryRecords;
while(country != NULL) {
printf("%3s %30s Population: %7d Life Expectancy: %5.2f\n",
country->code, country->name, country->population, country->lifeExpectancy);
country_t *prev_country = country;
country = country->next;
free(prev_country);
}
printf("Done\n");
exit(0);
}

load information from file to table

I want to load information from the file to store in a table but I find that there's an error during loading the information and that the function void charger_Etudiant(Etudiant *E) does not display the information as they are stored in the file, someone to help me please :)
typedef struct Etudiant
{
char nom[64];
char prenom[64];
char CNI[64];
int sante;
int niveau_scolaire;
int Code_confidentiel;
int CNE;
} Etudiant;
the function is:
void charger_Etudiant(Etudiant *E)
{
int i=0;
FILE *fichier = NULL;
fichier = fopen("Info_Etudiant.txt", "r");
if (fichier != NULL)
{
while(i<2&&!feof(fichier))
{
fscanf(fichier,"%d\t\t%s %s\t\t%d\t\t%s\t\t%d\t\t%d",&E[i].Code_confidentiel,E[i].nom,E[i].prenom,&E[i].CNE,E[i].CNI,&E[i].niveau_scolaire,&E[i].sante);
printf("%d\t\t%s %s\t\t%d\t\t%s\t\t%d\t\t%d",E[i].Code_confidentiel,E[i].nom,E[i].prenom,E[i].CNE,E[i].CNI,E[i].niveau_scolaire,E[i].sante);
i++;
}
fclose(fichier);
}
}
for exemple the information in the file is written in the following form and I want to store them in a table structure :
123 BADR HARI 10043720 SJ26825 1 3
I am not a fan of scanf() or of fscanf(). I prefer to use fgets() and strtok() and sscanf() to extract the fields, although here atoi() is good enough. One advantage of strtok() is that if the field delimitors change, there is only one tweak required.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRLENG 63
typedef struct Etudiant {
char nom [STRLENG+1];
char prenom [STRLENG+1];
char CNI [STRLENG+1];
int sante;
int niveau_scolaire;
int Code_confidentiel;
int CNE;
} Etudiant;
int charger_Etudiant(Etudiant *E) {
int i=0;
char buff[512];
char *tok;
char delims[] = "\t\n";
FILE *fichier = NULL;
fichier = fopen("Info_Etudiant.txt", "r");
if (fichier != NULL) {
while(fgets (buff, 512, fichier) != NULL) {
memset(&E[i], 0, sizeof(Etudiant));
if (tok = strtok (buff, delims)) {
E[i].Code_confidentiel = atoi(tok);
if (tok = strtok (NULL, delims)) {
strncpy (E[i].nom, tok, STRLENG);
if (tok = strtok (NULL, delims)) {
strncpy (E[i].prenom, tok, STRLENG);
if (tok = strtok (NULL, delims)) {
E[i].CNE = atoi(tok);
if (tok = strtok (NULL, delims)) {
strncpy (E[i].CNI, tok, STRLENG);
if (tok = strtok (NULL, delims)) {
E[i].niveau_scolaire = atoi(tok);
if (tok = strtok (NULL, delims)) {
E[i].sante = atoi(tok);
}
}
}
}
}
}
printf("%d\t\t%s %s\t\t%d\t\t%s\t\t%d\t\t%d\n",
E[i].Code_confidentiel, E[i].nom, E[i].prenom,
E[i].CNE, E[i].CNI,E[i].niveau_scolaire,E[i].sante);
i++;
}
}
fclose(fichier);
}
return i;
}
int main() {
return 0;
}
Do not use feof() to detect EOF condition. Check the return value from input functions instead.
Like #Weather Vane suggest using fgets()
.
#define MAXINTLEN (20)
// Use 2x size line buffer
#define MAXLINELEN ((sizeof(Etudiant) + 4*MAXINTLEN) * 2)
char buffer[MAXLINELEN + 2];
while (fgets(buffer, sizeof buffer, fichier) != NULL) {
int cnt = sscanf(buffer,"%d%63s%63s%d%63s%d%d",
&E[i].Code_confidentiel, E[i].nom, E[i].prenom, &E[i].CNE,
E[i].CNI, &E[i].niveau_scolaire, &E[i].sante);
if (cnt != 7) {
break; // scan error
}
printf("%d\t\t%s %s\t\t%d\t\t%s\t\t%d\t\t%d",
E[i].Code_confidentiel, E[i].nom, E[i].prenom, E[i].CNE,
E[i].CNI, E[i].niveau_scolaire, E[i].sante);
i++;
}
When scanning the "\t\t" does not necessarily scan 2 tabs. Any white space in scanf() (except in %[]) scans any number of white spaces. Code could use sscanf(buffer,"%d %63s %63s %d %63s %d %d", ... for clarity, but it does the same thing.
Specifiers "%d" and "%s" consume leading white-space anyways.
Always limit string inputs. Example: %63s

Resources