在c ++中读取文本文件 [英] Reading in text file in c++

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

问题描述

我有13个int号作为txt文件中的列表。

34

55

99

120

57

45

49

30

32 br />
21

56

22

0


最后一个值是0,并且不是任何计算中使用的值之一。我应该只创建一个数组作为全局变量。

那么除了最后一个(即0)之外,读取文件中所有数字的背后的逻辑是什么(带代码)它们在全局创建的数组中。


如果0位于45到49之间;在这种情况下,我们必须将0作为数组的值。


谢谢!

我希望我能尽快得到解决方案......

解决方案

< blockquote>首先,查找fstream找出如何读取文本文件并存储数据(还有其他方法,但fstream就是其中之一)


然后,了解数组,这样你就可以看到如何放置其中的值(来自文本文件的值)


尝试让文件读取和数组存储工作,人们将更有可能帮助你。如果您遇到任何一个问题,在您阅读并尝试之后,请发布您的代码并让我们知道您遇到什么问题。


If你知道文件中有整数,你只需要12,你可以尝试:


myfile>> arr [0]>> arr [1]等...


或使用一个循环,其中myfile是一个ifstream。


如果我不知道有多少个数字在文件中。它可能超过12个可能是14或16或17个(20个内)。在这里,我必须从文件中获取最后的数字。

就像我已经创建全局数组一样


#include< iostream>

#include< fstream>

using namespace std;

int main()

{

const int size = 20;

int arr [20];

int count = 0;

fstream infile;

infile.open( " c:/ C ++ / array1.txt");

if(!infile)

{

cout<<"错误!无法打开文件。;

}


do

{

infile的>>常用3 [count]个; //所以这里的代码将帮助我读取文件中的倒数第二个整数值。

count ++;

} while(!infile.eof());


cout<< endl<<" ;数组的大小为:<<< count;


for(int i = 0; i< count; i ++)

{

cout<<< endl<<" arr ["<<<<<"] ="<< arr [i];

}


cout<< endl;

infile.close();

返回0;

}


I''ve 13 int number as a list in txt file.
34
55
99
120
57
45
49
30
32
21
56
22
0


The last value is a 0 and is NOT one of the values to be used in any calculation . I am supposed to create only one array as global variable.
So what would be the logic behind(with code) to read all the numbers from the file except the last one (i.e, 0) and put them in globally created array.

and if 0 is situated between 45 and 49; in that case we have to take that 0 as the value of array.

Thank you !
I hope I''ll get solution soon...

解决方案

first, look up "fstream" to find out how to read a text file and store the data ( there are other ways, but fstream is one of them )

then, learn about arrays so you see how you can put values in it ( the values from the text file )

try to get the file reading and array storing to work and people will be much more likely to help you out. If you have problems with either of those, after you''ve read about it and tried it, post your code and let us know what specifically you''re having trouble with.


If you know there are ints in the file and you need only 12, you might try:

myfile >> arr[0] >> arr[1] etc...

or use a loop where myfile is an ifstream.


If I dont know how many number are there in the file. It might more than 12 might be 14 or 16 or 17 whatever (within 20). Here I''ve to pick up the numbers up to second last from a file.
like I already create global array as

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
const int size = 20;
int arr[20];
int count=0;
fstream infile;
infile.open("c:/C++/array1.txt");
if(!infile)
{
cout<<"Error! Can''t open the file.";
}

do
{
infile>>arr[count]; //so here what code will help me to read up to second last integer value from the file.
count++;
}while(!infile.eof());

cout<<endl<<"Size of the array is:"<<count;

for(int i=0;i<count;i++)
{
cout<<endl<<"arr["<<i<<"] = "<<arr[i];
}

cout<<endl;
infile.close();
return 0;
}


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

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