Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hi there can anybody tell me an eas

Status
Not open for further replies.

grungekid

Programmer
Jan 22, 2004
6
NL
hi there can anybody tell me an easy way of reading some data from a text file:

the data in the text file is:

John Smith
22 2 1982
02003456

i have been using getline and atoi, but i am having some problems!

cheers
devo
 
use fscanf

fsccanf(stream,"%s\n%d %d %d\n%d",...)

I REALLY hope that helps.
Will
 
That ok if your using a different header!

I am trying to get this to work:

student.txt

John Smith
22 2 1982
02003456


#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>

void main (void)
{
//declared variables
char instring[100];
char name[24];
fstream def;

def.open(&quot;student1.txt&quot;,ios::in);

// get name
def.getline(instring,100);
index = 0;
while ((instring[index] <'0') && (instring[index]>'9')) {index++;}
strcpy (name,&instring[index]);
cout << name <<endl;

this prints the name, now i need to get the rest of the info but i'm sure there's an easier way?

// get dob
char value[8];
int dob[3];
def.getline(instring,100);
for (int pos=0;pos<9;pos++)
{
value[pos]=(instring[pos]);
}
pos=0;
dob[0]=atoi(&value[pos]);
pos=3;
dob[1]=atoi(&value[pos]);
pos=5;
dob[2]=atoi(&value[pos]);
for (int mark=0;mark<3;mark++)
{
cout << dob[mark]<< &quot; &quot;;
}
cout << endl;


// get ub number
def.getline(instring,100);
int ubnum;

index = 0;
while ((instring[index] <'0')&& (instring[index]>'9')) {index++;}

ubnum=atoi(&instring[index]);

cout << ubnum << endl;

Note: i have a problem with readingthe ub number, for some reason it is missing out the zero

reads 02003456
prints 2003456

help?

 
//Hi
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
string str;
ifstream input1;
input1.open(&quot;student1.txt&quot;,ios::in);

while (getline(input1,str))
cout<< str <<endl;

input1.close();

return 0;
}
//Hope this help
 
Try (includes and validity checks omitted;):
Code:
namespace { // Locals
const char spc[] = &quot; &quot;; const char nl[] = &quot;\n&quot;; 
} // End of Locals...
/// YMD order ctor parameters!
struct Date
{
 int year, month, day;
 Date(int y = 1970, int m = 1, int d = 1)
     :year(y),month(m),day(d) {}
};

ostream& operator << (ostream& os, const Date& d)
{
 os << d.day << spc << d.month << spc << d.year;
 return os;
}
/// Make more safe class later...
struct Student
{
  string firstname;
  string lastname;
  string name() const { return firstname+&quot; &quot;+lastname; }
  Date   birthday;
  unsigned attr;
  Student(const string& fname, const string& lname,
          Date& bdate, unsigned a):
  firstname(fname), lastname(lname), birthday(bdate), 
  attr(a)
  {}
  string toString() const; // (home work;)
};

ostream& operator << (ostream& os, Student& st)
{
  os << st.firstname + spc + st.lastname + nl;
  os << st.birthday << nl;
  char fch = os.fill('0'); // leading zeroes...
  os << setw(8) << st.attr << nl;
  os.fill(fch); // don't forget to restore fill char...
  return os;
}

namespace { // Locals (continues)
class BadInput {}; // Bad Record exception
inline string& get(istream& in, string& word) 
{ 
  if (in >> word) // get next token
    return word;
  throw BadInput();
}
} // End of Locals

istream& operator >>(istream& in, Student& st)
{
  string word;

  try
  {
    get(in,st.firstname);
    get(in,st.lastname);
    get(in,word); st.birthday.day = atoi(word.c_str());
    get(in,word); st.birthday.month = atoi(word.c_str());
    get(in,word); st.birthday.year = atoi(word.c_str());
    get(in,word); st.attr = atol(word.c_str());
  }
  catch(BadInput&)
  {
  //in.setf(ios_base::failbit); // add later...
  }
  return in;
}
Now:
Code:
ifstream fin(&quot;student1.txt&quot;);
Student st;
int n; // counter...
for (n = 0; fin >> st; ++n)
{
  //...Process record...
}
if (fin.fail()) // Bad record occured...
  ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top