473,569 Members | 3,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading from input file

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

Aug 22 '07 #1
5 1663
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::lexicograp hical_compare(a .begin(), a.end(),
b.begin(), b.end()
);
}

And then sort the container using

std::sort(c.beg in(), c.end(), lexComp);

--
Erik Wikström
Aug 22 '07 #2
"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.
Aug 22 '07 #3

Newbee <ap************ *@gmail.comwrot e 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<<getdat a;'

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
Aug 22 '07 #4
On Aug 22, 12:50 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
Newbee <aparnachaudh.. .@gmail.comwrot e 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<<getdat a;'

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.

Aug 24 '07 #5
On 2007-08-24 07:43, Newbee wrote:
On Aug 22, 12:50 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
>Newbee <aparnachaudh.. .@gmail.comwrot e 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<<getdat a;'

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++.m oderated, however, does.

--
Erik Wikström
Aug 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7038
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 file... Thank you to all of you who can help me with this one...
18
2214
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
3
2193
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
8
5217
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 writing into a dynamic array. My problem is that the array shows extra z and probably because of this further processing gives run time error in borland...
40
4480
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 line at a time and print it out. How can I do this? I wrote the code below but it's not correct since the fscanf reads one word (terminating in...
9
2171
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 language is the best known for me. So.. for my future Python script, the input data are in form of: 1 1233.2E-3 2123.2323 2E+2 3453.3E+1...
0
2182
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 CSV file without any issues only if all the fields are not null. If any field is blank, it moves the values to the left and displays the value under...
21
3020
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 00000000_00 0 000 000 000 0000 000 I need to read the file line by line and eventually parse out each piece of the file and store in arrays that...
4
1911
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 input = &cin; while (!input->eof())
6
3515
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
0
7703
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
6290
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.