User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,575 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,604 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1427 | Replies: 5
Reply
Join Date: Oct 2007
Posts: 7
Reputation: blindkilla is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
blindkilla blindkilla is offline Offline
Newbie Poster

Puzzled with reading multiple lines from file

  #1  
Oct 27th, 2007
Ok i have a file of names that looks like this:
john
mary
joe
matthew


im trying to use a for loop to store each name in a variable or string so i can use a for loop again later to output each name

im pretty new to c++ and i cant figure this out

i can only figure out how to read 1 line from a file


thanks in advanced to anyone that helps
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Puzzled with reading multiple lines from file

  #2  
Oct 27th, 2007
Try using a while look for the first loop instead. A for loop implies you know how many times you need to go through the loop. A while implies you exit the loop when some criteria is satisfied, such as you reached the end-of-file (EOF).

Since you seem to know code tags, you should be aware that for really good help you need to post your code...
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Puzzled with reading multiple lines from file

  #3  
Oct 27th, 2007
Well. Regardless of the loop kind (assuming it's allowed to iterate enough times), it should read in the info:

while ( in >> word )
  std::cout<< word;

Where in is your input file stream and word is something used to store the information of the file.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: blindkilla is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
blindkilla blindkilla is offline Offline
Newbie Poster

Re: Puzzled with reading multiple lines from file

  #4  
Oct 27th, 2007
well here is the code i have

int main()
{
    char name[20];
    
    ifstream infile;
    
    infile.open("names.txt", ios::in);
    
    if (infile)
    {
    
        infile.get(name, 20);     
        
    }
    else
        cout << "Error Opening File" << endl;
    infile.close();
        
   return 0;
   
}

Im trying to put the name on each line into a separate array so after i could do something along the lines of

while (i != 0){
cout << "Name: " <<  name[i] << endl;
i++;
}
Last edited by blindkilla : Oct 27th, 2007 at 5:45 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Puzzled with reading multiple lines from file

  #5  
Oct 27th, 2007
you will need an array of strings. There's a couple ways to do it
// this array will hold up to 100 names and each name can be up to 19 characters long.
char names[100][20];

// This is a c++ array that can contain an unlimited (almost) number of names
// and each name can be unlimited (almost) length
vector<string> names;
Last edited by Ancient Dragon : Oct 27th, 2007 at 6:53 pm.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Puzzled with reading multiple lines from file

  #6  
Oct 27th, 2007
... or string name[100];

You don't seem to have a read loop defined yet.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC