Write files from TXT into 2D array

Hello ALl im new just wanted to ask a quick question thanks.

so im writing a program that reads in data from a ".txt" file and im supposed to use it to do computations


so I figured i should write the values into an array(or 2D array)
so i wrote the code below:


So this works except that my file "example.txt." looks like this

5.5 2.1
2.7 4.3


but when i write it into my array and executes it appears as

5.52.12.74.3

one big value

is there a better way to write it into my array so ill be able to reference one value at a time? or a different way i should go about with this problem?
thanks.
Ive read a few issues similar to mine on forums and got helped up too this far just not as far as my problem








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

#include "stdafx.h"
#include<iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <conio.h>
#define newline '\n'
using namespace std;


float result = 0;
int main()
{
// Read Data File into a 2-D Array
ifstream inFile ("example.txt");
inFile.precision(2);
inFile.setf(ios::fixed, ios::showpoint);
  
 
  // Declare Variables to Store Array Information
  int parcel [3] [3];
float dis [3];
  int id [3];
  int A=0;
  int B=0;
  int c=0;


//  Text File
  	ifstream inStream;
  	inStream.open("example.txt");
 
  	if (inStream.fail()) {
  	cout<< "Can't open file!\n";
  	system("pause");
  	}
 
  	else {
  		while (!inStream.eof()) {
 
 // write into Array
  		for (int i = 0; i <2; i++) {
  		inStream >> dis[i];
		result =dis[i];

		cout << result;"\n";
		
	}
		
  	}
  	// Close File

  	inStream.close();
	system("pause");
		}
  }


-------------------------------


You need to write the seperating spaces newline characters too.
Thanks for the tip but i changed it and the output and problem remains the same...i need to find a way to separate the output.
i changed it and the output and problem remains the same
Changed it how?
1
2
cout << result;
\n"; 



but the whole text file is still written too one line. dont i have to increment the array, so each "space" in the array gets one?
kemnet wrote:
cout << result;
\n";
sorry but that's nonsense -> cout << result << endl
oh you're right I was wrong. thanks
Still
the out put was

5.5 
2.3
10.0
 24.2


im still trying to get it to be

5.5 2.3
10.0 24.2

if possible please
1
2
inStream >> dis[0] >> dis[1];
cout << dis[0] << " " << dis[1] << endl;
so you don't need for (int i = 0; i <2; i++) and result and stuff

does that reading really work?
Last edited on
Thanks alot
well actually how am i too reference lets say "24.2" so i can work on it separately, Also I did the computations on my value and im ready to write them back too another text file so i did

1
2
  myfile.open("fdc.txt");
myfile<< labs[0] << " " << labs[1] <<" "<<labs[2]<< " " <<kg<<endl;	


There for i did
1
2
3
if (labs[1]<2)
	cout<<labs[0] << " " << labs[1] <<" "<<labs[2]<< " " <<kg<<endl;			


i get the correct results from this but its not letting me save it to a variable so i can try to write the variable to my text file
Last edited on
what are you trying to do and how does it result in an error? show some code
OK for example im taking
5.5 2.3
10.0 24.2

from a text file,
-writing it into an array
-* by 2
-but when i write it back to my text file i only get the last line and its not letting me save the whole array to a variable
1
2
3
4
5
6
7
8
9
10
	inStream >> labs[0] >> labs[1]>>labs[2];
//cout << labs[0] << " " << labs[1] <<" "<<labs[2]<< endl;	


parcelkg=labs[1];

kg=parcelkg*2;
//cout<<kg<<endl;
if (labs[1]<2)
	rewrite=labs[0] << " " << labs[1] <<" "<<labs[2]<< " " <<kg<<endl;	 //rewrite=float 
1
2
cout << result;
\n"; 
was done incorrectly. You are missing a quote it should look like:cout << result << "\n"; or cout << result << endl;
yea ty :) got that
Topic archived. No new replies allowed.