File I/O and conversion

E

Evyn

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
 
J

Jonathan Lane

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.
 
E

Evyn

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*'
 
J

Jim Langston

Evyn said:
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";
}

}
 
E

Evyn

2. Use stringstream to convert string to int (C++ way)

Thank you - this method seems to work very well.
 
T

Tim Slattery

Evyn said:
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/clibrary/cstdlib/atoi.html
 
E

Evyn

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/clibrary/cstdlib/atoi.html

Thanks!
 
E

Eric.Malenfant

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
}
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top