.txt files

Hello,

I am working on a input .txt file. I am getting all my information to come up, but just not in rows and columns like is should. Here is my program so far. I just need the data to be understood better and more organized. Please Help. Thanks for time.


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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <string>
#include <ctime>
 

using namespace std;


int main ()
{
 
//Input File

 ifstream myReadFile;
 myReadFile.open("mayTemperatureData.txt");
 char output[100];
 if (myReadFile.is_open()) {
 while (!myReadFile.eof()) {

//Output of File
    myReadFile >> output;
    cout<<output;


 }
}
myReadFile.close();

system ("pause");


return 0;
}
Last edited on
can you provide a example of the input file and the format you would like to see it ?
closed account (3qX21hU5)
Also please use codetags when posting code to the forum (Hint: Highlight all the code your pasted to the forum and click the <> button under Format:), it makes your code much easier to read and will probably get more answers since a lot of people won't take the time to read non formatted code.
Last edited on
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
Temperature Data (F) for May
Day    High     Low   Average
1	67	32	50
2	56	37	47
3	62	33	48
4	48	33	41
5	59	34	47
6	69	38	54
7	69	43	56
8	81	43	62
9	83	55	69
10	86	53	70
11	85	58	72
12	71	47	59
13	82	39	61
14	84	44	64
15	65	47	56
16	63	45	54
17	69	45	57
18	81	54	68
19	87	55	71
20	78	58	68
21	82	55	69
22	77	53	65
23	65	51	58
24	69	50	60
25	78	46	62
26	77	49	63
27	78	52	65
28	78	53	66
29	79	48	64
30	83	48	66
31	71	55	63


This is the .txt file

To Zereo- Thanks for the input. Will do next time
To SamuelAdams- This is the .txt file. Thanks for your time.
Last edited on
This is a bunch of messy code but I had fun playing with it and maybe it will help you.

Because you have text and numbers in a file, I don't think
myReadFile >> output; is the best option for input.

There is probably a easier way to work with what you have but I didn't figure it out in the time I had to look at it.


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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <string>
#include <ctime>
#include <stdio.h>
#include <ctype.h>
using namespace std;

int main ()
{
//Input File
int i=0;
int count=0;
 ifstream myReadFile;
 myReadFile.open("mayTemperatureData.txt");

 char output[100];
	 if (myReadFile.is_open())
	 {
		 while (!myReadFile.eof())
		 {
		 
		//Output of File
		    myReadFile >> output;
			
			if (isalpha(output[i]))
			{
				if ((output[0]=='D') && (output[1]=='a') && (output[2]=='y'))
				{
				cout << endl;
				cout << output << "\t";
				}
				else if ((output[0]=='A') && (output[1]=='v') && (output[2]=='e'))
				{
				cout << output << endl;
				}
				else
				{
				cout << output << "\t";
				}
			}
			else
			{
			if (count <4)
			{
				cout << output << "\t";
				count ++;
			}
			else
			{
				cout << output << endl;
				count =1;				
			}
			}
		 }
	 }
myReadFile.close();
// system ("pause");
return 0;
}
Well, I can think of a couple of different ways to handle that data. The first is to simply read the file a whole line at a time, using getline(). And then use cout to display each line. Job done.

Another way is to read the first line using getline().
Read the second line as four separate strings. Display them as you wish.
The remaining lines, read as four separate integers each time. Display them as required.
Topic archived. No new replies allowed.