1

I want to loop through a text file and concatenate all the strings until a blank line is hit and then do the same for the next strings and so on. The result of each string concatenation should be added to an char* array:

char* element_arr[287];

void splitPassports(FILE* file_to_read){
     char element[500];
     char str[80];
     int i = 0;
     int j = 0;

     while(j < FILELENGTH){
         if (fgets(str,80, file_to_read) && !isBlank(str)){
             strcat(element,str);
             printf("The string is: %s\n",str);
         }else if (isBlank(str)){
             element_arr[i] = element;
             memset(element, '\0', sizeof element);
             printf("%s\n",element_arr[0]);
             i++;
         }
         j++;
     }

     fclose(file_to_read);
 }

However when I try to do this I get an illegal hardware instruction error (I'm on a Mac). How can I properly add the new strings to the existing one and then append that to my array and after that set it back to zero to do the same for the few lines until a blank line is hit? I'm relatively new to C so thanks in advance :)

Phil
  • 321
  • 1
  • 17
  • 1
    `element` is one character wide (the terminator). Any non-empty concatination will invoke undefined behavior. Unrelated, [`while(!feof(file_to_read))` is wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – WhozCraig Jan 17 '21 at 13:50
  • 2
    Please take some time to refresh [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Please don't include line-numbers in he code you show. – Some programmer dude Jan 17 '21 at 13:52
  • Also note that the code will lead to more undefined behavior than just the array out of bounds problem. You also return a pointer to the local array `element`, an array whose life-time ends with the function. – Some programmer dude Jan 17 '21 at 13:53
  • Where do you get the error? In the `return`? Probably because you are writing more than 287 entries into `element_arr` or more than 80 chars into `str`. – Werner Henze Jan 17 '21 at 13:54
  • @Someprogrammerdude do i have to declare ```element_arr``` as a global variable to return it? Because I can't make it static – Phil Jan 17 '21 at 14:08
  • @Phil Not `element_arr`, but `element`. Or even better, pass it as an argument to the function, together with the size of it. – Some programmer dude Jan 17 '21 at 14:23
  • 1
    @Phil Okay you have changed the code. Then yes it's `element_arr`. ***But*** since all initialized elements in `element_arr` is initialized to point to `element` then you still have the same problem. You also never initialized the unused elements of `element_arr` so there's no way to know the number of initialized elements. I suggest you use *pointers*, *dynamic allocation*, *reallocation* and the `strdup` function to create the `element_arr` array and its elements dynamically. – Some programmer dude Jan 17 '21 at 14:48
  • @Someprogrammerdude Ok thank you for the precise answer. Since I'm new to C I haven't really looked at heap allocations etc. But looks like I have to now. I thought that there might be a workaround here but if you say no than I'll try doing it dynamically – Phil Jan 17 '21 at 15:43
  • @Someprogrammerdude But can you explain why my updated code now doesn't work? Because when I print out ```element``` it prints what it is supposed to. But when I look at the ```element_arr``` at that position it has all elements like you said. Why is that? Shouldn't it have only the updated element-value that I see when I print it? – Phil Jan 17 '21 at 16:05
  • Think about what the assignment `element_arr[i] = element` does. It does *not* create a new copy of the string in `element`, it makes all of `element_arr` point to `element`. – Some programmer dude Jan 17 '21 at 16:27
  • But if the element changes shouldn't it point to that new value that element holds @Someprogrammerdude. Or is the new element value now at a different location in memory – Phil Jan 17 '21 at 16:37

2 Answers2

2
  1. char element[] = ""; you declare the array which is 1 char long, so the strcat will always fail (unless the second string is also empty).

  2. Even if you define it the correct way, the assignment element_arr[i] = element; you will assign the array element with the same reference every time, so the array will be simple filled with references to the same object.

  3. while(!feof(file_to_read)) - read Why is “while ( !feof (file) )” always wrong?

I would recommend starting with the C book as you do not understand the basics of the language.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • But there has to be a way to do this? Two more questions: 1. when I try to make element a char* does that then fix problem no.1 you mentioned? 2. Why is it always the same reference to element can't I just "clear" the string and assign some new value to it? Which then gets added to element_arr? – Phil Jan 17 '21 at 14:05
  • 2
    @Phil the questions asked show that pointers & arrays are "black voodoo magic" for you. As I wrote - learn C from the book. Do the exercises. There is no other way. SO is not the repalcement – 0___________ Jan 17 '21 at 14:18
0

Dumb response to question 3, you could see if the problem is due to over-layering. I am also a Mac user, and my code will sometimes have errors occur due to having too many layers of functions etc. I don't know if anyone has already found the answer to question 3, however I genuinely hope this helps/helped.