Questions about Files?

Reading a text file into a linked list

Reading a text file into a linked list

by Ahmad AlLakeas -
Number of replies: 5
Picture of GroupEn

is it better to read text file into a linked list or into 1 large string?

I solved question 3 in the lab by reading each character in the file into a doubly linked and manipulate the linked list because its easier than dealing with strings then deleting the list

is this a good approach or using strings is better? or it doesn't matter?

In reply to Ahmad AlLakeas

Re: Reading a text file into a linked list

by Ali Berro -
Picture of GroupEn
Not a bad approach but reading character by character or writing character by character is slow when you want to make a Real-Life commercial application, I'd suggest you at least create a string of size 512 bytes (sometimes you would see them calling it a buffer), then fill that string with data until its full, manipulate it, empty it and repeat. If you're talking about the tion question, you could just create a new file, then when you're reading that original file, you can parse each word one after the other and write it to the newly created file, the newly created file is the file that we want, so after that, we Re-read it and copy everything to the old file, and finally, we delete it.
Also you're thinking creatively, you're using the newly learnt tools to better help you out, that's great but never forget that sometimes we care about Optimization, Time, Memory,..., we will take them later I think as a separate course.
In reply to Ali Berro

Re: Reading a text file into a linked list

by Siba Haidar -
Yes indeed Ahmad,
Ali is right.
Nothing wrong about your solution except that you are not taking advantage of special treatments for strings as bulk and this will have effect on the efficiency.

On the other hand a one large string will never be large enough. So the compromise is a string of classic length (max length constant defined in the exercise is a fine suggestion) which plays the role of bucket
In reply to Siba Haidar

Re: Reading a text file into a linked list

by Siba Haidar -
In advanced programming course
We will set more constraints on the memory use limit and time limit
So it will be a selective criterion like in the competitions
In reply to Ali Berro

Re: Reading a text file into a linked list

by Ahmad AlLakeas -
Picture of GroupEn
the problem is dealing with strings in C is painful
In reply to Ahmad AlLakeas

Re: Reading a text file into a linked list

by Siba Haidar -
It gives pleasure because It needs pointer manipulation and good memory state knowledge