文件I / O和转换 [英] File I/O and conversion
问题描述
全部,
我有一个整数的文本文件。我可以格式化文件,使
整数为每行1或连续。
读取文件不是问题。我想要做的是总结
整数并得到一个均值。很容易,但是如何将我读入的
字符或字符串转换为int类型?我在输入文件中尝试使用getline,1 /
整数,并使用1
行中的整数。没有运气。
首次尝试
-------------------
字符串行;
ifstream myfile(f);
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile,line);
// sum = sum +(int)line; //不工作
运行++;
}
myfile.close();
}
其他cout<< 无法打开文件;
mean = sum / running;
cout<< Mean = <<平均<< "总和 <<总和<< "跑步 <<运行
<< endl<< endl ;;
第二次尝试
----------------------
>
ifstream f1(f);
if(!f1)
{
cout<< 打开文件时出错 <<结束;
退出(1);
}
而(f1.get(ch)!= NULL)
{
// int tmp =(int)ch; //不工作
sum = sum + tmp;
cout<< ch << ch<< "总和 <<总和<<结束;
运行++;
}
mean = sum / running;
cout< < Mean = <<平均<< "总和 <<总和<< "跑步 <<
正在运行<< endl;
非常感谢任何建议/指示。
问候,
吉姆
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
推荐答案
Evyn< Ev *********** @ gmail.comwrites:
Evyn <Ev***********@gmail.comwrites:
>如何将我读到的
字符或字符串转换为int类型?
>how do I convert the
character or string I read into type int?
我没有详细阅读你的代码,但是
*要将字符串转换为数字,阅读常见问题
http://www.parashift.com/c++-faq-lit....html#faq-39.2
*如果您从文件中读取字符,可能性是你在读取ASCII字符时只需要
- 即文件中的49代表''1',
等
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.
看看以下功能:
atoi()
cin
还有一个词汇解析器在boost中执行此操作,但在这种情况下使用大锤破解花生可能是
。
Jonathan。
-
9月27日下午12:42,Evyn< Evan.Dembs ... @ gmail。 comwrote:
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 <Evan.Dembs...@gmail.comwrote:
HI all,
我h ave一个整数的文本文件。我可以格式化文件,使
整数为每行1或连续。
读取文件不是问题。我想要做的是总结
整数并得到一个均值。很容易,但是如何将我读入的
字符或字符串转换为int类型?我在输入文件中尝试使用getline,1 /
整数,并使用1
行中的整数。没有运气。
首次尝试
-------------------
字符串行;
ifstream myfile(f);
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile,line);
// sum = sum +(int)line; //不工作
运行++;
}
myfile.close();
}
其他cout<< 无法打开文件;
mean = sum / running;
cout<< Mean = <<平均<< "总和 <<总和<< "跑步 <<运行
<< endl<< endl ;;
第二次尝试
----------------------
ifstream f1(f);
if(!f1)
{
cout<< 打开文件时出错 <<结束;
退出(1);
}
而(f1.get(ch)!= NULL)
{
// int tmp =(int)ch; //不工作
sum = sum + tmp;
cout<< ch << ch<< "总和 <<总和<<结束;
运行++;
}
mean = sum / running;
cout< < Mean = <<平均<< "总和 <<总和<< "跑步 <<
正在运行<<结束;
非常感谢任何建议/指示。
问候,
Jim
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
9月27日下午1:49,t ... @ eng.cam.ac.uk(Tim Love)写道:
On Sep 27, 1:49 pm, t...@eng.cam.ac.uk (Tim Love) wrote:
我没有详细阅读你的代码,但
*要将字符串转换为数字,请阅读常见问题解答
http:// www。 parashift.com/c++-faq-lit...sues.html#faq- ...
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
这篇关于文件I / O和转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!