-
getline question
Hi, does anyone know if you can get getline to read to ',' or the end of the line...
so for example:
Input:
1,s,3a,find
2,3b,g,go
Code:
while(fin.peek!=EOF)
{
char temp[100];
fin.getline(temp,100, ',' | '\n');
cout<<temp<<endl;
}
so you get an output of this:
1
s
3a
find
etc...
does anyone know how to do this?
Thanks
-
that is what it is doing right now...
-
It's usually better to read the whole line and then break it up at your leisure. Nobody likes to be rushed when it comes to potentially buggy input. :-)
Code:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
char a[100];
cin.getline(a, 100, '\n');
for (char *p = strtok(a, ","); p != 0; p = strtok(0, ","))
{
cout<< p <<endl;
}
system("PAUSE");
}
-
...really, its not working for me. get line is reading to the end of the line and spitting out the whole line. does anyone know why its not working?
Thanks
-
That is what that code is meant to do...
fin.getline(temp,100, ',' | '\n'); real till ' or \n is encountered and display..
-
hmm...well, if you wouldn't mind vasanth, could you take a look at this and see if you know whats wrong?
I've included my code, input and output
Thanks
-
could you tell us in detail what output u r expecting for that input...
-
oh..sorry
I'm expecting something like this;
1
back
4
1
a
bang
3
Thanks
-
Cela's soultions should work then.. amd soory.. i did not understand the output you wanted in the begining...
-
hehe, k, thx for your input tho! =o) And Thx Cela!!=o)