如何读取和输出文本数据
有个5列70行的文本数据,如何通过C++来读取
[解决办法]
fscanf
[解决办法]
fgets读
fprintf写
[解决办法]
数据有什么特点吗?即有什么规律。如果有规律可以使用fscanf();还有一个问题,这些在文件中内容你是如何写进去的,如果是通过别人的程序以二进制的形式写入的,那么我想还是使用fread()和fwrite();函数比较好。如果是手动写入的,那么可以使用fscanf();如果是没有规律的,那么就直接使用fgets();
[解决办法]
FILE* file;
file.open(文件名字,"a+");
rewind(file)
fread(.......);
fwrite(...........);
fcsanf 格式化输入
fprintf 格式化暑促
[解决办法]
我这儿有个例子,你拿去看看可能会有帮助~
#include<iostream>
#include<fstream>
using namespace std;
int test_write()
{
ofstream outf("f3.dat",ios::binary);
if(!outf)
{
cout<<"Cannot open output file.\n";
exit(1);
}
char ch='a';
for(int i=0;i<26;i++)
{
outf.put(ch);
ch++;
}
outf.close();
return 0;
}
int test_read()
{
ifstream inf("f3.dat",ios::binary);
if(!inf)
{
cout<<"Cannot open input file\n";
exit(1);
}
char ch;
while(inf.get(ch))
cout<<ch;
inf.close();
return 0;
}
int main()
{
test_write();
test_read();
return 0;
}
[解决办法]
可以看看我空间C++ 栏目的读取txt