does anyone know how to read records in a file?
This is what i wrote so far.
Last edited on
I guess if I was doing a file like that:
1 2
|
#17129//Accounts//sam@
#429513895//Human Resources//danie@
| |
I would also learn how to use std::string here:
http://www.cplusplus.com/reference/string/string/
I would do something like:
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
|
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string CurrentLine;
fstream inFile;
string currentAccontNumber;
string currentDepartment;
string currentIndividual;
inFile.open("Temp.txt")
if(inFile.good())
{
while(!inFile.eof())
{
getline(inFile, CurrentLine);
if(!CurrentLine.empty()) // we don't want to work with an empty line, like one at the end of file sometimes.
{
// the next lind uses std::string::find() to find "//"
int firstSSPos = CurrentLine.find("\/\/" /*this might be wrong */);
if(fisrtSSPos != string::npos)
{
currentAccountNumber = CurrentLine.substr(0, firstSSPos-1);
int SecondSSPos = CurrentLine.find("\/\/");
if(SecondSSPos != string::npos)
{
currentDepartment = CurrentLine.substr(firstSSPos+2, SecondSSPos - (firstSSPos +2));
currentIndividual = CurrentLine.substr(SeccondSSPos+2);
}
}
cout << "Line: " << CurrentLine << endl;
cout << "Number: " << currentAccountNumb << endl;
cout << "Department: " << currentDepartment << endl;
cout << "Individual: " << currentIndividual << endl << endl;
}
} // while eof....
} // inFile.good...
else
{
cout << "We had a problem with the file couldn't open it!!" << endl;
}
return 0;
} // end of main
| |
I can do this other ways with the std::string[n] stuff. Others Might give you stringstreams.
I hope this is food for thought, the edits were after I thought about my math to get my pieces.
Last edited on
Hey Thanks a lot ... I Appreciate it very much.....this is the first time I'm using files so I'm making hell of mistakes .. :)
Last edited on