Why this code gives unreadable first line? - c

This code reads file and split it lines into array in order to compare lines elements to each other.
The problem that it gives me as the first line
)�H� 2382 2382
I think that the function char **linecontent(char *line) is the problem but I am new in C and I tried every possible solution and I have nothing.
I am very sorry for asking.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char** split(char string[],const char seps[])
{
char ** res = NULL;
char * p = strtok(string, seps);
int n_spaces = 0, i;
while (p) {
res = realloc(res, sizeof (char*) * ++n_spaces);
if (res == NULL) {
exit(-1); /* memory allocation failed */
}
res[n_spaces-1] = p;
p = strtok(NULL, seps);
}
res = realloc(res, sizeof (char*) * (n_spaces+1));
res[n_spaces] = '\0';
return res;
free(res);
}
char** readfile(char *name, int *lsize)
{
FILE *fp;
char *result;
char line[500];
char *pline = NULL;
char **lines = NULL;
int i = 0;
int l = 0;
fp = fopen(name, "r");
while (fgets(line,500, fp)) {
i++;
pline = strdup(line);
lines = (char**)realloc(lines, sizeof (char**) * (++l));
/* Add to lines */
lines[l-1] = pline;
*lsize += 1;
pline = NULL;
}
fclose(fp);
return lines;
}
char** linecontent(char *line)
{
char **linecont;
char hit[300];
strncpy(hit, line, sizeof(hit) - 1);
hit[sizeof(hit) - 1] = '\0';
linecont = split(hit, "\t");
return linecont;
}
int main()
{
char **lines = NULL;
int lsize = 0;
lines = readfile("TEMP", &lsize);
int i = 0;
while (i != lsize) {
char **linecont1;
char *thisline=lines[i];
linecont1=linecontent(thisline);
char *pname1 = linecont1[0];
char *acc1 = linecont1[1];
int start1 = atoi(linecont1[3]);
int miss1 = atoi(linecont1[4]);
printf("%s\t%s\t%d\t%d\n", pname1, acc1, start1, start1);
i++;
}
}

Related

Invalid write of Size 8 when mallocing array of strings

I am trying to write my own Shell in C. I have a problem. I wrote my own _strtok function that uses strtok but returns all the tokens as an array of strings. For testing I use the string "ls -laR" defined in the main function. I get the valgrind error "Invalid write of size 8" when trying to malloc the number of chars in the second pointer in the array of strings named "Doubl". Why is it doing this? I am allocating the proper number of pointers to strings in the doubl array. Any insight or help would be appreciated
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
char **_strtok(char *str, char *delim)
{
char **doubl;
char *s = str;
char *string;
int i = 0;
while (*s)
{
if (*s == *delim)
i++;
s++;
}
doubl = malloc(sizeof(char *) * i + 1);
i = 0;
string = strtok(str, delim);
while (1)
{
doubl[i] = malloc(sizeof(char) * strlen(string) + 1);
strcpy(doubl[i], string);
i++;
if (string == NULL)
break;
string = strtok(NULL, delim);
}
return (doubl);
}
char *get_path(char **env)
{
char **check = env;
char *path = NULL;
char pth[] = "PATH";
int i, j, stop = 0;
for (i = 0; check[i] && stop == 0; i++)
{
for (j = 0; j < 4 && stop == 0; j++)
{
if (check[i][j] != pth[j])
break;
if (check[i][j] == pth[j] && j == 3)
{
path = malloc(strlen(check[i]));
strcpy(path, check[i]);
stop = 1;
}
}
}
return (path);
}
char **cmd_to_arg(char **cmd, char **env)
{
/* FREE PATH BEFORE END */
char *path = get_path(env);
char *slash = "/";
char **args = NULL, **check = _strtok(path, ":"), **checkStart = check, **cmdStart = cmd;
int status = -1, i = 0, j;
while (*checkStart)
{
strcat(*checkStart, slash);
strcat(*checkStart, cmd[0]);
status = access(*checkStart, F_OK | X_OK);
printf("%s\n", *checkStart);
if (status == 0)
break;
checkStart++;
}
for(;*cmdStart; i++, cmdStart++)
printf("%d\n", i);
args = malloc(sizeof(char *) * i);
args[0] = malloc(strlen(*checkStart));
strcpy(args[0], *checkStart);
puts(args[0]);
for (j = 1; j < i && cmd[j] != NULL; j++)
{
//printf("%d\n", j);
args[j] = malloc(strlen(cmd[j]) * sizeof(char));
strcpy(args[j], cmd[j]);
puts(args[j]);
}
return (args);
}
int main(int ac, char **av, char **env)
{
(void)ac, (void)av, (void)env;
char line[] = "ls laR";
//size_t size = 0;
char **cmd; //**cmdStart;
//int i = 0, j = 0;
cmd = _strtok(line, " ");
cmd = cmd_to_arg(cmd, env);
return (0);
}

