Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > File I/O and conversion

Reply
Thread Tools

File I/O and conversion

 
 
Evyn
Guest
Posts: n/a
 
      09-27-2007
HI all,

I have a text file of integers. I can format the file so that the
integers are 1 per line, or continuous.

Reading the file is not a problem. What I want to do is sum the
integers and get a mean. Easy enough, but how do I convert the
character or string I read into type int? I have tried with getline, 1
integer per line in my input file, and get with the integers in 1
line. No luck.

First attempt
-------------------

string line;
ifstream myfile (f);
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
// sum = sum + (int) line; // Does not work
running++;
}
myfile.close();
}
else cout << "Unable to open file";
mean = sum/running;
cout << "Mean = " << mean << " sum " << sum << " Running " << running
<< endl << endl;;


second attempt
----------------------

ifstream f1(f);
if(!f1)
{
cout << "Error opening file" << endl;
exit(1);
}
while(f1.get(ch) != NULL)
{
// int tmp = (int) ch; // Not working
sum = sum + tmp;
cout << "ch " << ch << " sum " << sum << endl;
running++;
}

mean = sum/running;
cout << "Mean = " << mean << " sum " << sum << " Running " <<
running << endl;


Any advice/pointers much appreciated.

Regards,
Jim

 
Reply With Quote
 
 
 
 
Tim Love
Guest
Posts: n/a
 
      09-27-2007
Evyn <(E-Mail Removed)> writes:

>how do I convert the
>character or string I read into type int?

I've not read your code in great detail, but

* To convert a string to a number, read the FAQ
http://www.parashift.com/c++-faq-lit....html#faq-39.2

* If you read characters from the file, the odds are that you're
reading ASCII characters - i.e. a 49 in the file would represent '1',
etc.

 
Reply With Quote
 
 
 
 
Jonathan Lane
Guest
Posts: n/a
 
      09-27-2007
Have a look at the following functions:
atoi()
cin

There's also a lexical parser in boost which does this but is probably
using a sledgehammer to crack to a peanut in this case.

Jonathan.

--

On Sep 27, 12:42 pm, Evyn <(E-Mail Removed)> wrote:
> HI all,
>
> I have a text file of integers. I can format the file so that the
> integers are 1 per line, or continuous.
>
> Reading the file is not a problem. What I want to do is sum the
> integers and get a mean. Easy enough, but how do I convert the
> character or string I read into type int? I have tried with getline, 1
> integer per line in my input file, and get with the integers in 1
> line. No luck.
>
> First attempt
> -------------------
>
> string line;
> ifstream myfile (f);
> if (myfile.is_open())
> {
> while (! myfile.eof() )
> {
> getline (myfile,line);
> // sum = sum + (int) line; // Does not work
> running++;
> }
> myfile.close();
> }
> else cout << "Unable to open file";
> mean = sum/running;
> cout << "Mean = " << mean << " sum " << sum << " Running " << running
> << endl << endl;;
>
> second attempt
> ----------------------
>
> ifstream f1(f);
> if(!f1)
> {
> cout << "Error opening file" << endl;
> exit(1);
> }
> while(f1.get(ch) != NULL)
> {
> // int tmp = (int) ch; // Not working
> sum = sum + tmp;
> cout << "ch " << ch << " sum " << sum << endl;
> running++;
> }
>
> mean = sum/running;
> cout << "Mean = " << mean << " sum " << sum << " Running " <<
> running << endl;
>
> Any advice/pointers much appreciated.
>
> Regards,
> Jim



 
Reply With Quote
 
Evyn
Guest
Posts: n/a
 
      09-27-2007
On Sep 27, 1:49 pm, (E-Mail Removed) (Tim Love) wrote:
> I've not read your code in great detail, but
>
> * To convert a string to a number, read the FAQ
> http://www.parashift.com/c++-faq-lit...sues.html#faq-...


Thanks - I'll give it a read

 
Reply With Quote
 
Evyn
Guest
Posts: n/a
 
      09-27-2007
On Sep 27, 1:50 pm, Jonathan Lane <(E-Mail Removed)>
wrote:
> Have a look at the following functions:
> atoi()
> cin


Thanks - my instict was to use atoi but I got this error: 53 C:\EJD
\irrational.cpp invalid conversion from `char' to `const char*'

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-27-2007
"Evyn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> HI all,
>
> I have a text file of integers. I can format the file so that the
> integers are 1 per line, or continuous.
>
> Reading the file is not a problem. What I want to do is sum the
> integers and get a mean. Easy enough, but how do I convert the
> character or string I read into type int? I have tried with getline, 1
> integer per line in my input file, and get with the integers in 1
> line. No luck.
>
> First attempt
> -------------------
>
> string line;
> ifstream myfile (f);
> if (myfile.is_open())
> {
> while (! myfile.eof() )
> {
> getline (myfile,line);
> // sum = sum + (int) line; // Does not work
> running++;
> }
> myfile.close();
> }
> else cout << "Unable to open file";
> mean = sum/running;
> cout << "Mean = " << mean << " sum " << sum << " Running " << running
> << endl << endl;;
>
>
> second attempt
> ----------------------
>
> ifstream f1(f);
> if(!f1)
> {
> cout << "Error opening file" << endl;
> exit(1);
> }
> while(f1.get(ch) != NULL)
> {
> // int tmp = (int) ch; // Not working
> sum = sum + tmp;
> cout << "ch " << ch << " sum " << sum << endl;
> running++;
> }
>
> mean = sum/running;
> cout << "Mean = " << mean << " sum " << sum << " Running " <<
> running << endl;


