C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 19th, 2003, 07:16 PM
clam61 clam61 is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 151 clam61 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 21 sec
Reputation Power: 0
Basic fstream with c++

hi guys
its been a long time since i have done c++. i need to read in data from a text file. there is one word on each line, and part of what i need to do is to read in each word and store it as a string.

i was looking on the net for getline() and such, and i did this as a small test to see if i could get fstream towork:

Code:
ifstream inStream;
inStream.open("data.txt");

char tempS[256];
string s;

inStream.getline(tempS, 256);
while (!inStream.eof())
{
     s= tempS;
     cout <<  s << endl;
}


but all it does is read the first line over and over. how do i increment the position to the next line?

thanks

Reply With Quote
  #2  
Old May 19th, 2003, 07:53 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
You don't have to increment anything because the get pointer is at the end of the line--all you have to do is read in another line.

I suspect, however, that you didn't really post all the relevant code, and that the above code is in a loop. If you open the file again, the get pointer is reset to the beginning. You only want to open the file once.

Also, you don't have to read into a char array and then assign it to a string. There's a string function called getline() that you can use to directly read into a string type:

getline(inStream, s)

Last edited by 7stud : May 19th, 2003 at 07:58 PM.

Reply With Quote
  #3  
Old May 19th, 2003, 09:32 PM
clam61 clam61 is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 151 clam61 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 21 sec
Reputation Power: 0
hey thanks for the help. you're right, that wasn;t the all the relevant code, there were some function calls to do stuff wtih b+ trees that weren't important. but the weird thing is, i did only open the file once...i dont know. anways i used the getline function you suggested and it worked like a charm. thanks!

btw, what library is that part of?

Reply With Quote
  #4  
Old May 19th, 2003, 11:02 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
"ii did only open the file once...i dont know"

Well if you only opened the file once, then you only encounter this line once:

nStream.getline(tempS, 256);

and therefore you are only reading in one line total, but you said in your first post,

"all it does is read the first line over and over"

How is that possible when your program only encounters the line once? If the lines you posted are inside a function, then everytime the function is called, the file is reopened.

"btw, what library is that part of?"

<string>

Reply With Quote
  #5  
Old May 20th, 2003, 01:08 AM
clam61 clam61 is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 151 clam61 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 21 sec
Reputation Power: 0
the function was main().

all it did was read in the first line over and over in an infinite loop.

anyways, its not imporant. thanks for ur help.

Last edited by clam61 : May 20th, 2003 at 01:15 AM.

Reply With Quote
  #6  
Old June 26th, 2003, 06:57 AM
pyranha pyranha is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 1 pyranha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
just try it like this:

ifstream inStream;
inStream.open("data.txt");

char tempS[256];
string s;

do{
inStream.getline(tempS, 256);
s= tempS;
cout << s << endl;
}while (!inStream.eof());

/////////////////////////////////

that de programm allways printed the first line is normal, because you had the cout in the loop.... but there was no increment....

so nice day and good luck

pyranha

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Basic fstream with c++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT