C++ 查找文件结尾时出现问题?
从文件读取输入时出现问题,计算每个单词中的字符数,然后将此计数输出到输出文件 输入文件中的示例内容: 一二三四五 正确的输出是: 3 3 5 4 现在,如果在输入文件中,我在“五”的末尾放了一个空格,下面的代码就可以工作了。如果我不把这个空格放进去,代码就会卡在嵌入的while循环中(见下文) 有什么想法吗?提前谢谢C++ 查找文件结尾时出现问题?,c++,file-io,while-loop,infinite-loop,eof,C++,File Io,While Loop,Infinite Loop,Eof,从文件读取输入时出现问题,计算每个单词中的字符数,然后将此计数输出到输出文件 输入文件中的示例内容: 一二三四五 正确的输出是: 3 3 5 4 现在,如果在输入文件中,我在“五”的末尾放了一个空格,下面的代码就可以工作了。如果我不把这个空格放进去,代码就会卡在嵌入的while循环中(见下文) 有什么想法吗?提前谢谢 #include <iostream> #include <fstream> using namespace std; int main() {
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char c; //declare variable c of type char
int count = 0; //declar variable count of type int and initializes to 0
ifstream infile( "input.txt" ); //opens file input.txt for reading
ofstream outfile( "output.txt" ); //opens file output.txt for writing
c = infile.get(); //gets first character from infile and assigns to variable c
//while the end of file is not reached
while ( !infile.eof() )
{
//while loop that counts the number of characters until a space is found
while( c != ' ' ) //THIS IS THE WHILE LOOP WHERE IT GETS STUCK
{
count++; //increments counter
c = infile.get(); //gets next character
}
c = infile.get(); //gets next character
outfile << count << " "; //writes space to output.txt
count = 0; //reset counter
}
//closes files
infile.close();
outfile.close();
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
char c;//声明char类型的变量c
int count=0;//int类型的declar变量计数并初始化为0
ifstream infle(“input.txt”);//打开文件input.txt进行读取
ofstream outfile(“output.txt”);//打开文件output.txt进行写入
c=infle.get();//从infle中获取第一个字符并分配给变量c
//而未到达文件的结尾
而(!infle.eof())
{
//while循环,计算字符数,直到找到空格为止
while(c!='')//这是while循环,它被卡住了
{
count++;//递增计数器
c=infle.get();//获取下一个字符
}
c=infle.get();//获取下一个字符
外文件修改内文件的条件,同时:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char c; //declare variable c of type char
int count = 0; //declar variable count of type int and initializes to 0
ifstream infile( "input.txt" ); //opens file input.txt for reading
ofstream outfile( "output.txt" ); //opens file output.txt for writing
c = infile.get(); //gets first character from infile and assigns to variable c
//while the end of file is not reached
while ( !infile.eof() )
{
//while loop that counts the number of characters until a space is found
while( c != ' ' && !infile.eof() )
{
count++; //increments counter
c = infile.get(); //gets next character
}
c = infile.get(); //gets next character
outfile << count << " "; //writes space to output.txt
count = 0; //reset counter
}
//closes files
infile.close();
outfile.close();
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
char c;//声明char类型的变量c
int count=0;//int类型的declar变量计数并初始化为0
ifstream infle(“input.txt”);//打开文件input.txt进行读取
ofstream outfile(“output.txt”);//打开文件output.txt进行写入
c=infle.get();//从infle中获取第一个字符并分配给变量c
//而未到达文件的结尾
而(!infle.eof())
{
//while循环,计算字符数,直到找到空格为止
而(c!=“”&!infle.eof())
{
count++;//递增计数器
c=infle.get();//获取下一个字符
}
c=infle.get();//获取下一个字符
outfile解决此问题的另一种方法是简化:
#include <fstream>
#include <string>
int main()
{
std::string word;
std::ifstream infile("input.txt");
std::ofstream outfile("output.txt");
while (infile >> word)
outfile << word.size() << ' ';
}
#包括
#包括
int main()
{
字符串字;
std::ifstream infle(“input.txt”);
std::ofstream outfile(“output.txt”);
while(内嵌>>word)
outfile通过检查以下字符来检查EOF:
while(fgetc(infle)!=EOF){
//Rest代码在这里
}
而(!infle.eof())
不,不,不,不,不,不,不,不,不,不,不,不,不,不,不,不,不,不,不,不,不,不,不,不,不。只是想扩大我的理解。@miltonb谢谢@user657267。我的代码现在必须立即更改。谢谢,这很有效!到达我需要去的地方的更简单的方法。我将来需要亲吻!