C ++文件写/读 [英] C++ file writing/reading
本文介绍了C ++文件写/读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个数组,将数组写入文件,而不是显示它。它似乎是工作,但我只是一部分的输出(前3个元素)或我获得的值超过边界。
include< iostream>
#include< fstream>
using namespace std;
int main()
{
int arr [20];
int i;
for(i = 0; i <5; i ++)
{
cout< 输入数组的值:<< endl;
cin>> arr [i];
}
ofstream fl(numbers.txt);
if(!fl)
{
cout<< 文件不能开放写入!<<< endl;
}
for(i = 0; i {
fl << arr [i] endl;
}
fl.close();
ifstream file(numbers.txt);
if(!file)
{
cout< 从文件读取错误!< endl;
}
while(!file.eof())
{
std :: string inp;
getline(file,inp);
cout<< inp<< endl;
}
file.close();
return 0;
}
解决方案
p>
for(i = 0; i
pre>
看起来不对。
看起来像这样
int size;
size = sizeof(your array);
for(i = 0; i
I'm trying to create an array, write array to the file and than display it. It seems to be working but i get just part of the output (first 3 elements) or i get values over boundaries.
#include <iostream> #include <fstream> using namespace std; int main() { int arr[20]; int i; for (i = 0; i < 5; i++) { cout << "Enter the value to the array: " << endl; cin >> arr[i]; } ofstream fl("numbers.txt"); if (!fl) { cout << "file could not be open for writing ! " <<endl; } for (i = 0; i < arr[i]; i++) { fl<<arr[i]<<endl; } fl.close(); ifstream file("numbers.txt"); if(!file) { cout << "Error reading from file ! " << endl; } while (!file.eof()) { std::string inp; getline(file,inp); cout << inp << endl; } file.close(); return 0; }
解决方案its like hmjd said
for(i=0;i<arr[i];i++)
looks wrong
it should look like this
int size; size=sizeof(your array); for(i=0;i<size;i++)
这篇关于C ++文件写/读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文