C++ Code reading an external .cpp file?

I want to be able to make this code read those two lists from an external .txt file from my desktop for example. This will make it so I can read larger amount of words instead of having to paste them all in this script.

So instead of
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    std::istringstream tokens_in(
    R"(word4
word5
word6
word1
    )"
    );
     
    std::istringstream dict_in(
    R"(word1
    word2
    word3
    )"
    );


I want it to find those lists externally.

How could this be done, if possible even?

Posted in beginners section too, wasn't sure what was a better place. Sorry for the spam.
Last edited on
Like instead of having

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    std::istringstream tokens_in(
    R"(word4
word5
word6
word1
    )"
    );
     
    std::istringstream dict_in(
    R"(word1
    word2
    word3
    )"
    );


I could have an input.cpp, dictionary.cpp, and an output.cpp where all matched up names are printed out. Could this be done? I would really appreciate some help!
I can't find anything anywhere on how to go about doing this. I have an idea how to do this in JAVA but that would be entirely too slow. C++ is the way to go. If anyone could help me out please do so, it would be much appreciated.
The istringstream is an istream.
The ifstream is an istream.
Just replace one with the other.

http://www.cplusplus.com/doc/tutorial/files/

PS. Do not use global variables.
I'm new to C++, brand new. I understand I should have read some books and took some classes but I am in a situation where I need a project done quickly with the very little knowledge I know. I can follow and understand the tutorials fine but when it goes to putting it into my script I don't know how to re-write my current script and how to deal with the errors.

Do you understand what I'm saying though about having the script I have now read an external .cpp file with a list of words in it then having them match up words in ANOTHER .cpp file and discard of any that didn't match up by not printing them to an output.cpp.

That's what I'm aiming for.
I pretty much need help rebuilding my whole current code and making this possible. I tried and I can't seem to get it to want to write the words it matched up in the 'dictionary' from list1 into an outpost file. I don't know how to make it know to do that.
A list of words is data, not cpp.

Change your main:
1
2
3
4
5
6
7
8
9
10
11
#include <fstream>
int main()
{
  std::ifstream tokens_in( "input.txt" );
  std::ifstream dict_in( "dictionary.txt" );

  auto filtered = filter(tokens_in, read_dictionary(dict_in));
     
  for (auto& token : filtered)
    std::cout << token << '\n';
}

Test with files:
input.txt
word4
word5
word6
word1

dictionary.txt
word1
word2
word3
Last edited on
I'll test it out here after this reply.

Will it be able to find it if it's on my desktop or do I need to put it in the same location as the script/.exe?
C:\Users\nix\Desktop\test1.cpp|7|fatal error: ifstream: No such file or directory|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
is it #include <iostream> instead of #include <ifstream>? either way I get errors. :x
Sorry, the header is <fstream>. (You could have checked the reference documentation on this site; search with std::ifstream.)

Each process has a current working directory (cwd). Filenames with non-absolute paths are relative to cwd. I have no idea how Windows GUI sets cwd for a process.
okay I'll mess around with it some more, I really don't know much of what you just said but I'll see where I can get with it. Thanks.
Okay I got that working, it's reading it from .txt files now. The only problem is it's printing them out in CMD.

I need a way to make it print out the matching words in an output.txt.

How could I do this with the current code:

1
2
3
4
5
6
7
8
9
10
int main()
{
  std::ifstream tokens_in( "input.txt" );
  std::ifstream dict_in( "dictionary.txt" );

  auto filtered = filter(tokens_in, read_dictionary(dict_in));
     
  for (auto& token : filtered)
    std::cout << token << '\n';
}
checking out this tutorial:

http://www.cplusplus.com/doc/tutorial/files/

and cannot seem to find a way to make the current code I have print out the matches into an output.txt
I added

 
  std::ofstream out( "output.txt" );


and have it creating an outpost.txt file now but I can't seem to make it actually print the outputted text into the file. After I'm done running/building it a few words I put into input.txt and dictionary.txt were printed out into CMD but not into the output.txt

What am I doing wrong?
Both ofstream and cout are ostreams and therefore interchangeable, just like the istream subtypes are for input.
Is that why it's not printing the text out into output.txt?

I am really confused to why it's not. The only alternative I can think of is find a way for it to print out the output from CMD into a .txt from there.

But I would rather just do it the right way.

Why is my output.txt file remaining blank but CMD has a list of printed out words? Could you fix my code maybe?
I'll start a new thread regarding my current issue since I no longer need it to read from external .txts. I figured that out thanks to everyone who helped.
Topic archived. No new replies allowed.