1. Read the file data as an ints in the first place, not a string.
2. Use stringstream to convert string to int (C++ way)
3. Use atoi to convert c-style string to int (C way)

#include <sstream>
#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::ifstream Foo("Numbers.txt");
int Value;
while ( Foo >> Value )
{
std::cout << Value << "\n";
}
Foo.close();

std::ifstream Foo2("Numbers.txt");
std::string Line;
while ( std::getline( Foo2, Line ) )
{
std::stringstream Bar( Line );
Bar >> Value;
std::cout << Value << "\n";
}
Foo2.close();

std::ifstream Foo3("Numbers.txt");
while ( std::getline( Foo3, Line ) )
{
Value = atoi( Line.c_str() );
std::cout << Value << "\n";
}

}



 
Reply With Quote
 
Evyn
Guest
Posts: n/a
 
      09-27-2007
> 2. Use stringstream to convert string to int (C++ way)

Thank you - this method seems to work very well.

 
Reply With Quote
 
Tim Slattery
Guest
Posts: n/a
 
      09-27-2007
Evyn <(E-Mail Removed)> wrote:

>On Sep 27, 1:50 pm, Jonathan Lane <(E-Mail Removed)>
>wrote:
>> Have a look at the following functions:
>> atoi()
>> cin

>
>Thanks - my instict was to use atoi but I got this error: 53 C:\EJD
>\irrational.cpp invalid conversion from `char' to `const char*'


atoi wants a C-style string as an argument. It sounds to me like
you're passing it a char instead. This won't work:

char x;
int xi;
x = '1'
xi = atoi(x);

But this will:

char x[5];
int xi;
strcpy (x, "1");
xi = atoi(x);

There's a short discussion of atoi here:
http://www.cplusplus.com/reference/c...dlib/atoi.html

--
Tim Slattery
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://members.cox.net/slatteryt
 
Reply With Quote
 
Evyn
Guest
Posts: n/a
 
      09-27-2007
On Sep 27, 3:00 pm, Tim Slattery <(E-Mail Removed)> wrote:
> Evyn <(E-Mail Removed)> wrote:
> >On Sep 27, 1:50 pm, Jonathan Lane <(E-Mail Removed)>
> >wrote:
> >> Have a look at the following functions:
> >> atoi()
> >> cin

>
> >Thanks - my instict was to use atoi but I got this error: 53 C:\EJD
> >\irrational.cpp invalid conversion from `char' to `const char*'

>
> atoi wants a C-style string as an argument. It sounds to me like
> you're passing it a char instead. This won't work:
>
> char x;
> int xi;
> x = '1'
> xi = atoi(x);
>
> But this will:
>
> char x[5];
> int xi;
> strcpy (x, "1");
> xi = atoi(x);
>
> There's a short discussion of atoi here:http://www.cplusplus.com/reference/c...dlib/atoi.html
>
> --
> Tim Slattery
> (E-Mail Removed)://members.cox.net/slatteryt


Thanks!

 
Reply With Quote
 
Eric.Malenfant@gmail.com
Guest
Posts: n/a
 
      09-27-2007
On 27 sep, 07:42, Evyn <(E-Mail Removed)> wrote:
> HI all,
>
> I have a text file of integers. I can format the file so that the
> integers are 1 per line, or continuous.
>
> Reading the file is not a problem. What I want to do is sum the
> integers and get a mean. Easy enough, but how do I convert the
> character or string I read into type int? I have tried with getline, 1
> integer per line in my input file, and get with the integers in 1
> line. No luck.


C++ iostreams have formatting capabilities, so you don't have to first
read a string from the file, and then convert it to a number. You can
read a number directly from the stream:

std::ifstream f("input.txt");
int num;

while (f >> num){
// Do something with num
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion xhtml champs Python 0 06-21-2011 11:59 AM
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion PSD to XHTML Conversion Services and PSD to HTML CSS Conversion Services, PSD to Joomla, Drupal, Wor VHDL 0 04-25-2011 06:43 AM
conversion operator and conversion ctor subramanian100in@yahoo.com, India C++ 2 09-15-2009 12:46 PM
Lexxia: an XML Processor for File Conversion and generation of LaTex John Redmond XML 0 03-26-2009 03:22 AM
Reading compressed file and its conversion to numbers. Henry Lenzi Perl Misc 6 02-24-2005 03:09 AM



Advertisments