Need guidance

Hello, I am currently doing a project in my class.

The project has us creating a function that takes an input file and asks the user for a maxwidth. Basically we are making a program so that way the words in the file cannot go over that maxwidth and will automatically go down to the next line.

I thought I had an idea as to how to do that but I am stuck. I thought I would create a string or char array and have it input each word into the array[maxwidth] but i can't seem to get it to run properly and such.

I was wondering if I could get a few pointers as to how to do such a thing.
Put each word into the array[maxwidth] is bad idea. If memory is big enough, I prefer to read(or mmap) the whole file into process memory space, then use a pointer starting from the beginning of the memory, advance the pointer maxwidth bytes each time to get maxwidth length of a string, then adjust the pointer backward to align to a word. This is much faster than dealing with a word each time. Hopefully this can give you some hints.
It does. I don't know how to use pointers though :l
closed account (zb0S216C)
I would suggest std::vector for such a job. the std::vector expands and contracts at the clients amusement, which makes it ideal for your job. Since std::vector can expand and contract during run-time, you can extract each word from the file and expand the vector until the vector's length reaches maxwidth. Only then would you stop reading from the file.

With vectors, no manual DMA is required since it's all behind the scenes. Here's a link to std::vector: http://www.cplusplus.com/reference/stl/vector/

Wazzak
Last edited on
Topic archived. No new replies allowed.