Getting valgrind errors with static char * [duplicate]

I have to recode an implementation of the getline() function, but using the file descriptor of the file and not a FILE *. I am only allowed to use malloc() and free(), along with 5 functions being 25 lines long at most.
I think I've done correctly the project although I am a beginner in C and my code isn't probably good.
When I run it, it works fine, but valgrind shows that I definetely lost x bytes, x depending of the file length and the READ_SIZE (macro defined in the header).
According to valgrind's --leak-check=full, I have a memory leak in the str_realloc_cat function, when I malloc dest. I tried but couldn't find where should I free / do something else?
Here below is my code:
char *get_next_line(const int fd)
{
static char *remaining = "";
char *buffer;
ssize_t cread;
size_t i;
i = 0;
if (remaining == NULL)
return (NULL);
if ((buffer = malloc(SOF(char) * READ_SIZE + 1)) == NULL ||
(cread = read(fd, buffer, READ_SIZE)) < 0)
return (NULL);
buffer[cread] = 0;
remaining = str_realloc_cat(remaining, buffer);
while (remaining[i])
{
if (remaining[i] == 10)
{
remaining[i] = 0;
buffer = str_create_cpy(remaining);
remaining = remaining + i + 1;
return (buffer);
}
i++;
}
return (check_eof(fd, buffer, remaining, cread));
}
char *str_realloc_cat(char *rem, char *buf)
{
size_t i;
size_t dest_i;
char *dest;
i = (dest_i = 0);
if ((dest = malloc(SOF(char) * (str_len(rem) + str_len(buf) + 1))) == NULL)
return (NULL);
while (rem[i])
{
dest[dest_i] = rem[i];
dest_i++;
i++;
}
i = 0;
while (buf[i])
{
dest[dest_i] = buf[i];
dest_i++;
i++;
}
dest[dest_i] = 0;
free(buf);
return (dest);
}
char *check_eof(const int fd, char *buffer, char *remaining, ssize_t cread)
{
if (cread == 0)
return (NULL);
if (cread < READ_SIZE)
{
buffer = remaining;
remaining = NULL;
return (buffer);
}
return (get_next_line(fd));
}
char *str_create_cpy(const char *src)
{
char *dest;
size_t i;
i = 0;
if ((dest = malloc(sizeof(char) * str_len(src) + 1)) == NULL)
return (NULL);
while (src[i])
{
dest[i] = src[i];
i++;
}
dest[i] = 0;
return (dest);
}
int str_len(const char *str)
{
size_t i;
i = 0;
while (str[i])
i++;
return (i);
}
And a main functon if you would like to test:
#define SOF(x) sizeof(x) // Why in the comments
int main(int ac, char **av)
{
int fd;
char *s;
UNUSED(ac);
if (!av[1])
return 1;
fd = open(av[1], O_RDONLY);
while ((s = get_next_line(fd)))
{
printf("%s\n", s);
free(s);
}
close(fd);
}
Your algorithm is bad:
You keep the buffer in a allocate memory
You don't use a structure to regroup your variable
You use magic number remaining[i] == 10
You use recursive you can stack overflow return get_next_line(fd). Never mind, I didn't read well you have a tail recursive, just be sure to have the optimization on your compile for it.
You have Spaghetti code.
etc.
You should rewrite your whole function with a better logic first use this structure:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define GNL_SIZE 4096
struct gnl_context {
char buffer[GNL_SIZE];
size_t i;
size_t read;
};
char *get_next_line_r(int fd, struct gnl_context *gnl_context);
char *get_next_line(int fd);
static char *read_buffer(struct gnl_context *gnl_context, char *str,
size_t *size) {
size_t i = gnl_context->i;
while (i < gnl_context->read && gnl_context->buffer[i] != '\n') {
i++;
}
size_t j = i - gnl_context->i;
char *ret = realloc(str, *size + j + 1);
if (ret == NULL) {
return NULL;
}
memcpy(ret + *size, gnl_context->buffer + gnl_context->i, j);
*size += j;
ret[*size] = '\0';
gnl_context->i = i;
return ret;
}
char *get_next_line_r(int fd, struct gnl_context *gnl_context) {
char *str = NULL;
size_t size = 0;
loop:
if (gnl_context->i == gnl_context->read) {
ssize_t ret = read(fd, gnl_context->buffer, GNL_SIZE);
if (ret <= 0) {
return str;
}
gnl_context->read = (size_t)ret;
gnl_context->i = 0;
}
char *tmp = read_buffer(gnl_context, str, &size);
if (tmp == NULL) {
return str;
}
if (gnl_context->i != gnl_context->read) {
gnl_context->i++;
return tmp;
}
str = tmp;
goto loop;
}
char *get_next_line(int fd) {
static struct gnl_context gnl_context;
return get_next_line_r(fd, &gnl_context);
}
int main(void) {
char *str;
while ((str = get_next_line(0)) != NULL) {
printf("%s\n", str);
free(str);
}
}
I am concerned about this line:
remaining = remaining + i + 1;
remaining is a pointer to the allocated buffer. On this line, you destroy it, which means that you cannot free() it anymore.

