Hey guys and gals.

I'm struggling yet again to get my file input working. I have a text file containing:

John 19 smith 15 03 1986
Billy 15 Nomate 19 07 1990

...
etc

I'm needing to get a line in at a time but also store the variables in different types.

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int main()
{
	ifstream infile;
char buffer[25];
char firstname[25];


infile.open("emp.txt");

if (infile.fail())
{
	cerr << "error " <<endl;
	exit(1);
}
char ch;
while((ch = infile.get()) !=EOF && (ch!=' '))


cout << ch;

return 0;
}

I've got this and it prints out "john" which is fair enough. But how do i get it to start doing the '19' and smith part along with the rest.

Since I'll have varibles called

char firstname[20];
char surname[20];
int age;
...etc all numbers need storing differently. (ie in each own int or char varible).

Make sence I hope so.

Thanks,

John

you are going about this the hard way. This is a c++ program -- stop using C character arrays :mad:

You don't have to read the bloody file one character at a time. fstream has operators and functions that will read entire words or numbers into your program for you. All you have to do is learn how to use them. For example

int n;
infile >> n;

That will read an integer directly into the variable.

std::string last_name, first_name, mi;
infile >> last_name >> first_name >> mi;

or you can put all the above all on one line

std::string first_name, last_name;
int age;
infile >> first_name >> age >> last_name;

and use infile.is_open() to test that the file was opened ok, infile.fail() may not work as expected.

Hello again, I've created a struct not allowed to create a class, and pop'd in there Char firstname[20];

so the struc is called record;

so i create a 'new' (dynamically) record called (p) and do:

infile >> p->firstname;

...

cout << p->firstname;

However at runtime the program crashes, anyone help us out?

disregard pervious post, i fiddled around and got it to work. However I'm struggling to figure out how to get the next line of fields?

disregard pervious post, i fiddled around and got it to work. However I'm struggling to figure out how to get the next line of fields?

Our crystal ball is out of order today. Guess you'll have to post your new code :p

Our crystal ball is out of order today. Guess you'll have to post your new code :p

ifstream infile;

	infile.open("info.txt");

	if(infile.is_open())
	{		
		infile >> temp->name;
	}

	else
	{
		cerr << "Failure in opening file"<< endl;
		exit (1);
	}	

	cout << temp->name;

}

since the file contains more than one line how'd do i go through reading 1 line at a time like this?

you should normally use a loop to read all the lines of a file. For example

while( infile >> temp->name )
    cout << temp->name << endl;

you should normally use a loop to read all the lines of a file. For example

while( infile >> temp->name )
    cout << temp->name << endl;

thats not going to work, I'm putting them into a storage and then calling a function.

while(infile >> temp->name >> temp->age)		
  {
insert(); // call the function
}

the while loop no longer calls insert... and if im wrong it wont do it for each line?

amd this causes and infinitive loop:

while(!infile.eof())
{	
cout << "********************" << endl;
infile >> temp->name >> temp->age;		
frontinsert(); // insert it into the node.
}

thats not going to work, I'm putting them into a storage and then calling a function.

while(infile >> temp->name >> temp->age)		
  {
insert(); // call the function
}

the while loop no longer calls insert... and if im wrong it wont do it for each line?

you are wrong -- that loop will read each line until end-of-file is reached (assuming each line contains name and age). Call whatever functions you want within that loop. You can pass temp to insert() if insert() needs the information.

you are wrong -- that loop will read each line until end-of-file is reached (assuming each line contains name and age). Call whatever functions you want within that loop. You can pass temp to insert() if insert() needs the information.

well it causes and infitite loop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.