problem getting all lines read in

Please anyone, Am having problems reading all lines of my txt file.
below is the function i wrote to read the data in the file and search thru them. but it appears to read just only the first line.. please anyone.


int stop_tag(string f_name, string u_file,string file_put)
{
ofstream f_out;
f_out.open(file_put, ios::app);
f_out<<"Location of STOP condons TAG found at Locations :" <<endl;
ifstream yourfile (f_name);
size_t search;
if (yourfile.is_open())
{
while ( yourfile.good() )
{
getline (yourfile,u_file);
for ( int p = 1; p < u_file.length(); p++)
{

search = u_file.find("TAG");
if ( search!= string::npos)
{
cout <<search<<",";
f_out<<search<<",";
break;
}
}
}
}f_out<<endl;
return 0;
}
maybe the break on IDK line number is at fault.

IDK=I don't know the line number because you didn't format your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int stop_tag(string f_name, string u_file,string file_put)
{
ofstream f_out;
f_out.open(file_put, ios::app);
f_out<<"Location of STOP condons TAG found at Locations :" <<endl;
ifstream yourfile (f_name);
size_t search;
if (yourfile.is_open())
{
while ( yourfile.good() )
{
getline (yourfile,u_file);
for ( int p = 1; p < u_file.length(); p++)
{

search = u_file.find("TAG");
if ( search!= string::npos)
{
cout <<search<<",";
f_out<<search<<",";
break;
}
}
}
}f_out<<endl;
return 0;
}



Thats the formated copy please help guys
That's not formatted, it only has line numbers.

I had to work and work to get your code to compile, This is formatted, and complies. I don't suggest you use it, but did you try to take out the break statement on line 21 yet ?

If the break is not at fault, it may be the logic.
cout your values for search, u_file.length, p, and any I missed to see if the values are what you expected. I can't compile your code as is and don't know what the input is so can't really help there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include <conio.h>
#include <cstdlib>
#include <dos.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <windows.h>
using namespace std;

int main()
{
string f_name;
string u_file;
string file_put;
    
ifstream yourfile ("file.txt");
// size_t search;
if (yourfile.is_open())
{
   while ( yourfile.good() )
   {
      getline (yourfile,u_file);
      cout << u_file <<  endl;
   }
}
else
   {cout << "File not open";}

return 0;
}


*edit* don't know why you need the if on line 8, your not doing anything with it.
Last edited on
thanks alot, but your coding only still shows the first line alone. my file has different lines of strings. what i want to do is search for all lines to get the positions of a particular string "TAG" as above. but all it seem to do is only search in the first line alone. i hope this gives you more understanding of what am trying to do.. i need help seriously.
My program shows all lines, so maybe it's your data. How about posting the first 3-4 lines of your data.

*Edit*

This was my test file and what the output should look like with the above program.


C:\Temp>type out.txt
3 6
3.9 4.5
3.1 4.2 5.1
1.2 2.3 3.1 4.2
A
ABC
AB CD EF
G H I J
C:\Temp>testxxx
3 6
3.9 4.5
3.1 4.2 5.1
1.2 2.3 3.1 4.2
A
ABC
AB CD EF
G H I J

C:\Temp>
Last edited on
the programme above only cout << the content of the file but no search for positions of a particular string<<
u get me?
Topic archived. No new replies allowed.