how to store data in struct from .txt file?

hi

i have this data in .txt file

Average Ant,204932,50,5,Short course:1
Brilliant Bison,234543,80,3,Bachelor of Bounciness:2,5,3
Consistent Canary,123456,60,1,Diploma of Doggeral:3,6
Dusty the Dinosaur,000001,65,3,Master of Extinction:1,2,3,4,5,6
Iggy the Irratic,369523,50,15,Some Degree of Oddness:7,5,3,1

And i want to store this data in using struct

i tried doing this but i am getting lots of errors, can anybody help?

Here is my code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct Student{

string name[100],Program_name;
int Student_Code, Ability, Consistency, Subject_list;

};

void print_student(Student students[], int numstudents){
for( int i = 0; i < numstudents; i++){
cout << students[i].name[100]<< "\t" << students[i].Student_Code<< "\t" <<students[i].Ability << "\t" <<students[i].Consistency <<"\t"
<< students[i].Program_name<< "\t" <<students[i].Subject_list;
}
}

int main(){

Student Students[100];

ifstream fin("Student.txt");

if (!fin) exit(1);

Student tempstore;

int Numofstudent = 0;

fin >> tempstore.name;

while(!(tempstore.name== "Oddness")){

fin >> tempstore.Studentcode;

Students[Numofstudent] = tempstore;

Numofstudent++;

fin >> tempstore.name;
}

fin.close();

print_student(Students, Numofstudent);

return 0;


}



}
}

}
Last edited on
1. Please use the code tags when posting code -> http://www.cplusplus.com/articles/jEywvCM9/

2. You have an excess of closing braces.

3. You have arrays of std::string's, when you only need one. Are you confusing yourself with C-style char arrays?

This should at least compile.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>  //!! for exit()
using namespace std;

struct Student {

  string name/*!![100] not a char array! */, Program_name;
  int Student_Code, Ability, Consistency, Subject_list;

};

void print_student(Student students[], int numstudents)
{
  for (int i = 0; i < numstudents; i++) {
    cout << students[i].name/*!![100]*/
         << "\t"
         << students[i].Student_Code
         << "\t"
         << students[i].Ability
         << "\t"
         << students[i].Consistency
         << "\t"
         << students[i].Program_name
         << "\t"
         << students[i].Subject_list
         << endl;
  }
}

int main()
{
  Student Students[100];

  ifstream fin("Student.txt");
  if (!fin)
    exit(1);

  Student tempstore;
  int Numofstudent = 0;

  fin >> tempstore.name;
  while (!(tempstore.name == "Oddness")) {
    fin >> tempstore.Student_Code /*!! spelling */;
    Students[Numofstudent] = tempstore;
    Numofstudent++;
    fin >> tempstore.name;
  }

  fin.close();
  print_student(Students, Numofstudent);
  return 0;
}
Alright, thanks.

But i am getting the segmentation fault when i am running this.
Use a debugger and tell us where it's crashing; this will also help you figure out any possible issues. While doing this, keep track of which values are being put into your variables (e.g. is it reading the value "tempstore.name" correctly?)

Note that in your code, you're using the fin >> operator to extract data, but this is delimited by whitespace. If you need to delimit the data by the commas, you should use getline.
https://en.cppreference.com/w/cpp/string/basic_string/getline

Some links that might be helpful:
http://www.cplusplus.com/forum/general/17771/
https://stackoverflow.com/questions/1474790/how-to-read-write-into-from-text-file-with-comma-separated-values
https://stackoverflow.com/questions/19107439/c-getline-multiple-variable-types-using-comma-as-delimiter
Last edited on
Forget your struct.
Forget your array of struct.

Your first step should be to just parse the name from each line of input.

Otherwise, read this.
https://en.wikipedia.org/wiki/Garbage_in,_garbage_out

Eg.
Your input.
Average Ant,204932,50,5,Short course:1
Brilliant Bison,234543,80,3,Bachelor of Bounciness:2,5,3
Consistent Canary,123456,60,1,Diploma of Doggeral:3,6
Dusty the Dinosaur,000001,65,3,Master of Extinction:1,2,3,4,5,6
Iggy the Irratic,369523,50,15,Some Degree of Oddness:7,5,3,1 


Your output.
Name=Average Ant
Name=Brilliant Bison
Name=Consistent Canary
Name=Dusty the Dinosaur
Name=Iggy the Irratic


If you can't do that reliably, even trying to think about putting things in a structure or an array is a waste of time and effort. Your array may be fine, but if your while loop produces an endless string of "Iggy", you still lose.

Then you build the program one step at a time, say by next extracting the student code and printing that out along with the name.
struct seems off to me. 100 names? No list (vector, whatever) for the last group of numbers?

what salem said.

after that, the flow is

loop
getline
search line for commas
split out a substring based off commas
load a struct data piece with the substring data, converting to integer where needed.
end loop


The first 5 entries in each data line is consistent. string, 3 unsigned integers and another string. It is the entries that follows the first 5 that can pose a problem with a "one size fits all" container structure.

Easy enough to have those variable number of entries be contained in a vector.

Sadly the OP's code mixes C and C++ constructs, treating C++ strings as if they were C char arrays.
I'm going to suggest something different. Rather than reading the names from each line, write code to read a single line into a single record.
Read about getline(). Note in particular that you can specify the delimiter. So to read the name, just call getline and tell it to read up to a comma.

What are the fields in the input file? In other words, which field of input corresponds to which field of the struct?

The input lines have varying numbers of integers following the last string (the course name?) What are these numbers? I don't see anything in your struct to store a variable-sized collection of numbers.
Topic archived. No new replies allowed.