File input.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

File input.

 
0
  #1
Dec 5th, 2005
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.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. ifstream infile;
  10. char buffer[25];
  11. char firstname[25];
  12.  
  13.  
  14. infile.open("emp.txt");
  15.  
  16. if (infile.fail())
  17. {
  18. cerr << "error " <<endl;
  19. exit(1);
  20. }
  21. char ch;
  22. while((ch = infile.get()) !=EOF && (ch!=' '))
  23.  
  24.  
  25. cout << ch;
  26.  
  27. return 0;
  28. }

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File input.

 
0
  #2
Dec 5th, 2005
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
  1. int n;
  2. infile >> n;
That will read an integer directly into the variable.
  1. std::string last_name, first_name, mi;
  2. infile >> last_name >> first_name >> mi;

or you can put all the above all on one line
  1. std::string first_name, last_name;
  2. int age;
  3. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: File input.

 
0
  #3
Dec 5th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: File input.

 
0
  #4
Dec 5th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File input.

 
0
  #5
Dec 5th, 2005
Originally Posted by Acidburn
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: File input.

 
0
  #6
Dec 5th, 2005
Originally Posted by Ancient Dragon
Our crystal ball is out of order today. Guess you'll have to post your new code :p
  1. ifstream infile;
  2.  
  3. infile.open("info.txt");
  4.  
  5. if(infile.is_open())
  6. {
  7. infile >> temp->name;
  8. }
  9.  
  10. else
  11. {
  12. cerr << "Failure in opening file"<< endl;
  13. exit (1);
  14. }
  15.  
  16. cout << temp->name;
  17.  
  18. }

since the file contains more than one line how'd do i go through reading 1 line at a time like this?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File input.

 
0
  #7
Dec 5th, 2005
you should normally use a loop to read all the lines of a file. For example
  1. while( infile >> temp->name )
  2. cout << temp->name << endl;
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: File input.

 
0
  #8
Dec 6th, 2005
Originally Posted by Ancient Dragon
you should normally use a loop to read all the lines of a file. For example
  1. while( infile >> temp->name )
  2. cout << temp->name << endl;
thats not going to work, I'm putting them into a storage and then calling a function.
  1.  
  2. while(infile >> temp->name >> temp->age)
  3. {
  4. insert(); // call the function
  5. }

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

amd this causes and infinitive loop:

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

[/php]
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File input.

 
0
  #9
Dec 6th, 2005
Originally Posted by Acidburn
thats not going to work, I'm putting them into a storage and then calling a function.
  1.  
  2. while(infile >> temp->name >> temp->age)
  3. {
  4. insert(); // call the function
  5. }

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: File input.

 
0
  #10
Dec 6th, 2005
Originally Posted by Ancient Dragon
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC