How to iterate over file contents and then store this information in an array ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to iterate over file contents and then store this information in an array ?

I can go as far as making a FILE pointer. reading the file with fgets etc. The next part is my problem, If i want to strip the first 28 chars and the middle 40 chars then how many arrays do i need ? FILE = 2109 Lines firstStrip = 2109 times x 28 chars middleStrip = 2109 time x 40 chars etc. Can i store 28 chars in firstStrip [0]......firstStrip [2109] ? or should i be going 2 dimensional firstStrip [2109 ][28] ? I would only like to keep what i clip and dispose of everything else

23rd Nov 2018, 4:16 PM
Ed Hanlon
Ed Hanlon - avatar
8 Answers
+ 4
I wouldn't use fgets, unless you are guarenteed that the line will not exceed the string. That function will overwrite memory for all of the characters of the line ignoring the size of the string. Using a two dimensional array is identical to a one dimensional array so use what is easiest for you to understand. The only difference is the expression: firstStrip[line*28+character] vs. firstStrip[line][character]. Using: char firstStrip[2109*28]; you can put the first 28 characters in firstStrip[0] to firstStrip[27] and the second in firstStrip[28] to firstStrip[55].
2nd Dec 2018, 7:16 PM
John Wells
John Wells - avatar
+ 3
using arrays is much harder in C than any other languages I've dabbled in. I over came my problem by just stripping the ip from each line and writing to a file. i guess my overall issue was accessing the arrays in a different function and the data not being present. I need to study Arrays and pointers more . Thank for your answer John Wells
2nd Dec 2018, 7:28 PM
Ed Hanlon
Ed Hanlon - avatar
+ 2
John Wells should be able to help with this....
2nd Dec 2018, 7:00 AM
āœ³AsterisKāœ³
āœ³AsterisKāœ³ - avatar
+ 2
Feel free to message me, if you are having issues.You can even tag me in a private code comment.
2nd Dec 2018, 7:51 PM
John Wells
John Wells - avatar
+ 1
Thank you John Wells , i will work on getting my code into the app for you to see , I think from then you can understand my current mindset and then help me understand betteršŸ˜‚
2nd Dec 2018, 7:56 PM
Ed Hanlon
Ed Hanlon - avatar
+ 1
thanks John Wells
2nd Dec 2018, 7:57 PM
āœ³AsterisKāœ³
āœ³AsterisKāœ³ - avatar
+ 1
Thank you for the referral *Asterisk*
2nd Dec 2018, 7:58 PM
Ed Hanlon
Ed Hanlon - avatar
0
welcome Ed Hanlon
2nd Dec 2018, 8:27 PM
āœ³AsterisKāœ³
āœ³AsterisKāœ³ - avatar