Reading data from .txt file

I am supposed to do a program that reads the data from the notepad.
the inside of the notepad is something like this

2012-01-01 Rain 7 13 0.28 2012-01-02 ScatteredClouds 4 8 0.25 2012-01-03 Rain 6 12 0.28 2012-01-04 Rain 5 10 0.28 2012-01-05 Rain 7 12 0.28

This is a weather report from the year of 2012 to 2015. The part that i just post is merely 1% of the data in the notepad.
My task is to make my program be able to read the file. then user can prompt in for monthly stats, total rainfall, average min/max temp, etc..

I know how to make a code to read .txt file. But i dont know how to put it in an array. so i can read each data one by one.

Hope you guys can help me.
2012-01-01 Rain 7 13 0.28
......................... ^
..........LOWEST TEMPERATURE

2012-01-02 ScatteredClouds 4 8 0.25
...............................................^
.............................HIGHEST TEMPERATURE

2012-01-03 Rain 6 12 0.28
......................................^
..........................TOTAL RAINFALL

2012-01-04 Rain 5 10 0.28

2012-01-05 Rain 7 12 0.28

to make easier for u guys to understand
^
Last edited on
write the data to the file in the form of a class and ofile.write((char *)&obj,sizeof(obj)) and then u can use a while loop to check for the data asked by reasding from fole using ifile.read((char *)&obj,sizeof(obj)) and using strcmpi i think
Please post a the sample input inside code tags so that the formatting is preserved.

Are there any spaces in the "text" field? Are the fields separated by space or by some other character, '\t' for example? Are the records on their own lines?

Have you considered using a structure or class to hold this information?

Show your code showing what you've tried.
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

void MenuOpt()

{
cout << "Welcome to Weather Stats Generator" << endl;
cout << "Menu options:" << endl;
cout << "1. Load data file\n2. Overall stats\n3. Search by date\n4. Monthly stats\n5. Quit\n\n";

}

void UserOpt()

{
int option;
cout << "User Option: ";

}

void ClearScreen()

{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << endl;
}


int main()

{
string filename;
int option, date;
MenuOpt();
UserOpt();
cin>>option;

{
if(option==1)
cout << "Enter data file: " ;
cin >> filename;
ifstream file_in;
file_in.open( filename.c_str() );

if(!file_in.is_open() )
exit(0);

int i = 0;




file_in.close();





else if(option==2)
cout << "going to do soon";



else if(option==3)
cout << "Please enter the date like this ==> eg: 210112(21st January 2012)\nEnter Date :";
cin >> date;
/* LINE 81-86 IS FOR ENCOUNTERING CHARACTER */
while(cin.fail())
{
cin.clear();
cin.ignore(100000, '\n');
cout << "Please enter the date like this ==> eg: 210112(21st January 2012)\nEnter Date :";
cin >> date;
}




else if(option==4)
cout << "going to do soon";



else if(option==5)
cout << "going to do soon";


else
cout << "Invalid Action\n";


}
Please edit your post and add code tags.


I know how to make a code to read .txt file.

Since this program won't compile, my first suggestion is that you fix the compile errors and warnings then repost the modified code and ask specific questions about that modified code.
Topic archived. No new replies allowed.