ifstream from text file

I'm having trouble pulling text and numbers from a text file.
I need to read one line at a time.
my text file looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
MyName


100
99
84
95
85

94
100
99
100

98
100







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
#include <iostream>
#include <fstream>
#include <string>


using namespace std;
int main()
{

string file;
string names;
char array[100][12];


cout << "Please enter the name of the file: ";
cin >> file;

ifstream inFile;
inFile.open(file.c_str());



if (!inFile) {
	cout << "information  not read correctly..." << endl;
	exit(1);
}
else
	cout << "information read successfully..." << endl;
while (!inFile.eof())
{
inFile >> names;
	for (int rows = 0; rows < 5; rows++)
	{
		inFile >> array[rows][0];
	}



}
What exactly are you trying to do, and what isn't working?
to place everything in the text file into "row 0" of the array.
could i grab one character at a time in a for loop until a new line or space?
hope this will help
insted cout, simply pass the data to an array

1
2
3
4
5
6
7
	string data;
	ifstream inFile("name.txt");

	while(inFile >> data){
			cout << data << endl;

		}


Iron
Man
Hello
world



text file
Iron
Man
Hello
world
Last edited on
i need to pull in one line at a time though, because the text file consist of data that needs to go to different places.
Your program mostly works, if you take out the "for section" data is read and output. The for section restricts the output.

Maybe you are asking the wrong question. Maybe by telling us what you intend to do with it, after you read it would help.

My Test;


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
#include <iostream>
#include <fstream>
#include <string>


using namespace std;
int main()
{

string file;
string names;
char array[100][12];


cout << "Please enter the name of the file: ";
cin >> file;

ifstream inFile;
inFile.open(file.c_str());



if (!inFile) 
{
	cout << "information  not read correctly..." << endl;
	exit(1);
}
else
{
	cout << "information read successfully..." << endl;
}

while (!inFile.eof())
{
inFile >> names;
cout << names << endl;


}

}
i need to store the text file in 4 different arrays.

the name should go in the first array, then the 5 quizzes in the second array, then
4 assignments in the third, then 2 tests in the last array.
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include "functions.h"
using namespace std;
int main()
{

string file;
string inputData;

cout << "Please enter the name of the file: ";
cin >> file;

ifstream inFile;
inFile.open(file.c_str());


if (!inFile) {
	cout << "information  not read correctly..."  << endl << "Exiting Program..." << endl;
	exit(1);
}
else
{
	cout << "information read successfully..." <<endl;
}

//arrays
char students[NUM_STUDENTS][MAX_NAME_SIZE];
int quizzes[NUM_STUDENTS][NUM_QUIZZES];
int tests[NUM_STUDENTS][NUM_TESTS];
int assignments[NUM_STUDENTS][NUM_ASSIGNMENTS];

//reading text file
while (!inFile.eof())
{


	for (int rows = 0,columns = 0 ; rows < NUM_STUDENTS; rows++ )
		{
			inFile >> inputData;
			strcpy(&students[rows][columns], inputData.c_str());

			for (; columns <= NUM_QUIZZES; columns++)
			{
				inFile >> quizzes[rows][columns];
			}
			for (; columns <= NUM_ASSIGNMENTS; columns++)
			{
			inFile >> assignments[rows][columns];
			}

			for (; columns <= NUM_TESTS; columns++)
			{
			inFile >> tests[rows][columns];
			}
		}
for (int i = 0; i < 100; i++)
cout << students[0][i] ;

}
for (int x = 0; x < 100; x++)
{
cout << quizzes[0][x] ;

}



variables in caps are global variables in a header file.

Thanks for the help
Last edited on
Topic archived. No new replies allowed.