Copy data from file.txt into an array

Hi Anyone! I am having a heck of a time figuring out how to copy a data.txt file into an array. I've tried several variations and still cannot accomplish this task. I have attached a copy of the code. Can anyone provide a solution? Thank you in advance!

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct FOLDER
{
string name;
int age;
float gpa;
};

void CopyData(string fname, int x[], int n)
{
fstream f;
f.open(fname, io 5:: in);
for (int i=0; i<n; ++i)
{
f>>x[i];
}
f.close();
}
f>>a[i].name>>a[i].age>>a[i].gpa;

int main()
{
//declare variables

FOLDER a[5];
CopyData("Grades.txt", a, 5);



//terminate program
system("pause");
return 0;
code tags first ;)
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct FOLDER
{
string name;
int age;
float gpa;
};

void CopyData(string fname, int x[], int n)
{
fstream f;
f.open(fname, io 5:: in);
for (int i=0; i<n; ++i)
{
f>>x[i];
}
f.close();
}
f>>a[i].name>>a[i].age>>a[i].gpa;

int main()
{
//declare variables
FOLDER a[5];
CopyData("Grades.txt", a, 5);

//terminate program
system("pause");
return 0; 
}

Line 16 declare ifstream (for input - ofstream object for output - no need for flags this way)
Line 17 get rid of what looks like an ios flag and just have the file name.

Line 24 is global ? before even declaring what 'a[]' is ? that makes no sense....
Line 22 is closing the filestream...

In main()...do u want to pass an integer array into the function or a FOLDER struct into the function because 'a[]' is not an int array..?
Last edited on
Thanks for your help soranz, but maybe I'm way too much of a beginner because I don't know what "flags" means, nor "global"....I still appreciate your help! :-)
"flags" - they are basically settings that can make things like fstream more versatile to ur specific needs. ifstream and ofstream are classes derived from fstream to handle file input and output respectively. Using only fstream u have to set the ios::in or ios::out flag depending if u want to read from the file or write to the file. Using ifstream (for example) is the same as fstream with ios::in flag set. It is usually better (less mistake prone) to use ifstream/ofstream. That way u don't have to worry about setting and resetting those input and output flags.
http://www.cplusplus.com/reference/iostream/ios_base/flags/

"global" means any code that is not wrapped in a function like:
1
2
3
4
5
6
7
int var = 2;//this is a global variable and should not be here...
main()
{
//int var = 2; //should be here
var += 5;
cout << var; //the output will be the same either way but declaring 'int var' in main is better
}

output: 
7

global code is executed before any of the inner code. it is very rare that u should use global variables as they will often lead to unmanageable code. so, keep everything in ur functions/classes/structs.

line 24 is global and that particular line should cause compiler errors because it is using 'f' and 'a[]' which are only in scope during CopyData and main() respectively.
I think u mean for line 24 to be part of the CopyData function. but there's a problem with it because 'a[]' is not part of CopyData.
Do u see why ?
Last edited on
This makes more sense! I will correct it and see if it works! I believe line 24 is there because I changed this code around so many times. I kept trying different codes...cut and paste error I didn't catch! Thanks again!
Topic archived. No new replies allowed.