Reading from input file

N

Newbee

Hello everyone.. this is my first post on this forum..

Well i was wondering if any of you could suggest a way for me to read
form an input file that has mixed ASCII integers and characters and
operators.. say a page full of random text contaning numbers,
alphabets and comas and other operators like ( * &^ % $ $ #@ .

I have to read each of the string form the file and then sort it.. now
the problem that i am facing is that when i use the standar

ifstream infile;
string getdata;
infile<<getdata;

then when i go to sort the alphabets its sorted properly, but when i
sor the numbers its all messed up as its sorting it based on the ASCII
value of its 1st character and not the entire number... so i tried
converting the string into integer but its not working..

If any of you have any ideas please let me know...

Thanks in advance
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hello everyone.. this is my first post on this forum..

Well i was wondering if any of you could suggest a way for me to read
form an input file that has mixed ASCII integers and characters and
operators.. say a page full of random text contaning numbers,
alphabets and comas and other operators like ( * &^ % $ $ #@ .

I have to read each of the string form the file and then sort it.. now
the problem that i am facing is that when i use the standar

ifstream infile;
string getdata;
infile<<getdata;

then when i go to sort the alphabets its sorted properly, but when i
sor the numbers its all messed up as its sorting it based on the ASCII
value of its 1st character and not the entire number... so i tried
converting the string into integer but its not working..

If any of you have any ideas please let me know...

Use lexicographical comparison:

#include <algorithm>

bool lexComp(const std::string& a, const std::string& b)
{
return std::lexicographical_compare(a.begin(), a.end(),
b.begin(), b.end()
);
}

And then sort the container using

std::sort(c.begin(), c.end(), lexComp);
 
O

osmium

Newbee said:
Well i was wondering if any of you could suggest a way for me to read
form an input file that has mixed ASCII integers and characters and
operators.. say a page full of random text contaning numbers,
alphabets and comas and other operators like ( * &^ % $ $ #@ .

I have to read each of the string form the file and then sort it.. now
the problem that i am facing is that when i use the standar

ifstream infile;
string getdata;
infile<<getdata;

then when i go to sort the alphabets its sorted properly, but when i
sor the numbers its all messed up as its sorting it based on the ASCII
value of its 1st character and not the entire number... so i tried
converting the string into integer but its not working..

Firm up in your mind the difference between a digit and a number, ASCII
represents *digits*. Then form a suitable definition of a number, and
create two files, one of char and one of numbers. Sort the two files
individually. My guess is to print the char file first followed by the
sorted numbers. Is + 3.1416 greater than apple? Or is +3.1416 not a
number? After all, *you* made the rules.

People who make telephone books avoid this problem by spelling out the
numbers.
 
B

BobR

Newbee said:
I have to read each of the string form the file and then sort it.. now
the problem that i am facing is that when i use the standar

ifstream infile;
string getdata;
infile<<getdata;

then when i go to sort the alphabets its sorted properly,

You are not telling the truth! The snippet you showed CAN NOT compile (even
with the dreaded 'using namespace std;' <G>).

Hint: infile >> getdata; // not 'infile<<getdata;'

When you post in this NG, post *real* code (copy/paste).

Please read this FAQ (at least section 5) before posting again. It will help
you in MANY ways.
FAQ http://www.parashift.com/c++-faq-lite

Then try the things the others have suggested, and post your questions here
if you have trouble.
If it's not homework, we can give you some code to help you along,
otherwise, we'll help you learn, show your mistakes, etc..
 
N

Newbee

You are not telling the truth! The snippet you showed CAN NOT compile (even
with the dreaded 'using namespace std;' <G>).

Hint: infile >> getdata; // not 'infile<<getdata;'

When you post in this NG, post *real* code (copy/paste).

Please read this FAQ (at least section 5) before posting again. It will help
you in MANY ways.
FAQ http://www.parashift.com/c++-faq-lite

Then try the things the others have suggested, and post your questions here
if you have trouble.
If it's not homework, we can give you some code to help you along,
otherwise, we'll help you learn, show your mistakes, etc..

thanks a lot to all of you for ur answers and thanks for the pointers
for posting here.. i guess you would be the moderator Bob R??
I have manged to get my code working and make it do what it is
supposed to.. thanks a lot again.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

thanks a lot to all of you for ur answers and thanks for the pointers
for posting here.. i guess you would be the moderator Bob R??
I have manged to get my code working and make it do what it is
supposed to.. thanks a lot again.

Comp.lang.c++ is an unmoderated group, so there are no moderators,
comp.lang.c++.moderated, however, does.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top