Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
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.
you can use istream getline instead
http://www.cplusplus.com/reference/istream/istream/getline/
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.
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
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am writting a code in C++. And I have a string like this
std::string1 = "one
two
end"
this string has got a 2 newline. I want to save this string in the one line of file. for example:
std::string2 = "one\ntwo\nend"
End after I read from file this line and I change again string2 like a string1.
Do you know how I can do this??
My problem is to call the function to create a random strings and these strings maybe have got a newlines but I want to save and read from file the only one line. And this reason I don't know the string because it is random.
You can use raw string literals for this:
std::string1 myString = R"__(one
two
three)__";
The two underscores __ work as delimiters which can be chosen freely. Use the same in the beginning and the end of the raw string literal.
Also notice that anything in between will become part of the string, including all whitespace characters.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We all know that we have ifstream and ofstream classes with their own functionality: reading, writing, line by line reading etc.
ifstream input_file("test.in") ;
ofstream output_file;
output_file.open("test.out");
So, let assume now we want to extend reading/writing functional via creating some class (lets call it BackUp) and somehow make ifstream and ofstream to use BackUp's reading/writing instead (only). As far as I understood this principle is called acquaintance?
The main difference should be that when opening a file a copy of it should be created somewhere. Then we work with this starting file like usually, we overwrite it by our results and if the procedure was successful only then the temporary copy is deleted.
Yes, I know, that it's probadly better just to write and use a common function, but this is not my goal.
I have also made a schemes to understand the logic:
Before:
After:
Goal
I want the target file to be overwritten if the entire operation is successful, so if the program crashes, if one excidently turns off the PC etc. the data from the original file would be saved atleast somewhere.
Question itself
I need to grasp the idea itself how this can be done in general case but It would be nice if one would use this particular this example to explain.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i'm trying to understand this library that stanford uses for their CS course but i can't just use this feature in my CS course back in my home. How do i emulate this feature? i was wondering is this a getline(cin, integer)? or is this something to do with fstream? The reason being, i'm trying to go through the lecture series at UDEMY.
Here is the definition.
int getInteger(string prompt = "");
Reads a complete line from cin and scans it as an integer. If the scan
succeeds, the integer value is returned. If the argument is not a
legal integer or if extraneous characters (other than whitespace)
appear in the string, the user is given a chance to reenter the value.
If supplied, the optional prompt string is printed before reading the
value.
Usage:
int n = getInteger(prompt);
You can download simpio.h here: http://www.eecs.wsu.edu/~cs150/prog/libs.htm
If you can't or don't want to use the library, you could study its source code.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string on Linux standard terminal input. Its maximal length is 10^200 (as said in program specification). I have to count, how many "3" characters are inside it (occur in this string). I couldn't do that by for loop, because there is no so big variable type, which can be used as iterator. Is there any way to analyze so big strings?
Is there any way to analyze so big strings?
Not in this universe there is not. Such an entity cannot exist in this universe1 and that which does not exist, cannot be analyzed.
1 Current estimates of this universe's total number of particles particles are in the region of 1080.
As from your comment
The data source is standard terminal input.
Then you'll need a lot of monkeys to type this in.
Though you don't need to read in what's typed at once into a big string, but you can simply analyze char by char as typed. The std::istream::get(char_type& ch) method is suitable for doing so.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want the program to take user input to fill a 2D array of ints and I'm not sure what the easiest format to have the user enter the data would be (easy in terms of programming).
For example in Java I could use .split() to separate integers by spaces or commas etc., but since a string can't easily be broken up in C++, this is a problem.
I don't want to prompt the user each time for each element such as:
enter integer for location 0, 0
enter integer for location 0,10
enter integer for location 2, 0
Here is a stackoverflow answer that talks about how to tokenize a string in C++
How do I tokenize a string in C++?
I haven't tried boost but strtok is pretty easy to use but may have some multithreading issues.