I have a file which I have already tokenized but I need to store each token in an array to later use as parameters. How would i go about doing this?
// Read in File //
FILE *fp;
char buffer[100];
fp = fopen(params, "r");
printf("Here is filename...");
printf("%s\n", params);
fseek(fp, 0, SEEK_END);
//byte_size = ftell(fp);
rewind(fp);
if (fgets(buffer,sizeof(buffer),fp) != NULL)
{
char*p, *b;
b = buffer;
printf("parsing %s", buffer);
while ((p = strsep(&b, ",")) != NULL)
{
printf("param: %s\n",p);
}
}
fclose(fp);
Using linked list and convert it to array later may be good because we don't know how many tokens are there.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strsep(char **stringp, const char *delim);
typedef struct node_tag {
char *str;
struct node_tag* next;
} list_node;
list_node* create_node(const char *str) {
list_node* n = malloc(sizeof(list_node));
if (n == NULL) exit(1);
if (str == NULL) {
n->str = NULL;
} else {
n->str = malloc(sizeof(char) * (strlen(str) + 1));
if (n->str == NULL) exit(1);
strcpy(n->str, str);
}
n->next = NULL;
return n;
}
int main(void) {
const char *params = "dummy";
FILE *fp;
char buffer[100];
list_node *head = NULL;
list_node **tail = &head;
unsigned int count = 0;
unsigned int i;
char **array;
fp = stdin;//fopen(params, "r");
printf("Here is filename...");
printf("%s\n", params);
fseek(fp, 0, SEEK_END);
//byte_size = ftell(fp);
rewind(fp);
if (fgets(buffer,sizeof(buffer),fp) != NULL)
{
char*p, *b;
b = buffer;
printf("parsing %s", buffer);
while ((p = strsep(&b, ",")) != NULL)
{
printf("param: %s\n",p);
*tail = create_node(p);
tail = &(*tail)->next;
count++;
}
}
array = malloc(sizeof(char*) * count);
if (array == NULL) return 1;
for (i = 0; i < count && head != NULL; i++) {
list_node *next = head->next;
array[i] = head->str;
// Don't free(head->str) because it is used
free(head);
head = next;
}
for (i = 0; i < count; i++) {
printf("array[%u] = %s\n", i, array[i]);
}
for (i = 0; i < count; i++) free(array[i]);
free(array);
//fclose(fp);
return 0;
}
You can simply use array if you know the number of tokens.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strsep(char **stringp, const char *delim);
#define ARRAY_SIZE 8
int main(void) {
const char *params = "dummy";
FILE *fp;
char buffer[100];
char *array[ARRAY_SIZE];
int array_count = 0;
int i;
fp = stdin;//fopen(params, "r");
printf("Here is filename...");
printf("%s\n", params);
fseek(fp, 0, SEEK_END);
//byte_size = ftell(fp);
rewind(fp);
if (fgets(buffer,sizeof(buffer),fp) != NULL)
{
char*p, *b;
b = buffer;
printf("parsing %s", buffer);
while ((p = strsep(&b, ",")) != NULL)
{
printf("param: %s\n",p);
if (array_count < ARRAY_SIZE)
{
array[array_count] = malloc(sizeof(char) * (strlen(p) + 1));
strcpy(array[array_count], p);
array_count++;
}
}
}
for (i = 0; i < array_count; i++) {
printf("array[%u] = %s\n", i, array[i]);
}
for (i = 0; i < array_count; i++) free(array[i]);
//fclose(fp);
return 0;
}
Related
I am currently developing a function in C that parses a file into a double dimensional array of characters (char **), the problem is that I get an extra line at the end, and I don't see how to fix that.
Can you help me?
Ps: My school requires me to use getline() and fopen().
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void my_free_word_array(char **word_array)
{
size_t i = 0;
if (!word_array) {
return;
}
while (word_array[i] != NULL) {
free(word_array[i]);
++i;
}
free(word_array);
}
ssize_t my_put_str_arr(char **arr, int fd)
{
char cr = '\n';
ssize_t count = 0;
size_t i = 0;
if (!arr)
return -1;
while (arr[i]) {
count += write(fd, arr[i], strlen(arr[i]));
count += write(fd, &cr, 1);
i++;
}
return count;
}
void append_word_array(char ***array, char *line)
{
size_t array_len = 0;
while ((*array)[array_len] != NULL) {
array_len++;
}
size_t len = strlen(line);
if (line[len - 1] == '\n') {
line[len - 1] = '\0';
}
(*array)[array_len] = strdup(line);
(*array) = realloc((*array), (array_len + 2) * sizeof(char *));
(*array)[array_len + 1] = NULL;
}
void fill_from_file(char ***array, FILE *file)
{
char *line_buff = NULL;
size_t line_buff_size = 0;
ssize_t line_size = getline(&line_buff, &line_buff_size, file);
while (line_size >= 0) {
append_word_array(array, line_buff);
free(line_buff);
line_buff = NULL;
line_size = getline(&line_buff, &line_buff_size, file);
}
free(line_buff);
}
char **my_load_file_to_word_array(const char *filepath)
{
char **word_array = NULL;
FILE *file = fopen(filepath, "r");
if (!file) {
return NULL;
}
word_array = malloc(sizeof(char *));
if (!word_array) {
return NULL;
}
word_array[0] = NULL;
fill_from_file(&word_array, file);
fclose(file);
return word_array;
}
int main (int argc, char **argv)
{
char **file = my_load_file_to_word_array(argv[1]);
my_put_str_arr(file, 1);
my_free_word_array(file);
return 0;
}
Here is the content of the tested file (I added the \n \0 to make it easier for you to see):
My name is Saul.\n
I am Saul Goodman.\n
Better call Saul.\0
And this is the result I get :
My name is Saul.
I am Saul Goodman.
Better call Saul.
The "problem" with your code is that the function my_put_str_arr() prints the stored lines eached followed by a single \n character. If you don't want to print the last \n you would need to test if a next line exists. You could change your loop as follows:
while (arr[i]) {
count += write(fd, arr[i], strlen(arr[i]));
i++;
if (arr[i]) {
count += write(fd, &cr, 1);
}
}
I need to make a program in C that accesses some files and retrieves a set of keywords and stores them in a list, then for each keyword it would loop through a set of emails and count how many times each keyword appeared PER email
#note i'm not allowed to use global variables
my code's kind of a mess as it is, but here it is:
#define _GNU_SOURCE
#include<stdio.h>
#include<string.h>
#include<dirent.h>
#include <stdlib.h>
#define MAXKEYWORDLENGTH 64
typedef struct {
char keyword[MAXKEYWORDLENGTH];
int keywordCount;
int stdev;
} keywordData;
typedef struct {
int emailCount;
} emailData;
int fetchKeywordNumber(const char* filename)
{
int keywordNumber;
FILE* keywords = fopen(filename, "r");
fscanf(keywords,"%d", &keywordNumber);
fclose(keywords);
return keywordNumber;
}
keywordData *fetchKeywords(const char* filename)
{
FILE* keywords = fopen(filename, "r");
keywordData *kd;
// accessing first line of the file which has the number of keywords
int keywordNumber;
keywordNumber = fetchKeywordNumber(filename);
// first number in the file is the number of keywords in the file, so i dont need to count them
if(keywords)
if(fscanf(keywords,"%d", &keywordNumber) != 1) { /* error handling*/}
// printf("%d\n", keywordNumber);
kd = malloc(keywordNumber * sizeof(*kd));
if(kd)
for(int i = 0; i < keywordNumber; i++)
{
fscanf(keywords,"%s", kd[i].keyword);
// printf("%s\n", kd[i].keyword);
}
if(keywords) fclose(keywords);
return kd;
}
int countWord(const char* filename, char wordname, int wordIndex)
{
FILE* email = fopen(filename, "r");
int keywordCount = 0;
fseek(email, 0, SEEK_END);
long emailSize = ftell(email);
fseek(email, 0, SEEK_SET); // same as rewind(f);
char *string = malloc(emailSize + 1);
fread(string, emailSize, 1, email);
fclose(email);
string[emailSize] = 0;
char *bodyPos = strstr(string, "Body:");
const char *next = bodyPos;
while ((next = strcasestr(next, wordname)) != NULL)
{
keywordCount++;
next++;
printf("Found a keyword: %s, occurence number: %d\n", kd[i].keyword, kd[i].keywordCount);
}
return keywordCount;
}
keywordData *emailLoop(char wordName, int wordIndex)
{
keywordData* kd;
kd = malloc((wordIndex + 1) * sizeof(keywordData));
struct dirent *de; // Pointer for directory entry
DIR *dr = opendir("/home/student/bitdefender-challenge/check_baliza/data/emails/");
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory" );
return 0;
}
while ((de = readdir(dr)) != NULL)
{
if(!(strcmp(de->d_name, ".")) || !(strcmp(de->d_name, "..")))
continue;
char directory[256];
sprintf(directory, "%s%s", "/home/student/bitdefender-challenge/check_baliza/data/emails/", de->d_name);
kd[wordIndex].keywordCount = countWord(directory, wordName, wordIndex);
}
closedir(dr);
return kd;
}
int main()
{
char* keywordFilename = "/home/student/bitdefender-challenge/check_baliza/data/keywords";
char* emailFilename = "/home/student/bitdefender-challenge/check_baliza/data/emails/40";
// keywordData* result = readEmail(emailFilename, fetchKeywords(keywordFilename));
int keywordCount = fetchKeywordNumber(keywordFilename);
keywordData *result = fetchKeywords(keywordFilename);
for(int i = 0; i < keywordCount; i++)
{
emailLoop(result[i].keyword, i);
}
return 0;
}
i'd like to receive some advice on how to make this count the number of occurences for each word per each email and the standard deviation for each keyword
my output would need to be like this:
<word> <total occurence number found in all emails> <standard deviation>
I have a school assignment in which we have to read a text file, sort the words by alphabetical order and write the result into a new text file.
I've already got a program that can read the file and print it on the screen and a different program to sort words which you have to type in. Now I'm trying to merge these two programs, so that the data which been read out of the file will be put into the sorting program.
The program that we use to make the code is called CodeBlocks. Below are the two programs. I hope that you can give me advice and an example how to fix this because I tried everything I know but couldn't get it working.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#define MAX_NUMBER_WORDS 100
char* ReadFile(char *filename)
{
char *buffer = NULL;
int string_size, read_size;
FILE *handler = fopen(filename, "r");
if (handler)
{
//seek the last byte of the file
fseek(handler, 0, SEEK_END);
//offset from the first to the last byte, or in other words, filesize
string_size = ftell(handler);
//go back to the start of the file
rewind(handler);
//allocate a string that can hold it all
buffer = (char*)malloc(sizeof(char) * (string_size + 1));
//read it all in one operation
read_size = fread(buffer, sizeof(char), string_size, handler);
//fread doesnt set it so put a \0 in the last position
//and buffer is now officialy a string
buffer[string_size] = '\0';
if (string_size != read_size)
{
//something went wrong, throw away the memory and set
//the buffer to NULL
free(buffer);
buffer = NULL;
}
}
return buffer;
}
int numberOfWordsInDict(char **dict)
{
int i;
for (i = 0; i < MAX_NUMBER_WORDS; i++)
{
if (dict[i] == NULL)
return i;
}
return MAX_NUMBER_WORDS;
}
void printDict(char **dict)
{
int i;
printf("Dictionary:\n");
for (i = 0; i < numberOfWordsInDict(dict); i++)
printf("- %s\n", dict[i]);
if (numberOfWordsInDict(dict) == 0)
printf("The dictionary is empty.\n");
}
void swapWords(char **dict, char *word, char *word2)
{
int i, p1 = -1, p2 = -1;
char *tmp;
for (i = 0; i < numberOfWordsInDict(dict); i++)
{
if (strcmp(dict[i], word) == 0)
p1 = i;
if (strcmp(dict[i], word2) == 0)
p2 = i;
}
if (p1 != -1 && p2 != -1)
{
tmp = dict[p1];
dict[p1] = dict[p2];
dict[p2] = tmp;
}
}
void sortDict(char **dict)
{
int swap;
int i = 0;
do
{
swap = 0;
for (i = 0; i < numberOfWordsInDict(dict) - 1; i++)
{
if (strcmp(dict[i], dict[i + 1]) > 0)
{
swapWords(dict, dict[i], dict[i + 1]);
swap = 1;
}
}
} while (swap == 1);
}
void splitSentenceToWords(char **words, char *sentence)
{
int p1 = 0, p2 = 0;
int nrwords = 0;
char *word;
while (sentence[p2] != '\0')
{
if (isspace(sentence[p2]) && p1 != p2)
{
word = (char*)malloc(sizeof(char)*(p2 - p1 + 1));
words[nrwords] = word;
strncpy(words[nrwords], &sentence[p1], p2 - p1);
words[nrwords][p2 - p1] = '\0';
nrwords++;
p1 = p2 + 1;
p2 = p1;
}
else
{
p2++;
}
}
if (p1 != p2)
{
word = (char*)malloc(sizeof(char)*(p2 - p1 + 1));
words[nrwords] = word;
strncpy(words[nrwords], &sentence[p1], p2 - p1);
words[nrwords][p2 - p1] = '\0';
nrwords++;
p1 = p2 + 1;
p2 = p1;
}
}
int main(void)
{
char sentence[1024];
char *dict[MAX_NUMBER_WORDS] = {};
char *words[MAX_NUMBER_WORDS] = {};
char *string = ReadFile("test.txt");
if (string)
{
puts(string);
free(string);
}
//printf("Type een zin in: ");
scanf("%[^\n]s", &sentence);
splitSentenceToWords(words, &sentence);
printDict(words);
printf("Words has been sorted\n");
sortDict(words);
printDict(words);
return 0;
}
You are on the right track. The problem is that after your read in your file, you are not using the input to build your word list. Instead of;
splitSentenceToWords(words, &sentence);
try:
splitSentenceToWords(words, &string);
Delete
free(string)
This will get you started. You will have to clean this up when you understand it a bit better.
When I gdb the program, it says something is wrong with strcpy, but I do not know why.
Thanks.
The program requires me to read an input file, find the course abbreviation, the course description, the number of credit hours, and the professor name.
Also, I am trying to read the credit hours from the file, which are on the same line as the course. I am trying to only get the credit hours that are on the same line as the course, but it instead prints every number as a credit hour.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 20
typedef struct courses{
char *abbr;
char *name;
int credits;
char *prof;
struct courses *next;
}courses;
int isAbbr(char *string);
int isName(char *string);
int isCredit(char *string);
int isProf(char *string);
courses *readfile(FILE *);
courses *create_course(char *abbr, char *name, int credits, char *prof);
courses *create_list(courses *, courses *);
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("Inadequate amount of arguments.\n");
return 0;
}
FILE *fp = fopen(argv[1], "r");
if (fp == NULL)
{
printf("File cannot be opened.\n");
return 0;
}
courses* head = NULL;
head = readfile(fp);
int choice = 0;
while (choice != 3)
{
printf("\nSelect your option below:\n1-Register for a Course\n2-See my total\n3-Exit\nChoice: ");
scanf("%d",&choice);
}
return 0;
}
courses *readfile(FILE *fp)
{
courses *head, *entry;
head = entry = NULL;
char *abbr = malloc(MAX);
char *namep = malloc(MAX);
namep = "hello";
char *prof = malloc(MAX);
int credit;
int credflag = 0;
int nameFlag = 0;
int profFlag = 0;
int credits = 0;
char line[MAX];
while (fgets(line, MAX - 1, fp) != NULL)
{
if (line[strlen(line) - 1] == '\n')
{
line[strlen(line) - 1] = '\0';
}
char* token = strtok(line," ,\t");
while (token != NULL)
{
if (isAbbr(token) == 1)
{
abbr = token;
credflag = 1;
}
if (isName(token) == 1)
{
credflag = 1;
}
if (isCredit(token) == 1)
{
if(credflag == 1)
{
credits = atoi(token);
credflag = 0;
}
}
if (isProf(token)== 1)
{
if(nameFlag == 1) //print names, reset name flag = 0
{
entry = create_course(abbr, namep, credits, token);
head = create_list(entry,head);
nameFlag = 0;
}
else
{
namep = malloc(sizeof(char));
strcpy(namep, token);
nameFlag = 1;
}
}
else
{
nameFlag = 0;
}
token = strtok(NULL," ,\t");
}
}
}
courses *create_course(char *abbr, char *name, int credits, char *prof)
{
courses *entry = malloc(sizeof(courses));
entry->abbr=(char*)malloc(sizeof(char)*256);
strcpy(entry->abbr, abbr);
entry->name=(char*)malloc(sizeof(char)*256);
strcpy(entry->name, name);
entry->abbr=(char*)malloc(sizeof(char)*256);
strcpy(entry->prof, prof);
entry->credits = credits;
entry->next = NULL;
return entry;
}
courses *create_list(courses *head, courses *entry)
{
if (head == NULL)
{
return entry;
}
courses* curr = head;
while (curr->next != NULL)
{
curr = curr->next;
}
curr->next = entry;
return head;
}
int isProf(char *string)
{
int length = strlen(string);
int i;
if (isupper(string[0]))
{
for (i=1; i<length; i++)
{
if (islower(string[i]))
{
continue;
}
else
{
return 0;
}
}
return 1;
}
}
int isCredit(char *string)
{
int n;
int nfields = sscanf(string, "%d", &n);
if (nfields == 1 && n > 0)
{
return 1;
}
return 0;
}
int isName(char *string)
{
return 1;
}
int isAbbr(char *string)
{
int length = strlen(string);
if (length == 8 && string[4] == '-')
{
printf(" %s\n",string);
return 1;
}
return 1;
}
Just focus on strcpy:
char *namep = malloc(MAX);
namep = "hello";
here you just lose what you malloc for namep, use strcpy or something you want.
namep = malloc(sizeof(char));
strcpy(namep, token);
here you just malloc 1 char for namep, but strcpy auto add NULL terminator, so unless token is "", you overflow namep.
And every strcpy in create_course(), you just malloc 256 and strcpy, what if size of abbr, name, prof > 255? check size or use strncpy().
Hi guys I am in the second weekend of trying to find the solution to this problem. I am new at c programming and I have been trying to read each individual line of a text file and pass each of them to their own variable, where I will be able to manipulate them(such as compare them, do calculations etc).
I have a code to read each individual lines but I am unsure how to pass each line to a variable, here is the code:
#include <stdlib.h>
#include <stdio.h>
struct line_reader {
FILE *f;
char *buf;
size_t siz;
};
void
lr_init(struct line_reader *lr, FILE *f)
{
lr->f = f;
lr->buf = NULL;
lr->siz = 0;
}
char *
next_line(struct line_reader *lr, size_t *len)
{
size_t newsiz;
int c;
char *newbuf;
*len = 0;
for (;;) {
c = fgetc(lr->f);
if (ferror(lr->f))
return NULL;
if (c == EOF) {
if (*len == 0)
return NULL;
else
return lr->buf;
} else {
if (*len == lr->siz) {
newsiz = lr->siz + 4096;
newbuf = realloc(lr->buf, newsiz);
if (newbuf == NULL)
return NULL;
lr->buf = newbuf;
lr->siz = newsiz;
}
lr->buf[(*len)++] = c;
if (c == '\n')
return lr->buf;
}
}
}
void
lr_free(struct line_reader *lr)
{
free(lr->buf);
lr->buf = NULL;
lr->siz = 0;
}
int
main()
{
struct line_reader lr;
FILE *f;
size_t len;
char *line;
f = fopen("file.txt", "r");
if (f == NULL) {
perror("foobar.txt");
exit(1);
}
lr_init(&lr, f);
while (line = next_line(&lr, &len)) {
fputs("1: ", stdout);
fwrite(line, len, 1, stdout);
}
if (!feof(f)) {
perror("next_line");
exit(1);
}
lr_free(&lr);
return 0;
}
Any help would be appreciated.
What about using an array simply as a suggestion
e.g.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** readFile(const char *filename, size_t *lineCount){
FILE *fp;
char buff[4096];
size_t lines = 0, capacity=1024;
char **line;
if(NULL==(fp=fopen(filename, "r"))){
perror("file can't open.");
return NULL;
}
if(NULL==(line=(char**)malloc(sizeof(char*)*capacity))){
perror("can't memory allocate.");
fclose(fp);
return NULL;
}
while(NULL!=fgets(buff, sizeof(buff), fp)){
line[lines++] = strdup(buff);
if(lines == capacity){
capacity += 32;
if(NULL==(line=(char**)realloc(line, sizeof(char*)*capacity))){
perror("can't memory allocate.");
fclose(fp);
return NULL;
}
}
}
*lineCount = lines;
fclose(fp);
return (char**)realloc(line, sizeof(char*)*lines);
}
void freeMem(char** p, size_t size){
size_t i;
if(p==NULL) return;
for(i=0;i<size;++i)
free(p[i]);
free(p);
}
int main(){
size_t lines;
char **line;
if(NULL!=(line=readFile("file.txt", &lines))){//lines: set line count of file
printf("%s", line[25]);// 26th line of file, zero origin
}
freeMem(line, lines);
return 0;
}
On any POSIX-compliant system just use the m scan modifier:
for ( char *line, nl; scanf("%m[^\n]%c",&line,&nl) != EOF ; free(line) ) {
if ( !line )
strcpy(line=malloc(1),""), getchar();
// ...
}
m has been in the standard for five years now.