|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Use get instead of getline.
|
|
#3
|
||||
|
||||
|
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:
|
|
#4
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > VC++ volunteer and other compiler volunteer needed |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|