#1
  1. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15

    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    18
    Use get instead of getline.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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:
    basic_istream::tellg
    pos_type tellg();
    If fail() is false, the member function returns rdbuf()-> pubseekoff(0, cur, in). Otherwise, it returns streampos(-1).
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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.

IMN logo majestic logo threadwatch logo seochat tools logo