C tokenize and analyze csv file - c

I am learning about how to use token to analyze file and put it in struct array. My code compiles fine, but it crashes while running it. Can someone please help?
Here is my code:
struct state_info {
char code[3];
unsigned long num_records;
unsigned long total_income;
unsigned long total_vehicles;
unsigned long total_population;
unsigned long total_rent;
unsigned long total_renter;
};
int main(int argc, char *argv[]) {
const int num_states = 100;
struct state_info *states[num_states];
FILE *file = fopen(argv[i], "r");
analyze_file(file, states, num_states);
}
void analyze_file(FILE *file, struct state_info *states[], int num_states) {
const int line_sz = 100;
char line[line_sz];
while (fgets(line, line_sz, file) != NULL) {
char* token = strtok(line, " \n");
while(token != NULL) {
//allocate memory
int size = atoi(token);
if(states[size] == NULL) {
states[size] = malloc(sizeof(struct state_info));
states[size]->num_records = 0;
states[size]->total_income = 0;
states[size]->total_vehicles = 0;
states[size]->total_population = 0;
states[size]->total_rent = 0;
states[size]->total_renter = 0;
}
//add value to the states
token = strtok(NULL, " \n");
strcpy(states[size]->code, token);
token = strtok(NULL, " \n");
states[size]->total_population += atoi(token);
token = strtok(NULL, " \n");
states[size]->total_income += atoi(token);
token = strtok(NULL, " \n");
states[size]->total_vehicles += atoi(token);
token = strtok(NULL, " \n");
//calculate total rent and total renter
if(atoi(token) != 0) {
states[size]->total_rent += atoi(token);
states[size]->total_renter++;
}
}
states[size]->num_records++;
token = strtok(NULL, " \n");
}
}
I think there should be something wrong with my pointer but I am not sure about it.

Statement struct state_info *states[num_states] does not initialize the pointer array with NULL-values, such that it may contain "garbage" (i.e. pointers != NULL pointing to something not being a valid object).
Hence, a comparison like if(states[size] == NULL) will probably never evaluate to true and will consequently never allocate a proper object. Subsequent access to states[size] will lead to undefined behaviour (actually the access states[size]==... already is undefined behaviour, yet I bet that the "crash" is due to the subsequent access)
Maybe there are other issues as well; yet I think that this is the most obvious thing in your code.
Fix it by initialising states, e.g. struct state_info *states[num_states] = { NULL }

Related

How to read from the file and write it in the structure? I have a little trouble with my code

