Text Files

closed account (1TpXSL3A)
Hi guys.
I need to write a program which reads from a text file the elements of 2 given matrices.

Excluding the text file business, i have written up a code that will just display the elements on the screen.
I have attempted to put it in a text file and get it to read it, which is what i'm assuming needs to happen but nothing worked.

Here's my code that just displays the elements.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  cout << "Matrix A" << endl;
                      
   for ( int i = 0; i < 3; i++ )
      for ( int j = 0; j < 3; j++ )
      {
         cout << "A[" << i+1 << "][" << j+1 << "]: ";
         cout << A[i][j]<< endl;
		
      }
	  cout<< " " << endl;
	  cout <<"Matrix B"  <<endl;

	for ( int i = 0; i < 3; i++ )
      for ( int j = 0; j < 3; j++ )
      {
         cout << "B[" << i+1 << "][" << j+1 << "]: ";
         cout << B[i][j]<< endl;
		
      }


How do i bring in text files into this properly?
Please help, I'm new to this!


Thanks!


can you show us the code you used to attempt to put in a file and read it?
closed account (1TpXSL3A)
I removed it since it wasn't working, but it just had something like
ofstream f;
f.open ("f.txt");
f << A[i][j] << B[i][j];


f.close();

around it, and then to read from it I used ifstream.

ok so ive made this sample and it seemed to work for me.
this is to read the file, assuming the file has all the needed numbers and have spaces between them, that code you posted is incorrect.
try to figure out from this if you can on how to write to a file, show me what you come up with. i can help you tonight when i comeback from work(i dont have enough time to write the rest yet), if noone else does it by then.


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
	
        // use fstream to see if the exists
	fstream file("sampledata.txt");

	if (file)
	{
		for (int i = 0; i < 3; i++)
		{

			for (int j = 0; j < 3; j++)
			{
				file >> A[i][j];

			}

		}
		for (int i = 0; i < 3; i++)
		{

			for (int j = 0; j < 3; j++)
			{
				file >> B[i][j];

			}

		}

	}
	else {
		cout << "file not found";
	}

Last edited on
closed account (1TpXSL3A)
I've been working on it and I now have this.

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
ofstream file;
ifstream fileoutput;
file.open ("matrices.txt");
file << "The matrices are: " <<endl;
file << "Matrix A" << endl;
for ( int i = 0; i < 3; i++ )
      for ( int j = 0; j < 3; j++ )
      {
         file << "A[" << i+1 << "][" << j+1 << "]: ";
         file << A[i][j]<< endl;
		
      }
	  file << " " << endl << 
		  file <<"Matrix B"  <<endl;
	for ( int i = 0; i < 3; i++ )
      for ( int j = 0; j < 3; j++ )
      {
         file << "B[" << i+1 << "][" << j+1 << "]: ";
         file << B[i][j]<< endl;
		
      }                      
   file.close();

   fileoutput.open ("matrices.txt");
   if (file)
   {
		for (int i = 0; i < 3; i++)
		{

			for (int j = 0; j < 3; j++)
			{
				fileoutput >> A[i][j];

			}

		}
		for (int i = 0; i < 3; i++)
		{

			for (int j = 0; j < 3; j++)
			{
				fileoutput >> B[i][j];

			}

		}

	}
	else {
		cout << "file not found";
	}


This code puts it into a file but doesn't print it out on the screen when i run it.
Please help!!
closed account (1TpXSL3A)
don't worry I've figured it out! thanks for all the help
Topic archived. No new replies allowed.