Read from file to an array

This is a discussion on Read from file to an array within the C++ Programming forums, part of the General Programming Boards category; Hi, i would like to know if someone can let me know how to read from a file of words ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    Read from file to an array

    Hi, i would like to know if someone can let me know how to read from a file of words such as an essay written in Word into a two dimensional array. I will then be passing that array through a sorting algorithm and then outputing the sorted array into a file. I have an idea of how to do it but im not sure how i will know the size of the array needed if i dont know how many words are in the file. Thanks for the help.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    It depends on what you know. If the file is in Word, it is a binary file, and it would take some advanced knowledge to read the text out of it. If the file was just ascii text then it would be a lot easier using a file stream from <fstream>.

    As far as putting words into an array, these easiest way would be to use a vector from <vector>. It grows automatically when you use push_back, so you don't have to worry about the size.

    So basically, you should show what you know and how you would try to do it, and we can help with any problems you are having. By the way, welcome. Make sure you read the top two announcements in this forum, and before you post your code attempts also read the stickied post at the top.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    3
    well i know how to read from a text file such as wordpad or something like that, and i know how to put them into an array as long as i know how many words are in the file. This comes from a project i had to do for school and i generated random words and output them to a file then input them into an array and sorted them using the quick sort, insertion and heap sorts. The only thing is that i set the amount of random words i wanted to output therefore i knew how many elements of an array i needed to declare. Know just for fun i want to read from any text file and store them in a 2 dimensional array and use my sorting algorithms on them. Thanks for any help you can give.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21