#How to add variables into an array, where each variable will get added into the next index until EOF

1 messages · Page 1 of 1 (latest)

safe valeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

left cedar
#

Honestly, the better question for this post is: Are there other ways to update a text file through user input.

remote field
#

Updating data in the middle of a raw text file is usually impossible because if you change the size of a line you usually have to move all the data after it as well

#

So reading it all in like you are doing, and then writing it all out is usually one of the simpler solutions

#

There are 2 problems with this part of your current solution (there may be more elsewhere but I havent checked)

  1. Conversion is a char array not a char* array
  2. Conversion has a fixed size limit
#

Conversion being an array of char is bad because you wanted to store whole strings. Now you could progressively fill up Conversion with all the characters but it seems like you wanted each index to point to each successive full string. A string in c is held by char* (if you want to be able to edit it that is) so you need to store char*s not chars

#

For now lets pretend it can already store strings like you want, to cover the next issue. Conversion has a size limit of 80 because you declared it like char Conversion[80]. This means it can only store a maximum of 80 things. If you know you wont have more than 80 (or you want to modify them in batches) then thats fine. If you want to read all of them in first then you need to be able to resize your array. That means dynamic allocation which means malloc/realloc (unless you want to use VLAs and I suggest you dont)

#

You could avoid this second issue altogether by reading things in in batches of at most 80. When you reach your limit you write out everything to a temporary file. When you're done you delete the old file and rename the new one to replace it

left cedar
#

Alright I've changed the array to char *. It's now able to carry the variables into the array, however it doesn't seem to be carrying it into the right indexes

#

I printed out the i value (incrementing value so that each variable goes into the next index), and it seems to be right

#

However, when i print out a specific index in the array, it doesn't output as expected

#

For example, printing out Conversion[2][50] should yield me "P001", but instead i get P004

#

Seems that the entire array only has the last line of the text file saved

#

(formatted my messages so it doesn't look like a wall of text)

remote field
#

This is not how you index an array

#

well it is, but youre using it incorrectly

#

if you change Conversion to be

const char* Conversion[50];
```then to access each individual pointer in that you just do `Conversion[N]`
#

Idk why youre doing Conversion[N][50] everywhere

#

If you turn on warnings and actually read them it should tell you this

left cedar
#

Within the while loop, it seems that each variable witihin each loop are getting stored in index 0 1 and 2, as printing out Conversion[0], [1] [2] should only give '[email protected]' '1234567' 'P001'

#

In the for loop however, printing Conversion from index 0 to <30 only gives [email protected] info.

#

Dunno why it's even doing that

remote field
#

ahhhhhh

#

right okay

#

Id didn't read your code properly

#

Youre using the same arrays to store the names every time

#

So when you read in the second email it overwrites the first email

#

A possible solution is to make Conversion a 2d array instead of an array of pointers so its stores the values and not the pointers

left cedar
#

Alright, i've decided to use a different approach to update my text file.

#

Thank you very much for your help

#

!solved