ifstream

Hello.
I was wondering if i can choose from a text file with numbers inside the number i want exactly. For example the numbers are in 5 lines.:
5
3
6
2
7

Can i choose what to show? I want to show up only the number 2 or 7 or 3!
Is there any way to do that????
1
2
3
4
5
6
int i = 0;
while (fin >> variable[i]) {
    if(variable[i] == 2 || variable[i] == 3 || variable[i] == 7)
        cout << variable[i];
    i++;
}


assuming i understand your question correctly, that will print out 2, 3 and 7 each time it reads it in from the file
Last edited on
These numbers was an example.
the txt file may have 50 numbers or 100 or 10
these numbers i take them with a loop.
1
2
3
4
5
6
7
8
 ifstream f;
f.open("data.txt");
if(f.is_open());
{
  f>>a;
 
for(int i=0; i<a; i++)
{f>>g;}

and i didnt explained that the first number will be the nuber of the lines.
5
2
5
6
3
4
. and i want to choose one number of them.

Last edited on
is there any way to do this??

1
2
3
4
5
6
7
8
9
10
int g[];
 ifstream f;
f.open("data.txt");
if(f.is_open());
{
  f>>a;
 
for(int i=0; i<a; i++)
{f>>g[];}
cout<<g[i];
1
2
3
4
5
6
7
8
9
10
11
int g[]; //you need to give this array an explicit size
 ifstream f;
f.open("data.txt");
if(f.is_open());
{
  f>>a;
 //no closing brace for this if statement
for(int i=0; i<a; i++)
{f>>g[];} //you need to index that array
cout<<g[i]; // this is outside the for loop. i is local to the for loop, so this won't compile.


if you are looking for one specific number and you want to print it out when you find it, just put an if statement inside the for loop that will print g[i] when g[i] == the number you want
Last edited on
All i want is to do a bubble sort in this txt file.
And i dont now how exactly.And i think this way will help me.If i find how to do that i will continue i think.And i searched the forum for the bubble sort but i didnt find some good tutorials with fstream.
it doesn't matter whether you're reading from a file or not. just read the contents of the file into your array and run the array through a bubble sort.
And how can i do that???
Topic archived. No new replies allowed.