This question already has answers here:
read data from text file
(5 answers)
Closed 8 years ago.
I have a text file with the following layout:
step=fixed step start=100 step=1
32
112
step=fixed step start =211 step=1
11
34
and so on
I need to extract the numbers 100 and 211 respectively i.e. the start values as integers in my code and carry out some operations.
getchar and use space as delimiter. Or use scanf and formatted string. (but it still needs some spaces between words and numbers.)
Example:
scanf("%s %s %s %s %d",variables..);
scanf("%d",var);
scanf("%d",var1);
since you know format its quite easy. If it is from file use fscanf.
Related
This question already has an answer here:
Notepad++ : Insert blank new line after match of some string
(1 answer)
Closed 4 years ago.
I have to join lines to make questions (having serial no. in order 1,2,3....) in one line and all options as shown below in another single line ? Example :
1.For his alleged
involvements in
espio-nage
(1) abc
(2) saf
(3) asf
(4) aqg
Output should be:
1. For his alleged involvement in espio-nage...
(1)abc (2)saf (3)asf (4)aqg
Note:I just want to make it compact to read in kindle reader so that maximum space will get utilised
I think
\r?\n(?!\(1|\d)
(And replace with empty string) will do what you want
Explanation: Looks for a newline that's not followed by either (1 or a single digit.
If you ever have more than 9 options that will cause problems as it'll split before (10 (11 etc. - in which case \r?\n(?!\(1\)|\d) should do it
Select text/elements and press CTRL+J
This question already has answers here:
Regular expression to count number of commas in a string
(9 answers)
Closed 4 years ago.
I need to check for users that are putting their id in the wrong input box.
They might enter the id as 123-456-789-012 or 123456789012 or some variation so I can't just check for digits. The length of the id varies slightly per user, but is always more than 10 digits.
Valid input is a mix of characters and 0-10 digits.
I've seen a lot of solutions for plain digits, but not mixed text. I tried variations of
(\D*\d){0,10}
but that didn't work.
You want "0-n non-digits" then "1-10 lots of a digit-and-any-non-digits":
^\D*(\d\D*){1,10}$
This question already has answers here:
Skipping expected characters like scanf() with cin
(5 answers)
Closed 7 years ago.
Lets say i am given input
{ [1, 1], [2,10] , [-10, 20] }
I have to retrive number and check if the input is correct ( if "," isnt missing or if { and } are at the start/end )
In c, i could just use
scanf(" %c%d, %d%c",&zatvorka,&jedna,&dva,&zatvorka_dva);
In a while loop to scan the input , but how could i do it in c++? As far as i know , to cin is used to retrive data but it has no pattern like scanf() which would make it hard to retrieve data in such pattern and check if it is correct. How could i scan input like that ( for example) in c++?
The scanf function is available in C++ as well. You need to include the cstdio header file to access it.
More info:
http://www.cplusplus.com/reference/cstdio/scanf/
http://en.cppreference.com/w/cpp/io/c/fscanf
This question already has answers here:
What is the easiest way to parse an INI File in C++? [closed]
(13 answers)
Closed 8 years ago.
I am a c++ beginner and I have a (probably simple) question. So far i have defined several variables:
double Start = 0;
double End = 1;
int Steps = 100;
I want to change these values to a value that I have stated in a text file "paramaters.txt":
x_start = 0
x_end = 10
num_steps = 100
So my c++ needs to read the file and change the double End from 1 to 10. Reading the file can be done with this function:
std::ifstream file("parameters.txt")
I want to define a variable of type std::string, called label. Then i want to read the ’label’ from the file. Using a group of ’if (label == ”value”)’ statements to determine if I'm dealing with the start, end of the number of steps. Within the if-statement, the value of 10 would stand for the end for example.
I hope that someone can help me.
Regards,
It seems you want to read a file for some values. You can do that by reading file line by line and then parsing each line.
For e.g in your case you would separate the line into two words with demiliter as "=".
But often the best way to read file for some values is to use some libraries. Like you can use boost::program options.
This question already has an answer here:
c++ how to build a 2D matrix of strings from a .dat file? 5 columns x rows
(1 answer)
Closed 8 years ago.
Say that I have a .txt file with 5000 words one above the other. and I want to convert that list contained in the txt file into this form:
{"word1, "word2", "word3" ....."word5000"}
So that way I can use it as an array for C++.
Is there a way to do that? Any method is welcome , as long as it is an automated process. Thanks for reading!
Use a vector instead of an array. Using it, the task looks something like this:
std::ifstream in("words.txt");
std::vector<std::string> words{ std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>() };
Now the word that was on the first line of the file is in words[0], the second in words[1], and so on.
Note: if a line contains more than one word, this will read them as separate words. If you want the entire contents of a line treated as a single word, see the answers to a previous question specifically about how to do that.