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 5 1657
On 2007-08-22 11:03, Newbee wrote:
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);
--
Erik Wikström
"Newbee" writes:
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.
Newbee <ap*************@gmail.comwrote in message...
>
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..
--
Bob R
POVrookie
On Aug 22, 12:50 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
Newbee <aparnachaudh...@gmail.comwrote in message...
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..
--
Bob R
POVrookie
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.
On 2007-08-24 07:43, Newbee wrote:
On Aug 22, 12:50 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
>Newbee <aparnachaudh...@gmail.comwrote in message...
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..
-- Bob R POVrookie
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.
--
Erik Wikström This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: fabrice |
last post by:
Hello,
I've got trouble reading a text file (event viewer dump) by using the
getline() function...
After 200 - 300 lines that are read correctly, it suddenly stops
reading the rest of the...
|
by: Michael |
last post by:
Hi,
I moved to c++ from c, and wanted to know what the best way to read data
from files is in c++. Any thoughts? fscanf() is possible but fairly painful!
Regards
Michael
|
by: SB |
last post by:
Hello. I have an input file which is laid out in the following manner...
Name
Day 1
am time 1
am time 2
appointment
pm time 1
pm time 2
appointment
Day 2
|
by: dbuser |
last post by:
Hi,
I need help on a problem, as described below. I am reading a file
"input.txt"which has data like this:
abc def gh izk lmnopq rst uvwxyz
I am using fstream object to read the file and...
|
by: googler |
last post by:
I'm trying to read from an input text file and print it out. I can do
this by reading each character, but I want to implement it in a more
efficient way. So I thought my program should read one...
| |
by: paczkow |
last post by:
Dear Python Community,
I am an engineering and I am experiencing some trouble. Having output
data from other software I want to use it. To achieve this I decided to
use Python since this...
|
by: Anish G |
last post by:
Hi,
I have an issue with reading CSV files.
I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
|
by: Stephen.Schoenberger |
last post by:
Hello,
My C is a bit rusty (.NET programmer normally but need to do this in
C) and I need to read in a text file that is setup as a table. The
general form of the file is
00000000 USNIST00Z...
|
by: tushar.saxena |
last post by:
Hi,
I'm trying to read a file using the istearm class (I cant use ifstream
since the input might be a file or it might be stdin).
istream *input;
// Add checks for file name here, else use...
|
by: efrenba |
last post by:
Hi,
I came from delphi world and now I'm doing my first steps in C++. I'm
using C++builder because its ide is like delphi although I'm trying to
avoid the vcl.
I need to insert new features...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |