⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c语言读取文件.cpp

📁 湖南大学ACM-OJ的部分题目代码
💻 CPP
字号:
/*#include<iostream>
#include<fstream>
using namespace std;
int main()
{
	int a;
	char x[256];
	double y;
	FILE *file;
		file=fopen("11.txt","r");
	fscanf(file,"%d%s%f\n",&a,x,&y);
	cout<<a<<x<<y<<endl;
	return 0;
}

// 逐词读取方法一 
#include <fstream> 
#include <string> 
#include <iostream> 
using namespace std; 

int main() 
{ 
ifstream ifs("11.txt"); // 改成你要打开的文件 
streambuf* old_buffer = cin.rdbuf(ifs.rdbuf()); 

string read; 
while(cin >> read) // 逐词读取方法一 
cout << read; 
cin.rdbuf(old_buffer); // 修复buffer
return 0; 
} 


// 逐词读取方法二  
#include <iostream> 
#include <fstream> 
using namespace std; 

int main() 
{ 
ifstream ifs("11.txt"); // 改成你要打开的文件 
ifs.unsetf(ios_base::skipws); 

char c; 
while(ifs.get(c)) // 逐词读取方法二 
{ 
if(c == ' ') 
continue; 
else 
cout.put(c); 
} 
return 0; } 


// 逐词读取方法三 
#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

int main() 
{ 
ifstream ifs("11.txt"); // 改成你要打开的文件 

string read; 
while(getline(ifs, read, ' ')) // 逐词读取方法三 
{ 
cout << read << endl; 
} 
return 0;} 


 // 逐词读取方法四
#include <fstream> 
#include <iostream> 
using namespace std; 

int main() 
{ 
ifstream ifs("11.txt"); // 改成你要打开的文件 

char buffer[256]; 
while(ifs.getline(buffer, 256, ' ')) // 逐词读取方法四 
{ 
cout << buffer; 
} 
return 0;}



#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
fstream fs("11.txt",ios::in);
string str;
while(!fs.eof())
{
getline(fs,str);           //按行读入
string::size_type ix = 0;  //定位index,取每行第一个字符
cout<<str[ix]<< endl;   //string [] 操作
}
fs.close();
system("pause");
return 0;
}
*/

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
fstream fs("11.txt", ios::in|ios::out);
string str;
while(!fs.eof())
{
getline(fs,str);
string::size_type ix = 0;
cout << str[ix] << endl;

}

system("pause");
return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -