Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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! :)
Posted
Updated 3-Aug-11 6:15am
v2

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. 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! :)


Cheers!

—MRB
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 14:13pm    
You're too generous! You even had to do some guesswork, even though OP was supposed to explain the data format. My 5.
--SA
Espen Harlinn 5-Aug-11 13:21pm    
Great reply, my 5
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.
C++
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();
}
 
Share this answer
 
Comments
Espen Harlinn 5-Aug-11 13:22pm    
Great reply, my 5
Alex_RO 5-Aug-11 13:25pm    
thanks ;)
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>


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!!!
 
Share this answer
 
v2
you can use
char c1[1000];
FILE *fp;
while(!fp.eof())
fgets(fp,c1,1000);
here you will get the whole thing which ever is there in the whole line and the you can make modification that are needed accordingly k and select that which ever is needed
 
Share this answer
 
If the file, for example:
VB
1
bird
2
dog
cow
3
camel
cat
mouse
4
rabbit
horse
zebra
lion


1. Read the file using std::ifstream as in solution 3, for example.
2. Write it in std::string
3. Using std::stringstream split string in separate words and calculate the number of words;
4. Write separate words in std::vector<string>
5. Output only needed elements:
int k=2;
for(int i=1;
i<n;>
i=i+k)
{
cout << valVector[i]<<endl<<'\n';
k++;
}

This algorithm works.
 
Share this answer
 
v8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900