我如何从C ++中的.txt文件中读取 [英] how do I read from a .txt file in C++

查看:327
本文介绍了我如何从C ++中的.txt文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>
#include<string>


using namespace std;
char PrintCharacter(char);

char PrintCharacter(char sWord){
	 

char cAlp[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"};
char cAl[]= {"YPLTAVKREZGMSHUBXNCDIJFQOWypltavkrezgmshubxncdijfqow"};       

for(int i=0;i<52;i++){
	if(sWord!=cAlp[i])
	continue;
	else
	cout << cAl[i];
}

}



int main(){
string sSent;
sSent = "How do I do this";

	int iLength = sSent.length();
	
	for(int iStart=0;iStart<ilength;istart++){>
		if(isalpha(sSent[iStart]))
		PrintCharacter(sSent[iStart]);
		else
		cout <<sSent[iStart];
		}

/* y p l t a v k r e z g m s h u b x n c d i j f q o w */		
 
}







在此代码中,我正在翻译句子我该怎么做这个

到Ruf tu E tu drec< ---在al bhedizers。





so我的问题是句子在里面.cpp

我们的任务是在.txt文件中创建句子然后程序将读取它并输出.txt文件。



还有办法吗?我读了一些关于如何但我不明白如何应用它。

抱歉没有问题。




in this code, im translating the sentence "How do I do this"
to Ruf tu E tu drec <--- in al bhedizers.


so my problem is that the sentence is in inside .cpp
we were tasked to make the sentence inside a .txt file then the program will read it and output in also in a .txt file.

is there a way to do that?? i read some on how but I dont understand how to apply it.
sorry for noob questions.

推荐答案

你可以使用 std :: getline [ ^ ]函数,例如,以下程序尝试使用foo.txt执行此操作文件

You may use the std::getline[^] function, for the purpose, for instance, the following program tries to do it with the "foo.txt" file
int main()
{
  ifstream ifs("foo.txt");
  while (ifs.good())
  {
    string line;
    getline(ifs, line);
    for (int n = 0; n < line.length(); ++n)
    {
      char c = line[n];
      if ( isalpha(c) )
        PrintCharacter(c);
      else
        cout << c;
    }
    cout << endl;
  }
}


这篇关于我如何从C ++中的.txt文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