-2

I've designed a program that can encrypt 26 English letters.

Here's how I'm handling the input. I'm reading it from a text file and stores it in a string.

ifstream L;
string str1;
char ch;
L.open("ToBeCoded.txt");
while(iL.get(ch))
    str1.push_back(ch);

However, it's inefficient, if I want to read a different file, I have to change the name in codes to make it work. So is there any dynamic way to do so? Like drag the file or type the address of the file during run time?

By the way, do you have a better way to read txt to string? This one 'seems' slow.

Anton Savin
  • 38,277
  • 8
  • 49
  • 82

3 Answers3

1

you can use istream getline instead

http://www.cplusplus.com/reference/istream/istream/getline/

dariogriffo
  • 3,751
  • 1
  • 15
  • 31
1

I would suggest you use the getline for this kind of problem.
http://www.cplusplus.com/reference/string/string/getline/?kw=getline
getline is an ifstream function that will get the string user efficiently.

If you wanted to get the whole string file, just go to the link that Neil Kirk posted:
Read whole ASCII file into C++ std::string it explains exactly how to do that.

Community
  • 1
  • 1
Chantola
  • 549
  • 1
  • 5
  • 18
0

If you are on Windows you can use DragAcceptFiles and WM_DROPFILES messages. More details here:
http://msdn.microsoft.com/en-us/library/bb776406(VS.85).aspx
http://msdn.microsoft.com/en-us/library/bb774303(VS.85).aspx

Laura Maftei
  • 1,773
  • 1
  • 13
  • 23