The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
VC++ volunteer and other compiler volunteer needed
Discuss VC++ volunteer and other compiler volunteer needed in the C Programming forum on Dev Shed. VC++ volunteer and other compiler volunteer needed C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 1st, 2003, 01:21 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

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.
|

May 1st, 2003, 09:36 AM
|
|
|
|
Use get instead of getline.
|

May 1st, 2003, 09:42 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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). |
|

May 1st, 2003, 02:21 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|