Getting data from a .txt file using strings/arrays

Thank you for giving my post a quick look, means a lot!

Basically what I'm trying to do is read specific data from a .txt file, but I've never done this to a full extent before. I have a .txt file containing a list of characters containing 30 true/false test answers and 25 student names, arranged like this:

TFTFTFTFTFFFTTFFTTFFTTTTTTFFFF
Azrin, Neil
FFTFTFTFFFFFTFTFTTFFTTTTTTFFTF
Babbage, Charles
FFFTFFFTTTFFFTTFTTTFTFTTFTFFFF
Boole, George
etc...

I'm not allowed to change the layout of the .txt file. I need to find out a way to read the student's answers, and then the student's name to be able to get printed later on in the program.

I had an idea that I'm probably going to need 2 different strings, one for the test answers and another for the student names. Or is there a way that I can have just a single string, and have it read 2 sets of information (answers and name), for example have the first set of info cut off after 25 characters are read (1 character for each answer), then it reads the 2nd set of info once a /n is encountered. Or something along those lines. I just don't know how would I set up that array / string(s)?


I had an idea that I'm probably going to need 2 different strings, one for the test answers and another for the student names


This is probably the easiest way to do it.

Or is there a way that I can have just a single string, and have it read 2 sets of information (answers and name),


You could do that... but it'd be harder because then you'd have to wrestle with 2 different kinds of data being in the same variable. It's typically much easier to have 1 variable represent 1 thing. Trying to combine them just makes the task more confusing and difficult.

Or something along those lines. I just don't know how would I set up that array / string(s)?


Did you learn about structs yet? You could create a struct that has 2 members (answers, name)... but make an array containing several instances of that struct.
Have you studied struct? Or just arrays?

Can you use a std::string? Or just char arrays?

For example, you could keep the student information in a struct like this:

1
2
3
4
5
struct student_info_t
  {
  string name;
  string answers;
  };


To actually read the file, no matter what structure(s) you use to store the data, you will need the following basic construct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream f( "filename.txt" );
while (true)
  {
  string student_answers;
  getline( f, student_answers );

  string student_name;
  getline( f, student_name );

  if (!f) break;  // This is where we stop if there is no more student information

  // do something with student_name and student_answers, like 
  // store them in arrays or in a struct in an array or something.
  }

If you had an array or vector of student_info, for example, you could store it easily:

1
2
student_info_t student_info[ 100 ];
unsigned num_students = 0;
10
11
12
  student_info[ num_students ].name = student_name;
  student_info[ num_students ].answers = student_answers;
  num_students++;

Hope this helps.
Wow, thanks for the replies! Didn't expect to get this much info this quickly. This site is great.

But no, we have not done studied struct's yet, only arrays. I'll try out the suggestions and get back to you guys!

Also, I managed to find this thread in the "Daily C++ problems" section with some example code for a very VERY similar problem. I mean it's basically the same. It's the problem dated 7/19/12 titled Arrays and Strings
http://pastebin.com/vpKAvvfp

The only difference is the number of students, the order in which the answers/names are presented, the grading system, and the fact that I have student names and not student ID's. I figured all of which can be very easily swapped around. I also have to write a function for the mean test score as well as print out a histogram but those are both easy functions as well. However it doesn't look complete, or am I missing something?
It looks about right. (I haven't tried it, though. IIRC AARGH usually got things right.)
@Duoas

I see in that program, the user doesn't use strings, but char's instead. How would I go about changing that to get that code to work properly for the format of my .txt file?
Just change char foo[ 000whatever ]; to string foo;, and instead of in.getline( foo, 000whatever ); use getline( in, foo );.
Topic archived. No new replies allowed.