Fill a struct containing char** array as a member, C

...currently am practicing to fill a struct that contains an array(char array to store multiple elements). The scenario am trying to implement is as follows:
The general task is (to store student information, name as string and courses taken by the student as a list or char**)
student information is first loaded from file!(myfile.txt)
tokenize/parse student information and load to struct
The file that contains my student information is:
myfile.txt (each line contains student name and list of courses) delimited by ":"
Austin Barbra:Biology,chemistry,maths,music
Romio Chandra:Mechanics,IT,Geology,music,Astronomy
.
.
My main.c is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define path "myfile.txt"
typedef struct student_info
{
char *studt_name;
char *cources_as_string;
char **cources_as_list;
}std_info ;
std_info *myinfo; //a global var that will conatain student info
int student_count = 0,cource_count=0;
void load_file()
{
int i,yu,index=0;
char *line =NULL,* token = NULL;
size_t len=0;
FILE *fp;
fp =fopen(path,"r");
if(fp==NULL)
{
perror("FILE OPEN ERROR[IN load_file]: ");
return;
}
if (( myinfo = (struct student_info *) malloc( 2 * sizeof(myinfo) ) ) == NULL)//malloc for 2 students
puts ("malloc fail");
while (getline(&line, &len, fp) != -1 )
{
strtok(line,"\n");
char *token;
token = strtok(line,":");
myinfo[index].studt_name=(char * ) malloc(200 * sizeof(char ) );
strcpy(myinfo[index].studt_name,token);
token = strtok(NULL, ":");
myinfo[index].cources_as_string=(char * ) malloc(200 * sizeof(char ) );
strcpy(myinfo[index].cources_as_string,token);
index++;
}
student_count = index;
fclose(fp);
}
char** return_cource_list(char* cources_string)
{
char *token;
char **cource_list = malloc (sizeof (char *) * 10);
int index = 0;
//course_string is delimited by ",": (eg. Biology,chemistry,maths,music). parse this and add to my char ** variable.
token = strtok(cources_string,",");
cource_list[0]= token;
while (token != NULL)
{
cource_list[index]= token;
token = strtok (NULL, ",");
index++;
}
cource_count = index;
return cource_list;
}
int main()
{
int i,j;
load_file();
for(i=0;i<student_count;i++)
{
printf("============================\n");
printf("NAME: %s >>COURCE_string: %s\n",myinfo[i].studt_name,myinfo[i].cources_as_string);
char ip_list[200];
char** std_cource_list = return_cource_list(myinfo[i].cources_as_string);
for(j=0;j<cource_count;j++)
{
printf("\tCOURCE_list[%d]: %s\n",j,std_cource_list[j]);
//segmentation fault exists here, to copy "std_cource_list[j]" to my struct...(need help here).
strcpy(myinfo[i].cources_as_list[j],std_cource_list[j]);
}
}
}
The problem am facing is to fill the "char **cources_as_list;" member of the struct. Am getting a seg_fault from the inner for loop(iterating on j). Do i miss something in my code?
I fixed it quick so that it doesn't crash and output is correct, it can be better (there are leaks we can fix) but it actually prints your stuff now.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define path "myfile.txt"
typedef struct student_info {
char *studt_name;
char *cources_as_string;
char **cources_as_list;
} std_info;
std_info *myinfo; //a global var that will conatain student info
int student_count = 0, cource_count = 0;
void load_file() {
int i, yu, index = 0;
char *line = NULL, *token = NULL;
size_t len = 0;
FILE *fp;
fp = fopen(path, "r");
if (fp == NULL) {
perror("FILE OPEN ERROR[IN load_file]: ");
return;
}
if ((myinfo = (struct student_info *) malloc(2 * sizeof(*myinfo))) == NULL)//malloc for 2 students
puts("malloc fail");
while (getline(&line, &len, fp) != -1) {
strtok(line, "\n");
char *token;
token = strtok(line, ":");
myinfo[index].studt_name = malloc(200 * sizeof(char));
strcpy(myinfo[index].studt_name, token);
token = strtok(NULL, ":");
myinfo[index].cources_as_string = malloc(200 * sizeof(char));
strcpy(myinfo[index].cources_as_string, token);
index++;
}
student_count = index;
//fclose(fp);
}
char **return_cource_list(char *cources_string) {
char *token;
char **cource_list = malloc(sizeof(char *) * 10);
int index = 0;
//course_string is delimited by ",": (eg. Biology,chemistry,maths,music). parse this and add to my char ** variable.
token = strtok(cources_string, ",");
cource_list[0] = token;
while (token != NULL) {
cource_list[index] = strdup(token);
token = strtok(NULL, ",");
index++;
}
cource_count = index;
return cource_list;
}
/* returns an array of char*, all of which NULL */
char **alloc_argv(unsigned rows) {
char **matrix = malloc(rows * sizeof(char *));
if (!matrix) abort();
for (unsigned row = 0; row < rows; row++) {
matrix[row] = malloc(rows * sizeof(char *));
matrix[row] = "\0";
if (!matrix[row]) abort();
}
return matrix;
}
int main() {
int i, j;
load_file();
for (i = 0; i < student_count; i++) {
printf("============================\n");
printf("NAME: %s >>COURCE_string: %s\n", myinfo[i].studt_name, myinfo[i].cources_as_string);
char ip_list[200];
char **std_cource_list = return_cource_list(myinfo[i].cources_as_string);
for (j = 0; j < cource_count; j++) {
printf("\tCOURCE_list[%d]: %s\n", j, std_cource_list[j]);
//segmentation fault exists here, to copy "std_cource_list[j]" to my struct...(need help here).
myinfo[i].cources_as_list = alloc_argv(100);
myinfo[i].cources_as_list[j] = malloc(sizeof(char **));
strcpy(myinfo[i].cources_as_list[j], std_cource_list[j]);
}
}
}
Output
============================
NAME: Austin Barbra >>COURCE_string: Biology,chemistry,maths,music
COURCE_list[0]: Biology
COURCE_list[1]: chemistry
COURCE_list[2]: maths
COURCE_list[3]: music
============================
NAME: Romio Chandra >>COURCE_string: Mechanics,IT,Geology,music,Astronomy
COURCE_list[0]: Mechanics
COURCE_list[1]: IT
COURCE_list[2]: Geology
COURCE_list[3]: music
COURCE_list[4]: Astronomy
Process finished with exit code 0

Weird seg faults on consecutive calls to the same array

I tried really hard to search for a solution to this but I can't think of good enough keywords.
Currently I'm having troubles grasping the concept behind makeargv and it's usage with triple pointers (I have no idea what ***foo means, it doesn't seem to be as easy of a concept as **foo or *foo). So I made my own:
const char **makeargv(char *string, int *numargs) {
string = string + strspn(string, delims);
char *copy = malloc(strlen(string) + 1);
int i;
strcpy(copy, string);
int numtokens;
if (strtok(copy, delims) != NULL) {
for (numtokens = 1; strtok(NULL, delims) != NULL; numtokens++) {}
}
strcpy(copy, string);
const char *results[numtokens+1];
results[0] = strtok(copy, delims);
for (i = 1; i < numtokens; i++) {
results[i] = strtok(NULL, delims);
}
results[numtokens+1] = NULL;
*numargs = numtokens;
return results;
}
Here's the part at where it breaks:
void parse_file(char* filename) {
char* line = malloc(160*sizeof(char));
FILE* fp = file_open(filename);
int i = 0;
int numargs = 0;
int *pointer = &numargs;
while((line = file_getline(line, fp)) != NULL) {
if (strlen(line) == 1){
continue;
}
const char **args = makeargv(line, pointer);
printf("%s\n", args[0]);
printf("%s\n", args[1]);
/* This prints out args[0], but then args[1] causes a seg fault. Even if I replace
the args[1] with another args[0] it still causes a seg fault */
}
fclose(fp);
free(line);
}
I have a working array of strings. However when I try to print out the strings in the array, I can only print 1 of my choice and then it seg faults for any subsequent calls. lets pretend my array of strings is argv[3] = {"Yes", "no", "maybe"}, if i call argv[0], it will let me call "Yes", but any other calls (even if i call argv[0] again) do not work and cause a segfault. I can call any of the elements in the array, but once i call one the rest cease to work causing segfaults.
Help please? D: This is in C.
const char *results[numtokens+1];
This array "results" is a local variable, it is only available inside of "makeargv".
You'd better use malloc:
results = malloc(numtokens+1)
And I believe there is memory leak in your code.
You will not be able to free the memory for "char *copy"
char *copy = malloc(strlen(string) + 1);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **makeargv(char *string, int *numargs) {
static const char *delims = " \t\n";
string = string + strspn(string, delims);
char *copy = malloc(strlen(string) + 1), *p = copy;
strcpy(copy, string);
int numtokens;
for (numtokens = 0; strtok(p, delims); ++numtokens, p = NULL);
char **results = malloc(sizeof(char*)*(numtokens+1));
strcpy(copy, string);
int i;
p = copy;
for (i = 0; i < numtokens; ++i, p = NULL)
results[i] = strtok(p, delims);
results[i] = NULL;
*numargs = numtokens;
return results;
}
FILE *file_open(char *filename){
FILE *fp = fopen(filename, "r");
if(!fp){
perror("file_open");
exit(1);
}
return fp;
}
void parse_file(char* filename) {
char* line = malloc(160*sizeof(char));
FILE* fp = file_open(filename);
int i = 0, numargs = 0;
while(fgets(line, 160, fp)){
if (*line == '\n')
continue;
char **args = makeargv(line, &numargs);
for(i = 0;i<numargs;++i)
printf("%s\n", args[i]);
printf("\n");
if(args[0])
free(args[0]);
free(args);
}
fclose(fp);
free(line);
}
int main(int argc, char *argv[]){
parse_file(argv[1]);
return 0;
}

strtok_r - get indicate when there is nothing between the delimiters

i use strtok_r like:
char *the_sting = "a|b||e|f";
char *last;
char *current;
current = (char*)strtok_r(the_sting, "|", &last);
while(current != NULL)
{
printf(current);
printf("\n");
current = (char*)strtok_r(NULL, "|", &last);
}
i get:
>>a
>>b
>>e
>>f
the problem is, that i need 'blank' when there is nothing between the delimiters.
like:
>>a
>>b
>>
>>e
>>f
Compare the current current with the previous current. If the difference is more than strlen(previous_current) + 1 then one or more empty places was skipped.
Then strtok_r is not your function, furthermore, you can't use a string literal char *the_sting = "a|b||e|f"; because strtok_r modifies such string, use an array instead char the_sting[] = "a|b||e|f";
And finally, do not use printf in this way printf(current); (is dangerous), instead:
printf("%s", current);
This little function does what you want:
#include <stdio.h>
#include <string.h>
char *scan(char **pp, char c)
{
char *s, *p;
p = strchr(*pp, c);
if (p) *p++ = '\0';
s = *pp;
*pp = p;
return s;
}
int main(void)
{
char the_sting[] = "a|b||e|f"; /* I think you mean the_string here */
char *s, *p = the_sting;
while (p) {
s = scan(&p, '|');
printf("<%s>", s);
}
return 0;
}
Note that a simple char (not a string) is used as delimiter
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *my_strtok_r(char *str, const char *delims, char **store){
char *p, *wk;
if(str != NULL){
*store = str;
}
if(*store == NULL) return NULL;
//*store += strspn(*store, delims);//skip delimiter
if(**store == '\0') return NULL;
p=strpbrk(wk=*store, delims);
if(p != NULL){
*p='\0';
*store = p + 1;
} else {
*store = NULL;
}
return wk;
}
int main(void){
char the_sting[] = "a|b||e|f";
char *last;
char *current;
current = my_strtok_r(the_sting, "|", &last);
while(current != NULL)
{
printf(current);
printf("\n");
current = my_strtok_r(NULL, "|", &last);
}
return 0;
}
It's easy to roll your own, really:
#include <string.h>
typedef struct {
const unsigned char *data;
size_t len;
} buffer_t;
/* Use strpbrk() for multiple delimiters. */
buffer_t
memtok(const void *s, size_t length, const char *delim, buffer_t *save_ptr)
{
const unsigned char *stream,
*token;
size_t len = 0;
if (NULL == s) {
stream = save_ptr->data;
} else {
stream = s;
save_ptr->len = length;
}
token = stream;
/* Advance until either a token is found or the stream exhausted. */
while (save_ptr->len--) {
if (memchr(delim, *stream, strlen(delim))) {
/* Point save_ptr past the (non-existent) token. */
save_ptr->data = stream + 1;
return (buffer_t) { .data = token, .len = len };
}
++len;
++stream;
}
/* State : done. */
*save_ptr = (buffer_t) { .data = NULL, .len = 0 };
/* Stream exhausted but no delimiters terminate it. */
return (buffer_t){ .data = token, .len = len };
}
and for a short test:
int main(int argc, char **argv)
{
const char *the_sting = "a|b||e|f";
buffer_t kek = { .data = the_sting, .len = 8 },
token, state;
token = memtok(the_sting, 8, "|", &state);
while (token.data != NULL) {
char test[512];
memcpy(test, token.data, token.len);
test[token.len] = 0;
printf("%s\n", test);
token = memtok(NULL, 0, "|", &state);
}
return 0;
}
how about this:
char s[] = "1,2,,,,,,,3,4,5,6";
char *tok, *saved;
tok = strtok_r(s, ",", &saved);
do
{
fprintf(stderr, "tok = %s, saved = %s\n", tok, saved);;
if (',' == *saved)
{
while (',' == *saved++ )
{
fprintf(stderr, "saved = %s\n", saved);;
}
*saved--;
}
} while( (tok = (strtok_r(((void *)0), ",", &saved))));

Resources