| werlay (47) | |||
|
i need to search through a .dat file for the positions of a string but my code only reads the first line. the content of my file is below. GETGGGGGGGGGATGGGGGGATETGGAEGETAEEEEGETEEAEEEAEAEEEAGGAGGATAEAGATAEAGAGAEATAGGAEATAGAGTAGAAGAGGAGA AGAEAEEAGETGGAGAGGAAGETGAGAGGAGAGAGETGAGAGAGGAGAGAGGAGAGAGGETAGAGAGGAGAGGAGAGAEGAGAGGAGAGAGGETGAGAGAGGGETETTTTTTETAGGGET Below is the fuction i created to do this but it just reads the first line alone
can a pro please help me see whats wrong, i thought it was the break; but removing it even makes it worse. NB. its not object oriented coding please thats too much for me for now. thanks in advance guys | |||
|
|
|||
| werlay (47) | |
| please N>B the content of the file has about 7 lines | |
|
|
|
| JLBorges (1756) | |||
while ( yourfile.good() ) is bad.
| |||
|
|
|||
| SamuelAdams (321) | ||||
|
I know your tired of me, but i'm trying... BTW, it's easier to keep up if you don't open a new post every time you ask the same question. Your code works, to find the 1st match in both line 1 and line 2. I had to change it a little to get it to compile. I don't see how my changes would effect the results, but I dont have your entire program. - The extra includes just make it quicker and easier for me to test code. Remove what you dont need. - All I changed should be flagged by // changed
Output
| ||||
|
|
||||
| SamuelAdams (321) | ||||
|
This one find all instances of the string on all lines. It may be messy, I didn't make any attempts to clean it up.
Output:
| ||||
|
Last edited on
|
||||
| werlay (47) | |
|
thanks alot peeps. my code was right afterall but its doing the wrong thing. its only reading the position of the first occurence in each line. i want it to read the position of every occurence in all lines. please | |
|
|
|
| werlay (47) | |
|
thanks peeps, my code works afterall but its doing the wrong thing. it only searches for the position of the first occurence in each line rather than seaching for all occurence in all line. i thing the "find" fuction is the problem... any idea of how to get this done? | |
|
|
|
| werlay (47) | |
| please any help with the above problem? | |
|
|
|
| JLBorges (1756) | |
|
Have you read this? http://www.cplusplus.com/forum/general/88575/#msg475300 If you have, what problems are you having with it? Hint: code which has a construct like while ( yourfile.good() ) without a if( !std::getline( yourfile, u_file ) ) break ; inside the loop is not going to work correctly. | |
|
Last edited on
|
|
| werlay (47) | |
| @JLB,i tried using ur example but it get even worse. i dont understand ur immediate post | |
|
|
|
| JLBorges (1756) | |||
|
> i tried using ur example but it get even worse. It would be a lot easier if you posted the code that you tried, along with what are the errors that you got. > i dont understand ur immediate post
| |||
|
|
|||
| werlay (47) | |
| yeppie!!!!! JLB you are the best!!! seen my mistake. what about reading all content of a file as a whole string. so that the programme reads and search the content as a whole string not as an individual line even if the content has spaces in it. | |
|
|
|
| JLBorges (1756) | ||||
> seen my mistake.
> what about reading all content of a file as a whole string. For that, the file can't be huge - its contents have to fit into memory. And if line number information is required, the string would have to be parsed for new lines.
| ||||
|
|
||||
| MikeyBoy (235) | ||
This is so true. In a previous job, we used to call it the "Cardboard Programmer" phenomenon; you didn't really need an actual programmer to explain your problem to - a cardboard cut-out of a programmer would serve the same function :D | ||
|
|
||
| werlay (47) | |
|
thanks for the tip mate. is there no other way to do this... the above code seem complicated and does not work sort-off | |
|
|
|
| werlay (47) | |
| Anybody wanna help me please....reading all content of a file as a whole string. so that the programme reads and search the content as a whole string not as an individual line even if the content has spaces in it. | |
|
|
|
| JLBorges (1756) | |||
|
> and does not work sort-off Repeat: It would be a lot easier if you posted the code that you tried, along with the errors that you got. > is there no other way to do this... the above code seem complicated
| |||
|
|
|||
| werlay (47) | |
| @jlb thanks alot you are amazing. Can u send me your mail address so i can show you what exactly am trying to achieve? | |
|
|
|
| JLBorges (1756) | |
|
> so i can show you what exactly am trying to achieve? You would get better mileage if you post it here. Many eyes are better than one. | |
|
|
|
| werlay (47) | |||
|
okay lets put it like this. like my first post says. i have a file that contains different letters of many lines (about 8 likes). i wanna get search for the file and get the position of a certain 3 letter string. but the output searchs the lines individually, thus getting a result like this. " found at position : 5,21,34,25,5,45,23,5, " instead of somrthing like 5, 21,34,67,87,91,110 etc; you can see that it finds the position 5 twice,
what think should be done is getting is converting each letters into char and passing them into an array, line by line. but i dont know how to do this i hope this explain it better. any suggestions | |||
|
|
|||