C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 1st, 2003, 01:21 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
VC++ volunteer and other compiler volunteer needed

Hi,

I need some people with VC++ and some people with other compilers to run the following program to see if it works. Here is the input data(the number of 5's and 6's after each name is unimportant):

david555555555555555
sally666666666666666666

The program is supposed to pick out the first 5 char's from each line and output them, so the output should be:

david
sally

I have VC++6.0 and the code won't work. It outputs:

david
<blank line>

If anyone can run this and tell me the results and their compiler, or tell me why it won't work for me, I would appreciate it. You need to substitute the path of your input file for the one in the program

Code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{ 
	char text[6];
	char word[6];
	
	//substitute your file's path in the following line.
	ifstream inFile("C:\\TestData\\input.txt");
	
	inFile.getline(text, 6);

	cout<<inFile.tellg()<<endl;
	//displays where the get pointer in the input
	//file is pointing. I get tellg()=-1 which doesn't make sense
	
	inFile.ignore(1000, '\n');
	
	inFile.getline(word, 6);
	
	cout<<text<<endl
		<<word<<endl;

	return 0;
}

Last edited by 7stud : May 1st, 2003 at 06:00 AM.

Reply With Quote
  #2  
Old May 1st, 2003, 09:36 AM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 272 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 Days 17 h 57 m 24 sec
Reputation Power: 17
Use get instead of getline.

Reply With Quote
  #3  
Old May 1st, 2003, 09:42 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 38 m
Reputation Power: 1949
I get the same output.

From my interpretation of the help file entry on ifstream::tellg(), the -1 means that tellg() failed. Interestingly, when I compiled the program with g++, tellg() returned a 7, which is what you were expecting. Apparently the MS implementation expects some kind of initialization. Hard to tell, since the help file entry is the sparsest I've seen in VC++6:
Quote:
basic_istream::tellg
pos_type tellg();
If fail() is false, the member function returns rdbuf()-> pubseekoff(0, cur, in). Otherwise, it returns streampos(-1).

Reply With Quote
  #4  
Old May 1st, 2003, 02:21 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
Thanks for the responses.

"Interestingly, when I compiled the program with g++, tellg() returned a 7, which is what you were expecting"

Actually, tellg() should return 5. getline(var, n) reads in n-1 chars, which in this case is 5 chars, and they are at index positions 0-4, so the get pointer should be at position 5. Maybe you used different input?

"Use get instead of getline."

Thanks. get() works. I don't "get()" why getline() won't. The way I understand it, the only difference betwee the two functions is that getline() removes the delimiter from the stream, but in my example, the read never gets to the delimiter, so there should be no difference between get() and getline(). I know there's a bug with getline() documented on the MS website, is this just a case of MS completely screwing up the getline() function?

Last edited by 7stud : May 1st, 2003 at 02:30 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > VC++ volunteer and other compiler volunteer needed

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap