When I am executing printf command for level->description, the program gives me segmentation fault. I don't know why. Should I use malloc to repair it? The content (only 1 line ending with '\n') of file sokoban.dat is "chicago;addie;story begins here;-----#####-----------|-----##$.#-----------|-----#####-----------"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct {
char *name;
char *description;
char *password;
char *map;
struct level *next;
//char *solution;
} LEVEL;
LEVEL* parse_level(char *line) { //parsing from file into the structure
LEVEL level;
char level_name[50];
char level_password[50];
char level_description[100];
char level_map[200];
int i = 0;
int j = 0;
while (line[i] != ';') { //getting level name
level_name[j] = line[i];
i++;
j++;
}
level_name[j]='\0';
level.name=&level_name[0];
//strcpy(&level.name,level_name);
//printf("%s\n",level.name);
printf("%s\n",level_name);
j = 0;
i++;
while (line[i] != ';') { //getting level password
level_password[j] = line[i];
i++;
j++;
}
level_password[j]='\0';
level.password=&level_password[0];
printf("%s\n",level_password);
j = 0;
i++;
while (line[i] != ';') { //getting level description
level_description[j] = line[i];
i++;
j++;
}
level_description[j]='\0';
level.description=&level_description[0];
printf("%s\n",level_description);
j = 0;
i++;
while (line[i] != '\n') { //getting level map
level_map[j] = line[i];
i++;
j++;
}
level_map[j]='\0';
level.map=&level_map[0];
printf("%s\n",level_map);
j = 0;
level.next=NULL;
LEVEL* levelPointer=&level;
return levelPointer;
}
int main(){
FILE *fp = fopen("sokoban.dat", "r");
if( fp == NULL ){
printf("No such file\n");
return 1;
}
char line[500];
//strcpy(line,"");
char c;
int i=0;
while((c = fgetc(fp)) != '\n'){ //reading from file 1 by 1 character
line[i]=c;
i++;
}
printf("%s\n",line);
LEVEL* level;
level=parse_level(line);
//printf("%s\n",level->description); **//!!! this is where error occur**
printf("%s\n",level->map);
return 0;
}
In the function, parse_level() you take addresses of all the local variables and copy into the struct variable level and return level. All these copy of local addresses and using those objects later after their lifetime make your program illegal and causes undefined behaviour.
You should read about basics of language first and understand concepts such as pointers, arrays, returning values from a function, returning pointers etc before diving deeper.
The ones related to your problems are:
returning a local variable from function in C
Since I can't return a local variable, what's the best way to return a string from a C or C++ function?
Undefined, unspecified and implementation-defined behavior
Undefined behavior and sequence points
The Definitive C Book Guide and List
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct level {
char *name;
char *description;
char *password;
char *map;
struct level *next;
//char *solution;
} LEVEL;
#if 0
char *strdup(const char *str){
size_t len = strlen(str);
char *ret = malloc(len + 1);
if(ret){
memcpy(ret, str, len + 1);
//ret[len] = '\0';
}
return ret;
}
#endif
LEVEL* parse_level(char *line) {
//Returns the allocated memory
LEVEL *level = malloc(sizeof(LEVEL));
char *token;
token=strtok(line, ";");//It cut the string as a separator the ';'.
level->name = strdup(token);//Copy the memory allocated strings cut out.
token=strtok(NULL, ";");//Get next token
level->password = strdup(token);
token=strtok(NULL, ";");
level->description = strdup(token);
token=strtok(NULL, ";");
level->map = strdup(token);
level->next = NULL;
#if DEBUG
printf("debug print : level\n");
printf("%s\n", level->name);
printf("%s\n", level->password);
printf("%s\n", level->description);
printf("%s\n", level->map);
#endif
return level;
}
void LEVEL_free(LEVEL *p){
free(p->name);
free(p->password);
free(p->description);
free(p->map);
free(p);
}
int main(){
FILE *fp = fopen("sokoban.dat", "r");
if( fp == NULL ){
printf("No such file\n");
return 1;
}
char line[500];
char c;
int i=0;
while((c = fgetc(fp)) != '\n'){ //reading from file 1 by 1 character
line[i]=c;
i++;
}
line[i] = '\0';
printf("%s\n",line);
LEVEL* level;
level=parse_level(line);
printf("%s\n",level->description);
printf("%s\n",level->map);
LEVEL_free(level);//free for malloced memory
return 0;
}
Related
I read words from the file. When I throw them into the structure, it writes the same values.
What is Problem and How can I fix
Ide: VsCode
Compiler: mingw64-gcc-g++
File Content;
{Sam}
{Patrick}
{Philips}
My Code;
struct Sentence
{
char *word;
};
struct Sentence *words[20];
void readFile(const char *path, char *fileName)
{
int wordpointer = 0;
int len = strlen(fileName);
FILE *fp;
if ((fp = fopen((path), "r")) != NULL)
{
char ch = fgetc(fp);
while (ch != EOF)
{
if (ch == '{')
{
int counter = 0;
while (ch != EOF)
{
char word[20];
ch = fgetc(fp);
if (ch == '}')
{
//printf("%s\n",word);
struct Sentence *st = malloc(sizeof(struct Sentence));
st->word = word;
words[wordpointer] = st;
wordpointer++;
break;
}
word[counter++] = ch;
}
}
ch = fgetc(fp);
}
fclose(fp);
}
for (int i = 0; i < wordpointer; i++)
printf("%s\n", words[i]->word);
}
I can get proper output in the printf function in the comment line, but when I print the Struct, all the values as below are the last word in the file.
Output;
Philips
Philips
Philips
In this while loop
while (ch != EOF)
{
char word[20];
//...
all pointers st->word = word; points to the same local variable word
if (ch == '}')
{
//printf("%s\n",word);
struct Sentence *st = malloc(sizeof(struct Sentence));
st->word = word;
words[wordpointer] = st;
wordpointer++;
break;
}
declared like
st->word = word;
So after exiting the while loop the pointers will be invalid.
You need to allocate memory for each string and copy there entered strings. Moreover you need to append them with the terminating zero character '\0'.
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.
I am in the stage of preparing myself for exams, and the thing that I m least proud of are my skills with strings. What I need to do is remove a word from a sentence, without using <string.h> library at all.
This is what I've got so far. It keeps showing me that certain variables are not declared, such as start and end.
#include <stdio.h>
/* Side function to count the number of letters of the word we wish to remove */
int count(char *s) {
int counter = 0;
while (*s++) {
counter++;
s--;
return counter;
}
/* Function to remove a word from a sentence */
char *remove_word(const char *s1, const char *s2) {
int counter2 = 0;
/* We must remember where the string started */
const char *toReturn = s1;
/* Trigger for removing the word */
int found = 1;
/* First we need to find the word we wish to remove [Don't want to
use string.h library for anything associated with the task */
while (*s1 != '\0') {
const char *p = s1;
const char *q = s2;
if (*p == *q)
const char *start = p;
while (*p++ == *q++) {
counter2++;
if (*q != '\0' && counter2 < count(s2))
found = 0;
else {
const char *end = q;
}
}
/* Rewriting the end of a sentence to the beginning of the found word */
if (found) {
while (*start++ = *end++)
;
}
s1++;
}
return toReturn;
}
void insert(char niz[], int size) {
char character = getchar();
if (character == '\n')
character = getchar();
int i = 0;
while (i < size - 1 && character != '\n') {
array[i] = character;
i++;
character = getchar();
}
array[i] = '\0';
}
int main() {
char stringFirst[100];
char stringSecond[20];
printf("Type your text here: [NOT MORE THAN 100 CHARACTERS]\n");
insert(stringFirst, 100);
printf("\nInsert the word you wish to remove from your text.");
insert(stringSecond, 20);
printf("\nAfter removing the word, the text looks like this now: %s", stringFirst);
return 0;
}
your code is badly formed, i strongly suggest compiling with:
gcc -ansi -Wall -pedantic -Werror -D_DEBUG -g (or similar)
start with declaring your variables at the beginning of the function block, they are known only inside the block they are declared in.
your count function is buggy, missing a closing '}' (it doesn't compile)
should be something like
size_t Strlen(const char *s)
{
size_t size = 0;
for (; *s != '\n'; ++s, ++size)
{}
return size;
}
implementing memmove is much more efficient then copy char by char
I reformatted you code for small indentation problems and indeed indentation problems indicate real issues:
There is a missing } in count. It should read:
/* Side function to count the number of letters of the word we wish to remove */
int count(char *s) {
int counter = 0;
while (*s++) {
counter++;
}
return counter;
}
or better:
/* Side function to count the number of letters of the word we wish to remove */
int count(const char *s) {
const char *s0 = s;
while (*s++) {
continue;
}
return s - s0;
}
This function counts the number of bytes in the string, an almost exact clone of strlen except for the return type int instead of size_t. Note also that you do not actually use nor need this function.
Your function insert does not handle EOF gracefully and refuses an empty line. Why not read a line with fgets() and strip the newline manually:
char *input(char buf[], size_t size) {
size_t i;
if (!fgets(buf, size, stdin))
return NULL;
for (i = 0; buf[i]; i++) {
if (buf[i] == '\n') {
buf[i] = '\0';
break;
}
}
return buf;
}
In function remove_word, you should define start and end with a larger scope, typically the outer while loop's body. Furthermore s1 should have type char *, not const char *, as the phrase will be modified in place.
You should only increment p and q if the test succeeds and you should check that p and q are not both at the end of their strings.
last but not least: you do not call remove_word in the main function.
The complete code can be simplified into this:
#include <stdio.h>
/* Function to remove a word from a sentence */
char *remove_word(char *s1, const char *s2) {
if (*s2 != '\0') {
char *dst, *src, *p;
const char *q;
dst = src = s1;
while (*src != '\0') {
for (p = src, q = s2; *q != '\0' && *p == *q; p++, q++)
continue;
if (*q == '\0') {
src = p; /* the word was found, skip it */
} else {
*dst++ = *src++; /* otherwise, copy this character */
}
}
*dst = '\0'; /* put the null terminator if the string was shortened */
}
return s1;
}
char *input(char buf[], size_t size) {
size_t i;
if (!fgets(buf, size, stdin))
return NULL;
for (i = 0; buf[i]; i++) {
if (buf[i] == '\n') {
buf[i] = '\0';
break;
}
}
return buf;
}
int main() {
char stringFirst[102];
char stringSecond[22];
printf("Type your text here, up to 100 characters:\n");
if (!input(stringFirst, sizeof stringFirst))
return 1;
printf("\nInsert the word you wish to remove from your text: ");
if (!input(stringSecond, sizeof stringSecond))
return 1;
printf("\nAfter removing the word, the text looks like this now: %s\n",
remove_word(stringFirst, stringSecond));
return 0;
}
Your start and end pointers are defined within a block which makes their scope limited within that block. So, they are not visible to other parts of your code, and if you attempt to reference them outside their scope, the compiler will complain and throw an error. You should declare them at the beginning of the function block.
That said, consider the following approach to delete a word from a string:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int delete_word(char *buf,
const char *word);
int main(void)
{
const char word_to_delete[] = "boy";
fputs("Enter string: ", stdout);
char buf[256];
fgets(buf, sizeof(buf), stdin);
if (delete_word(buf, word_to_delete))
{
printf("Word %s deleted from buf: ", word_to_delete);
puts(buf);
}
else
{
printf("Word %s not found in buf: ", word_to_delete);
puts(buf);
}
system("PAUSE");
return 0;
}
int chDelimit(int ch)
{
return
(ch == '\n' || ch == '\t') ||
(ch >= ' ' && ch <= '/') ||
(ch >= ':' && ch <= '#') ||
(ch >= '[' && ch <= '`') ||
(ch >= '{' && ch <= '~') ||
(ch == '\0');
}
char *find_pattern(char *buf,
const char *pattern)
{
size_t n = 0;
while (*buf)
{
while (buf[n] && pattern[n])
{
if (buf[n] != pattern[n])
{
break;
}
n++;
}
if (!pattern[n])
{
return buf;
}
else if (!*buf)
{
return NULL;
}
n = 0;
buf++;
}
return NULL;
}
char *find_word(char *buf,
const char *word)
{
char *ptr;
size_t wlen;
wlen = strlen(word);
ptr = find_pattern(buf, word);
if (!ptr)
{
return NULL;
}
else if (ptr == buf)
{
if (chDelimit(buf[wlen]))
{
return ptr;
}
}
else
{
if (chDelimit(ptr[-1]) &&
chDelimit(ptr[wlen]))
{
return ptr;
}
}
ptr += wlen;
ptr = find_pattern(ptr, word);
while (ptr)
{
if (chDelimit(ptr[-1]) &&
chDelimit(ptr[wlen]))
{
return ptr;
}
ptr += wlen;
ptr = find_pattern(ptr, word);
}
return NULL;
}
int delete_word(char *buf,
const char *word)
{
size_t n;
size_t wlen;
char *tmp;
char *ptr;
wlen = strlen(word);
ptr = find_word(buf, word);
if (!ptr)
{
return 0;
}
else
{
n = ptr - buf;
tmp = ptr + wlen;
}
ptr = find_word(tmp, word);
while (ptr)
{
while (tmp < ptr)
{
buf[n++] = *tmp++;
}
tmp = ptr + wlen;
ptr = find_word(tmp, word);
}
strcpy(buf + n, tmp);
return 1;
}
If you have to do it manually, just loop over the indicies of your string to find the first one that matches and than you’ll have a second loop that loops for all the others that matches and resets all and jumps to the next index of the first loop if not matched something in order to continue the searching. If I recall accuretaly, all strings in C are accesible just like arrays, you’ll have to figure it out how. Don’t afraid, those principles are easy! C is an easy langugae, thiught very long to write.
In order to remove: store the first part in an array, store the second part in an array, alloc a new space for both of them and concatinate them there.
Thanks, hit the upvote button.
Vitali
EDIT: use \0 to terminate your newly created string.
EDIT: Have included only the relevant code
I have to manipulate an input string that looks like this
create /foo
create /foo/bar
write /foo/bar "text"
create /foo/bar/baz
And I've created this program (you don't need to look at all of it)
and the problem I have is with the function printAllFolders() which is called in the main() and it is defined right under the main() function. The problem must be in that function. Is it correct to pass the string in the struct path[] giving the parameter
comando->path ?
When I put that function in the main it does give me segmentation fault issue. The rest works just fine.
EDIT: just to be clear, the printAllFolders() does print all the strings in the path array, so I just need to pass the path[255] array, not the one with all two indexes.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct _command {
unsigned char command[10];
unsigned char path[255][255];
int pathLevels;
} command;
command* createCommandMul(unsigned char* str);
void printfAllFolders(unsigned char* stringhe, int lengthArray);
int main() {
command* comando = (command*) malloc(sizeof(command));
unsigned char* upPath = NULL;
unsigned char* allPath = NULL;
FILE* fp;
unsigned char* line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("/Users/mattiarighetti/Downloads/semplice.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
comando = createCommandMul(line);
upPath = upperPath(comando);
allPath = fullPath(comando);
printfAllFolders(comando->path, comando->pathLevels);
}
fclose(fp);
if (line)
free(line);
return 0;
}
void printfAllFolders(unsigned char* stringhe, int lengthArray) {
unsigned char* stringa = stringhe;
int length = lengthArray;
if (length == 0) printf("Cartella %s", stringa[length]);
for (int i = 0; i < length+1; i++) {
printf("Cartella %d %s\t", i, stringa[i]);
}
}
command* createCommandMul(unsigned char* str) {
unsigned char* c = str;
command* commandPointer = (command*) malloc(sizeof(command));
int commandIndex = 0;
int pathLevel = 0;
int pathIndex = 0;
/* Parte Comando */
while(*c != ' ' && commandIndex<10) {
commandPointer->command[commandIndex] = *c++;
commandIndex++;
}
commandPointer->command[commandIndex] = '\0';
while(*c == ' ') c++;
while(*c == '/') c++;
/* Parte Path*/
while(*c!='\0') {
if (*c == '/') {
commandPointer->path[pathLevel][pathIndex] = '\0';
pathLevel++;
pathIndex = 0;
c++;
} else {
commandPointer->path[pathLevel][pathIndex] = *c++;
pathIndex++;
}
}
commandPointer->path[pathLevel][pathIndex] = '\0';
commandPointer->pathLevels = pathLevel;
return commandPointer;
}
Perhaps there is some confusion about the difference between an array:
path[255][255]
and a pointer
unsigned char* stringhe
When passing the array -as- a pointer:
printfAllFolders(comando->path, ...
the printfAllFolders() function sees stringhe as a memory address where there happens to be an unsigned char stored. The printAllFolders() function does not know that stringhe actually points to a more complex object (array of unsigned char with [255][255] dimensions).
One fix to the question code is to change:
void printfAllFolders(unsigned char* stringhe, int lengthArray)
to the following:
void printfAllFolders(unsigned char stringhe[255][255], int lengthArray)
This passes additional information to the function needed to understand the more complex nature of stringhe.
Of course, the following line (again) removes this needed information:
unsigned char* stringa = stringhe;
Perhaps this line ^above^ should be eliminated?
Then change the line:
if (length == 0) printf("Cartella %s", stringa[length]);
to:
if (length == 0) printf("Cartella %s", stringhe[length]);
and then change line:
printf("Cartella %d %s\t", i, stringa[i]);
to:
printf("Cartella %d %s\t", i, stringhe[i]);
I'm trying to write a code that goes through a given string using a pointer to parse it.
The original code I wrote worked fine but it was... redundant so I tried making it into a function call to make it more concise. Here is what i have:
char inputArray[300];
char buffer[300];
char username[100];
char password[100];
char name[100];
int i=0;
void repeat(char *to)
{
while(*to!='=')
{
to++;
}
}
void array(char *mm,char *tt)
{
i=0;
while(*tt!='+')
{
mm[i]=*tt;
tt++;
i++;
}
}
int main()
{
printf("give me the shit in this fashion: username=?+password=?+real=?\n");
scanf("%s",inputArray);
strcpy(buffer,inputArray);
char *tok=buffer;
repeat(tok);
tok++;
array(username,tok);
repeat(tok);
tok++;
array(password,tok);
tok++;
repeat(tok);
tok++;
array(name,tok);
}
For some reason it won't give me back the pointer array tok where it left off from the previous function call. why is that? it acts as if after calling it the pointer starts back from the beginning.
Functions receive copies of their arguments. Original arguments remain unaffected.
Giving something back has a special syntax in C: the return statement. Thus
char* repeat (char *to) // <- this function gives back a char*
{
while (*to != '=')
{
to++;
}
return to; // <- giving something back
}
Call it like this:
tok = repeat(tok);
Treat array in the same fashion.
Note 1, this function will result in *undefined behaviour if the string doesn't contain '='.
Note 2, it is also possible to pass a pointer to tok as the other answer suggests, but for sake of clarity it is only recommended to use this style when you need to return more than one thing from a function.
just change your repeat to this:
void repeat(char **to) {
while (**to != '=') {
(*to)++;
}
}
and call it like this:
repeat(&tok);
and always check for errors:
if (scanf("%299s", inputArray) != 1){
printf("incorrect input\n");
return 1;
}
and your sample code (and add check for errors in array and repeat to not go out of bounds):
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
char inputArray[300];
char buffer[300];
char username[300];
char password[300];
char name[300];
int i = 0;
void repeat(char **to) {
while (**to != '=') {
(*to)++;
}
}
void array(char *mm, char *tt){
i = 0;
while (*tt != '+') {
mm[i] = *tt;
tt++;
i++;
}
}
int main() {
printf("give me the shit in this fashion: username=?+password=?+real=?\n");
if (scanf("%299s", inputArray) != 1){
printf("incorrect input\n");
return 1;
}
inputArray[299] = 0;
strcpy(buffer, inputArray);
char *tok = buffer;
repeat(&tok);
tok++;
array(username, tok);
repeat(&tok);
tok++;
array(password, tok);
tok++;
repeat(&tok);
tok++;
array(name, tok);
}
and you may use this to not go out of bounds:
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
char* read_str(char *src, char *dst){
char *p, *q;
p = src;
while (*p != 0 && *p != '=') p++;
if (*p == 0) {
*dst = 0;
return NULL; // '=' not found
}
p++;
q = p;
while (*q != 0 && *q != '+') q++;
//if (*q == 0) return NULL;// '+' not found
while (p <= q) *dst++ = *p++;
dst--;
*dst = 0;
q++;
return q;
}
#define MAX_LEN 100
int main() {
char username[MAX_LEN];
char password[MAX_LEN];
char name[MAX_LEN];
char inputArray[MAX_LEN] = "username=Alex+password=123+real=Alex";
char *p = inputArray;
p = read_str(p, username);
if (p == NULL)return 1; // error
p = read_str(p, password);
if (p == NULL)return 1; // error
read_str(p, name);
printf("username: %s \n", username);
printf("password: %s \n", password);
printf(" name: %s \n", name);
}