954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading a text file Into an Array

Hey,

i am a complete newbie to C++ and have a task to do. i have looked all though my textbook and all over the net and have not found any examples similar. my problem is i am trying to read from a text file and store the data as an array. an example of the data is below:

test1
party1
234
321
23
4
test2
party2
4532
90
0
0

somehow i need to read them into an array something like:

[test1, party1, 234, 321, 23, 4]
[test2, party2, 4532, 90, 0, 0] and so on......

i know you are meant to post some of your own code but like i said i have no idea. any help would be appreciated, even just somewhere to start.

thankyou in advance.

slyrexy
Newbie Poster
3 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

You may use getline() . Here's tutorial on file i/o. Read and try yourself, if you encounter any problem you can post here.

vishesh
Nearly a Posting Virtuoso
1,381 posts since Oct 2006
Reputation Points: 85
Solved Threads: 42
 

you mean you want to concantinate all those words into one big comma-separated string? Not too difficult to do if you use std::string c++ class

std::string the line;

while read a word is successful
   line += string;
   line += ", ";
end of while loop


now the line will have a trailing ", " which you will have to strip back off.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

you mean you want to concantinate all those words into one big comma-separated string? Not too difficult to do if you use std::string c++ class

std::string the line;

while read a word is successful
   line += string;
   line += ", ";
end of while loop

now the line will have a trailing ", " which you will have to strip back off.

Incorrect, the commas in his example were used merely to communicate the fact that the words are separate entities in the array.

It also appears that:

test1 ... test[n]


Has a defined pattern. In other words, it might be useful to consider using a class or struct to relate the items in some manner.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

>i have looked all though my textbook and all over the
>net and have not found any examples similar.
You mean you haven't found any examples that do exactly what you want. I find it hard to believe that there are no examples that are similar, because I've written a large number of examples that do this myself. Let's start with this: Do you know how to open and read from a file?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

yes what iamthwee is saying is correct. i have used a comma to seperate each of the elements in the array. and yes there is a pattern to the data. each testn has the next 5 lines that go with it. i am very new to C++ and i am just starting to learn the processes.

slyrexy
Newbie Poster
3 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
>i have looked all though my textbook and all over the >net and have not found any examples similar. You mean you haven't found any examples that do exactly what you want. I find it hard to believe that there are no examples that are similar, because I've written a large number of examples that do this myself. Let's start with this: Do you know how to open and read from a file?


Narue like i said i have very limited knowledge of C++ and i wouldn't know if another example would do something similar to what i need it to. thats why i am asking for some direction. i do know how to open and read from a file but thats as much as i know at the moment, and it only displays the data as it appears in the file. any help you can offer would be appreciated.

slyrexy
Newbie Poster
3 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You