如何从文件加载数据 [英] How to load data from a file

查看:82
本文介绍了如何从文件加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我只想知道如何从文件中选择数据并打印.
例如,如果文件名input.txt包含以下详细信息:

3
2


3
骆驼

鼠标
4
兔子

斑马
狮子

鉴于此,我想读取文件并在一行中打印鼠标狮子.

谢谢! :)

Hello everyone,

I just want to know how to select a data from a file and print it.
for example if there is a file name input.txt that contains the following details:

3
2
dog
cow
3
camel
cat
mouse
4
rabbit
horse
zebra
lion

given this, I want to read the file and print cow, mouse, and lion in one line.

Thank you! :)

推荐答案

因为这很显然是某种赋值或homeork,所以我会给你这么多.

文件格式是通过以下方式构建的:
Since this most obviously some sort of assignment or homeork I''ll give you this much.

The file format is built in this way:

  1. 第一个文件条目始终是一个数字,该数字告诉您它包含多少个部分或集合
  2. 每个部分以一个数字开头,该数字告诉每个部分或集合具有多少个条目
  3. 从您的请求中可以清楚地看到应该输出每个部分或每个集合的最后一个条目.
  4. 因此您知道一个部分从哪里开始(在数字之后),并且知道要读取多少行该号码.
  5. 快走吧! :)

  1. The first file entry is always a number that tells you how many sections or sets it contains
  2. Each section starts with a number that tells how many entries each section or set has
  3. From your request it can be clearly seen that the last entry of each section or set should be output.
  4. So you know where a section starts (after the number) and you know how many lines to read by that number.
  5. Get going already! :)



干杯!

-MRB



Cheers!

—MRB


遵循曼弗雷德·R·比希(Manfred R. Bihy)的思想,我编写了一些代码,在特定数字后找到需要的单词.例如,如果您具有:
4
阿尔法
beta
伽玛
兰达
.... etc ...
将输出"landa".您可以继续执行相同的逻辑,然后根据数字查找其他单词.
Following the ideea of Manfred R. Bihy, I wrote some code that find the word you need after a specific number. For example, if you have:
4
alfa
beta
gama
landa
....etc...
will output "landa".You can continue with the same logic, and find the other words depending of the number.
include iostream
include fstream
include stdlib.h
include ctype.h

char str[10];
	int x;
	char str2[10];

ifstream fi("test2.txt");
if(!fi.is_open())
{cout<<"Imposible to open the file";
exit(1);}
else{
	fi>>str;
	if(isdigit(str[0]))
		x=atoi(str);
	for(int i(0); i!=x; ++i)

       fi>>str2;
       cout<<str2<<endl;
	}
fi.close();
}


伪代码:

使用fstream对象(ofstream/ifstream)打开文件.
读入每个工作(fstream_object>>变量,注意并非在每一行上都有效)
然后,如果变量等于您要搜索的输出.

示例:
Pseuocode:

Open file with fstream object(ofstream/ifstream).
Read in every work(fstream_object >> variable, Note doesn''t work on every line)
Then if the variable equals what you are searching for output.

Example:
#include <iostream>
#include <fstream>

using namespace std;

int main(){
    ifstream data;
    data.open("file.txt")
    string var;

   while(data){
       indata >> var;
       if(var == "mouse" || var == "cat" || var == "lion")
           cout << var;
    }

    return 0;
}</fstream></iostream>



这可能有一些错别字,因为我没有检查它,但这基本上就是它的外观.
下次只需谷歌搜索一些c ++ fstream教程.
祝你好运!



And this may have some typos, for i did not check it but this is basically what it should look like.
And next time just google some fstream tutorials for c++.
Good Luck!!!


这篇关于如何从文件加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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