I have to write this code, I mean I should read from the file name of students and their mark, and then sort students by the grow of mark. Now I just want to output only mark. I want to display grades using structures. I don't know where the problem is.
text.file
Jon 3
Alina 5
Ron 1
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdlib.h>
int main()
{
const int N = 3;
int i = 0;
struct student {
char surname[50];
int mark;
};
struct student PI1[N];
char str[50];
const char s[1] = " ";
char* token;
FILE* ptr;
token = strtok(str, s);
ptr = fopen("test.txt", "r");
if (NULL == ptr) {
printf("file can't be opened \n");
}
while (fgets(str, 50, ptr) != NULL){
token = strtok(str, s);
strcpy(PI1[i].surname, token);
token = strtok(NULL, s);
PI1[i].mark = atoi(token);
i++;
}
fclose(ptr);
printf("The marks is:\n");
printf("%d %d %d", PI1[0].mark, PI1[1].mark, PI1[2].mark);
return 0;
}
You need to prevent the program from reading from the file pointer if opening the file fails:
ptr = fopen("test.txt", "r");
if (NULL == ptr) {
perror("test.txt");
return 1; // this could be one way
}
The second argument to strok should be a null terminated string. const char s[1] = " "; only has room for one character. No null terminator (\0). Make it:
const char s[] = " "; // or const char s[2] = " "; or const char *s = " ";
Don't iterate out of bounds. You need to check so that you don't try to put data in PI1[N] etc.
while (i < N && fgets(str, sizeof str, ptr) != NULL) {
// ^^^^^^^^
Check that strok actually returns a pointer to a new token. If it doesn't, the line you've read doesn't fulfill the requirements.
while (i < N && fgets(str, sizeof str, ptr) != NULL) {
token = strtok(str, s);
if(!token) break; // token check
strcpy(PI1[i].surname, token);
token = strtok(NULL, s);
if (token) // token check
PI1[i].mark = atoi(token);
else
break;
i++;
}
You could also skip the strcpy by reading directly into your struct student since char str[50]; has the same length as surname. str should probably be larger though, but for now:
while (i < N && fgets(PI1[i].surname, sizeof PI1[i].surname, ptr) != NULL) {
token = strtok(PI1[i].surname, s);
if(!token) break;
token = strtok(NULL, s);
if (token)
PI1[i].mark = atoi(token);
else
break;
i++;
}
Only print as many marks as you successfully read
printf("The marks are:\n");
for(int idx = 0; idx < i; ++idx) {
printf("%d ", PI1[idx].mark);
}
putchar('\n');

Strtok() problems when I call a function within the loop

The program prints all the outputs from the file I expect it to if I comment out the second line however if I re-add it the tokens reach null earlier and only 2 words from the file are printed any problems I'm missing?
printf("%s\n",texttoken);
fprintf(resultptr,"%s ",filterer(redwords,lowercase(texttoken)));
The rest of the code is below.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main(){
char *filterer(char* redwords, char* word);
char *lowercase(char *word);
FILE *redptr;
FILE *textptr;
FILE *resultptr;
char *redwords = malloc(20);
char *text = malloc(255);
char *texttoken;
char *temp;
redptr = fopen("redfile.txt", "r");
textptr = fopen("textfile.txt", "r");
resultptr = fopen("result.txt", "w");
fgets(redwords,20,redptr);
redwords = lowercase(redwords);
fgets(text,255,textptr);
texttoken = strtok(text, " ");
while(texttoken != NULL){
printf("%s\n",texttoken);
fprintf(resultptr,"%s ",filterer(redwords,lowercase(texttoken)));
texttoken = strtok(NULL, " ");
}
}
char *filterer(char *redwords, char *word){
int match = 0;
char *token;
token = strtok(redwords, ",");
while(token != NULL) {
if(strcmp(word,token)==0){
match = 1;
}
token = strtok(NULL, ",");
}
if(match == 1){
int i;
int len = strlen(word);
char modified[len+1];
modified[len] = NULL;
for(i=0; i<len; i++){
modified[i] = '*';
}
return modified;
}
return word;
}
char *lowercase(char *word){
int i;
for(i=0; i<=strlen(word); i++){
if(word[i]>=65&&word[i]<=90)
word[i]=word[i]+32;
}
return word;
}
At least these problems:
Return of invalid pointer
return modified; returns a pointer to a local array. Local arrays become invalid when the function closes.
char modified[len+1];
modified[len] = NULL;
for(i=0; i<len; i++){
modified[i] = '*';
}
return modified; // Bad
Save time: Enable all warnings
Example: warning: function returns address of local variable [-Wreturn-local-addr]
Nested use of strtok()
Both this loop and filterer() call strtok(). That nested use is no good. Only one strtok() should be active at a time.
while(texttoken != NULL){
printf("%s\n",texttoken);
fprintf(resultptr,"%s ",filterer(redwords,lowercase(texttoken)));
texttoken = strtok(NULL, " ");
}
Since filterer() is only looking for a ',', look to strchr() as a replacement.

Error when reading strings from CSV (Core Dumped)

I keep getting the same error, I'm new to programming so I'm not so sure if the Syntax is correct.
Every time I run it, it returns Segmentation Fault(core dumped), I'm not even sure If I can open a file with a string (address) instead of the filename in extense.
Also the files I'm reading from are CSV but in txt format.
I'm using C99
#define BUFFER_SIZE 1024
#define TAM_PERGUNTAS 128
struct question{
char category[TAM_PERGUNTAS];
char question[TAM_PERGUNTAS];
char option1[TAM_PERGUNTAS];
char option2[TAM_PERGUNTAS];
char option3[TAM_PERGUNTAS];
char correct[TAM_PERGUNTAS];
};
struct question vec_question[BUFFER_SIZE];
void questions() {
FILE *perguntas;
int numaleat=0;
int num_questions, counter = 0, index, temp_randomizer=0;
char line[BUFFER_SIZE];
char answer[32];
char address[TAM_PERGUNTAS];
address[0] = '\0';
srand(time(NULL));
printf("Digite agora o numero de perguntas desejadas.(MAX 20) : "); //Insert Number of questions
scanf("%d", &num_questions);
printf("\n");
for (counter = 0; counter < num_questions; counter++) {
temp_randomizer = rand() % j; //j Represents the number o CATEGORIES at play and acts as a marker in the SELECTION string
sprintf(address, "%s.txt", SELECTION[temp_randomizer]);
perguntas = fopen(address, "r");
if (perguntas == NULL) {
printf("ERROR OPENING FILE!");
}
index = 0;
while (fgets(line, sizeof(line), perguntas) != NULL) {
strcpy(vec_question[index].category, strtok(line, ";"));
strcpy(vec_question[index].question, strtok(NULL, ";"));
strcpy(vec_question[index].option1, strtok(NULL, ";"));
strcpy(vec_question[index].option2, strtok(NULL, ";"));
strcpy(vec_question[index].option3, strtok(NULL, ";"));
strcpy(vec_question[index].correct, strtok(NULL, ";"));
vec_question[index].correct[strlen(vec_question[index].correct) - 1] = '\0';
index++;
}
fclose(perguntas);
index = 20;
numaleat = rand() % index;
printf("%s : %s\n%s\n%s\n%s",vec_question[numaleat].category,vec_question[numaleat].question,vec_question[numaleat].option1,vec_question[numaleat].option2,vec_question[numaleat].option3);
for (int i = 0; i < num_users; i++) {
printf("\n%s: ", &users[i][20]);
scanf("%s", &answer[32]);
if (answer == vec_question[numaleat].correct)
userspoints[i] += 1;
}
}
}
In general one should assume that functions like strtok can fail.
Sometimes it fails and returns a NULL value. A short record in your input is a likely cause.
Consider using it with a loop, and breaking out of the loop once strtok returns NULL.
I found a simple example here.
#include <string.h>
#include <stdio.h>
int main () {
char str[80] = "This is - www.tutorialspoint.com - website";
const char s[2] = "-";
char *token;
/* get the first token */
token = strtok(str, s);
/* walk through other tokens */
while( token != NULL ) {
printf( " %s\n", token );
token = strtok(NULL, s);
}
return(0);
}
Note that it does one strtok to get the first token. That might return NULL in which case the loop doesn't run. If it doesn't return NULL then it prints that token, and asks strtok for the next token. It keeps doing that until strtok returns NULL.

Why do I keep getting a "warning: comparison between pointer and integer error"?

Does anyone know why my program doesn't read from my delimited file? I thought it would print everything from my delimited file after the program ran through my printPropertyListing() method at the bottom but instead it gives me the error message warning "warning: comparison between pointer and integer". It's telling me the error is on the beginning line of my for loop in the main method. Any solutions please?
Here is what my delimited file looks like:
123 Cherry Tree Drive#330#Condo#2#1#275900#Toronto#
14 Leaside Lane#N/A#House#4#2#445500#Brampton#
2478 Waterfront Avenue#N/A#House#5#3#899900#Mississauga#
7 Lucky Lane#1206#Condo#3#2#310000#Toronto#
51 West Street#32#Townhouse#4#2#450000#Brampton#
193 Crystal Road#1519#Condo#1#1#250750#Toronto#
3914 Tangerine Terrace#N/A#House#3#1#427750#Mississauga#
10 Redding Road#N/A#House#4#2#512350#Toronto#
76 Old School Avenue#227#Townhouse#3#2#475000#Toronto#
90 Brookhaven Terrace#N/A#House#4#2#512750#Brampton#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char buildingType[10];
int numBedrooms;
int numBathrooms;
}Propertylisting;
typedef struct {
Propertylisting propertylisting;
char address[100];
char unitNum [10];
char city [50];
int listPrice;
} Listing;
void parseListings(FILE *in, Listing listing[], int arraySize);
void printPropertyListing(Listing l);
int main()
{
Listing listing[10];
FILE *fp = fopen("PropertyListings.txt", "r");
if (fp == NULL)
{
printf("Could not open file!");
exit(1);
}
else
{
parseListings(fp, listing, 10);
if (listing == 0)
{
printf("No Property data found.");
exit(1);
}
printf("\nNumber of listings in file: %d\n\n", listing);
int i;
for (i = 0; i < listing; i++)
{
printPropertyListing(listing[i]);
printf("\n");
}
fclose(fp);
}
return 0;
}
void parseListings(FILE *in, Listing listing[], int arraySize)
{
// Holds the current index of the Listing array
int n = 0;
// Set value as empty string
char line[256];
// A token pointer that the strtok() function returns
char *token;
char *delimiter = "#";
while (!feof(in)&& n > arraySize)
{
fgets(line, 256, in);
// Read the address
token = strtok(line, delimiter);
strcpy(listing[n].address, token);
// Read the unitNum
token = strtok(NULL, delimiter);
strcpy(listing[n].unitNum, token);
// Read the building typede
token = strtok(NULL, delimiter);
strcpy(listing[n].propertylisting.buildingType, token);
token = strtok(NULL, delimiter);
int numBedrooms = strtol(token, NULL, 10);
listing[n].propertylisting.numBedrooms = numBedrooms;
token = strtok(NULL, delimiter);
int numBathrooms = strtol(token, NULL, 10);
listing[n].propertylisting.numBathrooms = numBathrooms;
token = strtok(NULL, delimiter);
int listPrice = strtol(token, NULL, 10);
listing[n].listPrice = listPrice;
token = strtok(NULL, delimiter);
strcpy(listing[n].city, token);
n++;
}
return n;
}
void printPropertyListing(Listing l)
{
printf("%s %s %s\n%s %d %d %d\n\n",
l.address,
l.unitNum,
l.city,
l.propertylisting.buildingType,
l.propertylisting.numBedrooms,
l.propertylisting.numBathrooms,
l.listPrice);
}
The problem is in your for loop:
for (i = 0; i < listing; i++)
i is of type int and listing is of type Listing [10];
As pointed out by others, your listing variable is an array (which is actually a pointer to the start of a block of memory being used as an array). This means you are attempting to compare a pointer to an int, which does not make sense.
It looks like you are trying to compare to the size of the array, and since your parseListings function already returns the number of listings it has parsed, you can instead do the following:
int numListings = parseListings(fp, listing, 10);
if (numListings == 0) {
printf("No Property data found.");
exit(1);
}
printf("\nNumber of listings in file: %d\n\n", numListings);
int i;
for (i = 0; i < numListings; i++) {
printPropertyListing(listing[i]);
printf("\n");
}
You will note that I changed a few other locations where you were also using the listing variable, but I suspect you had wanted to check against the number of listings.
In addition to this, there is the points which Jens made about other bugs in your program, which would make this the perfect time to learn about the while 1: ... break; paradigm.
while (n < arraySize) {
fgets(line, 256, in);
if(feof(in)) {
break;
}
Credit to other people for pointing out all of these bugs; I have just attempted to formulate all of their responses into a single coherent guide to things you should consider for improving this code specifically and all of your code in general.
Edit: additionally, the return type of parseListing should be changed to int in order to match the fact that you return n;

Splitting C char array into words

I am trying to split a given char array into separate strings. I am doing this by putting the address of each word into an array, and then getting the string from the address to print.
So I have updated my code but now the program freezes after printing the numArgs but before "test2." I don't understand why.
----------------old code-----------------------
char* parseArgs(char* comPtr){
char *args[100] = {0};
char *token;
int i = 0;
token = strtok(comPtr, " ");
while(token != NULL){
args[i] = malloc(100);
args[i] = &token;
token = strtok(NULL, " ");
}
return *args;
}
char* args = parseArgs(comPtr);
int i = 0;
while(i < numArgs){
printf("arg%d: %s\n",i,&args[i]);
i++;
}
-----------------------end old code--------------------
------------------new code------------------------
int countArgs(char* comPtr){
char *token;
int i = 0;
token = strtok(comPtr, " ");
while(token != NULL){
i++;
token = strtok(NULL, " ");
}
return i;
}
char** parseArgs(char* comPtr){
printf("test1");
char** args = calloc(100, sizeof(char*));
char* token;
int i = 0;
while(token = strtok(comPtr, " ")){
args[i] = token;
}
printf("test2");
return args;
}
printf("ComPtr: %s\n",comPtr);
char* path = "/bin/";
//int pid = fork(); //pid always 0 so using pid = 1 to test
//printf("PID:%d",pid);
int pid = 1;
printf("PID:%d",pid);
if(pid != 0){
int numArgs = countArgs(comPtr);
printf("test1");
printf("NumArgs: %d\n",numArgs);
printf("test2");
char** args = parseArgs(comPtr);
int i = 0;
printf("test3");
while(i < numArgs){
printf("arg%d: %s\n",i,args[i]);
printf("test4");
i++;
}
}
else{
//waitpid();
}
You've lost track of where your memory is, your pointers are pointing etc.. If you want to return the list of pointers to tokens, you need something like this:
char** parseArgs(char* comPtr){
char** p_args = calloc(100, sizeof(char*);
int i = 0;
char* token;
while (token = strtok(comPtr, " "))
p_args[i] = token;
return p_args;
}
char** p_args = parseArgs(comPtr);
int i = 0;
while(i < numArgs)
{
printf("arg%d: %s\n",i,p_args[i]);
i++;
}
free(p_args);
I haven't tested it, but it should point you in the right direction. Have a careful think about how it differs from your program, and use a debugger and/or printf() statements in the code to print out addresses and see how it works (or debug it if necessary).
Declare the pointer array 'char *args[100]' as global variable. In your program your are allocating memory to the local pointer and its life is within the function. so at the end of the function your pointer variable scope ends. Here there is memory leak too.
The freeze is due to
int i = 0;
while(token = strtok(comPtr, " ")){
args[i] = token;
}
where you repeatedly - in an infinite loop - find the first token in comPtr, token becomes &comPtr[0] in each iteration (unless the string starts with spaces), and that is assigned to args[i].
After the first call, all calls to strtok that shall find further tokens in the same string - if any - should have a NULL first argument.
Also, you should probably increment i in the loop, since presumably you don't want to overwrite args[0] with each new token.

Resources