Extract multiple text file in one program

Hi, is it possible if i want to extract multiple text file in one program and append it to the existing text file? Here, i only know to read one file, i want it to read multiple text file without naming it std::ifstream ifsInputFile ("1.txt"); is it possible to do that? or any better idea?



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
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>

int main ()
{
  // Open a file 
  std::ifstream ifsInputFile ("file1.txt");

  if (ifsInputFile.is_open ())
  {
	std::string strLine;

	// Get every line
	while (std::getline (ifsInputFile, strLine))
	{
	  // Use std::stringstream to isolate words using operator >>
	  std::stringstream ssWordsBuf (strLine);

	  std::string strWord;
	  while (ssWordsBuf >> strWord)
		std::cout << strWord << std::endl;  // <--- Output!
	}

	ifsInputFile.close ();
  }

  return 0;
}



below is my text file

file1

The study is published in the journal Nature Climate Change.
It focuses on the Scotia Sea and the Antarctic Peninsula - the places
where the crustaceans are most abundant.

file2

The team's analysis indicates the centre of krill distribution has now
moved to where more favourable conditions are found, tracking
southward towards the Antarctic continent by about 440km, or four
degrees of latitude.


if i run my code, it will automatically extract the text file and later if i open my file1.txt it will save it as extract word

yes, its possible.

start a loop here, possibly over argc
{
std::ifstream ifsInputFile (make this a variable, perhaps argv[index]);
..etc
}

do you just want to append files? you can do that with console commands too, without any c++ at all.
Last edited on
@jonnin

i change my code to make it as a function. I know this is totally wrong. can you guide me please. Also can u elaborate more about appending files using console command? Thank you so much

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 <iostream>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

void extractWord ()
{
    string filename;
    for (int i = 0; i<2 ; i++)
    {
         ifstream InputFile (filename, argv[i]);

    if (InputFile.is_open ())
        {
            string line;
            while (getline (InputFile, line))
        {
            stringstream wordBuf (line);
            string word;
            while (wordBuf >> word)
            cout << word <<endl;
        }

        InputFile.close ();
        }
    }

}

int main ()
{
    extractWord ();
}
also i ask wrongly, the text file should not append but change whole text it to the extract word line by line
you need a list of the file names, either hard coded. argv was a suggestion that you put the names on the commandline when you run the program or drag and drop (multiple allowed) style. Google argc /argv in c++ if you want to go this route. then programname file1 file2 file3 ... would just work, or select all 3 and drag & drop works too.

try this:
void extractWord ( string filename)
{
ifstream InputFile(filename);
if (InputFile.is_open ()
///etc ok from here

at the end:
ofstream outfile(targetfilename) //open this to append. you may have to do outfile.open(..., ios::app) but you could also just make it static and open it once.
ofs << word << endl; //replace cout here
}

main()
{
extractWord("file1.txt");
extractWord("file2.txt");
}

and instead of cout, you can open another target file and write to it (can be same file every time, remember to set the open flags for appending).

------------
from the commandline, in windows use type, in unix, use cat,
eg
cat file1 > targetfile; //> means overwrite the file if existing.
cat file2>> targetfile; //>> means append to it.
targetfile now has the 2 files stuck together.
its exactly the same except cat is type in windows.

you can use your programs too:
myprogram > output.txt //everything in a cout or similar print to console will go to the file now!

you are going to notice that I keep putting the work back to you. If you really can't do it I will do it for you but try. Play with it. Learn. You can do this!
Last edited on
okay i got it now, but do you know if its possible to write the c++? after i extract the file 1, i want it to change my original file1. so went i open it, it is no longer in paragraph. because i don't think i may be able to use the console command because i need to write the c++ code
the console is just to append. I was not sure exactly what you wanted. Still, keep it in mind, there are times when that stuff is really handy. If you were on unix and wanted to go to a lot of trouble you could probably find one of the commandline tools to split it to words for you, but lets skip that unless you just want to go there. At some point, the c++ is easier to do, for really basic stuff, the console is easier... path of least resistance.

of course you can do it in c++.
open the file, read everything and process it to a list of words, close the file, open it again as output, overwrite it with your word list. You need to modify what you have in this case, as you are not keeping your words around currently. Another way would be to write a new file, delete the old file, and rename the new file -- clunky, but it would use what you have without as much change.


Last edited on
If possible can you provide me some code hint? I will appreciate it a lot since i just taking this subject
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ROUGHLY, you may need to clean it up but this is the approach/idea:

void extractWord (string filename)
{
     ifstream InputFile (filename);   //check for open, I left that out 
     vector<string> words;
     string s;
     while(InputFile >> s)
     {
          words.push_back(s);
     }     
       InputFile.close ();
      ofstream InputFile (filename);           
     for(int i = 0; i < words.size(); i++)
      InputFile << words[i] << endl;
      InputFile.close ();
}

which should write 1 word per line from the file back over the original file. 
Last edited on
oh i read the question wrongly, i suppose to store the extract into memory not overwrite the text file. do i just need to include #include <memory> or i still need to code? because my professor said it can be done using the library? is it possible?
<memory> is some very low level tools you probably don't want right now.
the vector I showed you IS stored in memory. Remove the file overwrite part and its good, I think? As above... do whatever you need to with 'words'.

Last edited on
here my code is. Okay the instruction is

1) I need to extract the text file and store it in memory
2) do indexing construction for the text file i extract ( which means i need to sort. am i right)
3) do the data structure implementation
4) search result output
- which text file (i have 2 text file, it should show me if the word is in the file1 or file 2)
-which line
-the word itself

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

int extractWord (string filename)
{
    ifstream InputFile(filename);
    if (InputFile.is_open())
        {
            string line;
            while (getline (InputFile, line))
        {
            stringstream wordBuf (line);
            string word;
            while (wordBuf >> word)
            cout << word <<endl;
        }

        InputFile.close ();
        }
}


int searchWord()
{
   string input_file,searchWord,line;
   while (1)
    {
        int line_Number=0,found=0;
        cout<<"File: ";
        getline(cin,input_file);
        if (input_file == "exit")
        break;

        cout<<"Search Word: ";
        getline(cin,searchWord);
        ifstream file(input_file.c_str());
        if(file)
        {
            while(getline(file,line))
            {
                line_Number++;
                int position=0;
                for(int i=line.find(searchWord); i<line.length(); i=i+position)
                {
                    position=line.find(searchWord,i);
                    if(position != string::npos)
                    {
                        cout<<endl<<searchWord<<" is at "<<line_Number<<":"<<position<<endl;
                        found=1;
                    }
                    else break;
                }
            }
            file.close();
            if(found==0)
            {
                cout<<endl<<searchWord<<" not in file"<<endl;
            }
        }
        else
        {
            cout<<endl<<input_file<<" not found" <<endl;
        }

}
}
int main ()
{
    string filename;
    cout << "Choose Filename : ";
    cin >> filename;
    cout <<extractWord(filename);
    cout <<searchWord();




}


i only do the 1) and 4) but it the words that i search is not found i believe it didnt store in the memory

file1

The study is published in the journal Nature Climate Change.
It focuses on the Scotia Sea and the Antarctic Peninsula - the places
where the crustaceans are most abundant.

file2

The team's analysis indicates the centre of krill distribution has now
moved to where more favourable conditions are found, tracking
southward towards the Antarctic continent by about 440km, or four
degrees of latitude.
Registered users can post here. Sign in or register to post.