text file processing

Hi all,

I really need some help from you guys..

I have a problem here, I have to make a program , which process text file in sequential way. Data from file sis being read bye one symbol at a time. It may not read the whole file or a line at once.

The text file is given, I need a code which finds and stores in other text file all those lines which contains arbitrary set subsequence "n" (we may assure that the length of the subsequence does not exceed 40 symbols).

And together with the found line shall the number of it is stored in the second text file.

Help is much appreciated.

Thank you,

Martin.
Martin,
the general 'rule' on the forums here is that people will happily help with specific problems in a piece of code, or give pointers on how to approach something, but will generaly expect you to 'have a go' first.
In this case it sounds like you have a few key operations to perform
1) Read the text file into memory
2) Search a line of the text file for the existence of the subsequence
3) Write a line to a second text file if the subsequence if found
Any ideas on how to do these?
Any ideas on a general program structure to achive this?
Post your progress and we'll be happy to assist.
PS: When posting code, if you use the # Format it will put a pair of [code ][/code ] tags. Put the code between the tags and it will format nicely
EG
[code]
int main()
{
//This program does nothing:-)
return 0
}
[/code]
Hello,

Thank you Faldrax for tips and help.

So.. here I am, this peace of code reads text step by step, symbol at a time, from file in.txt into out.txt.

Now I have a problem. What is arbitrary set subsequence "n" .. How does arbitrary instructed line looks like..

for example text file look like this :

1
2
3
FLAC is specifically designed for efficient 
packing of audio data, unlike general lossless 
algorithms such as ZIP and gzip.



I have to find out how such subsequnce can be defined... I am working on this.
update: I found out that It can be later added by User manually, so I have to find the line added by User and have to "cout" the line with it`s appropriate line number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream>
#include <iostream>
using namespace std;

int main ()
{
    char c;
    fstream fin ("in.txt", ios::in);
    fstream fout ("out.txt", ios::out);
    fin.get(c);
    while (fin)
    {
        fout.put(c);
        fin.get(c);
    };
    fin.close ();
    fout.close ();
    
    system ("pause");
    return 0;
}



update:

Ok, I figured out that the arbitrary instructed line will be typed into the program.exe window and the function should work like a search . If for example I type in "FLAC zip audio and other words." The program should store lines and their numbers which contains those words in other file.

Maybe I should use get.line instad of chars only?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <fstream.h>
    int main()
    {
        char str[2000];
        fstream file_op("c:\\in.txt",ios::in);
        while(!file_op.eof()) 
        {
              file_op.getline(str,2000);
              cout <<str;
        }         file_op.close();
        cout <<endl;

        return 0;
    }
Last edited on
Hi, I would tend to use the string class rather then a char array for the lines (see http://www.cplusplus.com/reference/string/string/ fro more info on the string class).
This will allow you to use getline to read a line, and then use the find method of the string class to check for the presence of one of the keywords the user has entered.
Again, if you have the user enter a string for the list of keywords you can use find to get the location of the next space, and substr to chop it up into keywords (sounds like an array of strings is needed for these).
Thanks,

Instead of get.line I have to read in.txt file symbol by symbol ..
Topic archived. No new replies allowed.