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.

Recommended Answers

All 6 Replies

Member Avatar for GreenDay2001

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

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.

Member Avatar for iamthwee

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.

>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?

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.

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.