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
Related
This question already has answers here:
Regular expression to stop at first match
(9 answers)
Closed 1 year ago.
I am looking for some help to put a REGEX expression together that will return the contents between ROUTINE and END_ROUTINE
I have tried
ROUTINE S[0-9]{1,5}_[A-Z0-9_]* ([\s\S]*)?(?=END_ROUTINE
)
But it doesnt seem to provide the correct output
ROUTINE S7026_HMI_LB0_SP_MAX
RC: "===================================================================================================$N"
"$N"
"Local HMI Station B Setpoint Commands Max Clamp$N"
"$N"
"===================================================================================================";
N: NOP();
END_ROUTINE
ROUTINE S0100_DIAGNOSTICS
N: JSR(S0101_PLC_Diagnostics,0);
N: JSR(S0102_IO_Diagnostics,0);
END_ROUTINE
It could be enough to add a modifier U to make it ungreedy.
/ROUTINE(.+)END_ROUTINE/sU
(you can test it here: https://regex101.com/r/U2MM6G/1)
Alternatively, you can also do this more specifically:
/ROUTINE(.+?)END_ROUTINE/s
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.
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
2 Questions:
The first question is, I need to input the name of the text file when the program runs so let's say it's called "Banana.txt" , I type it in and I'm missing the last word of the file. This is basically all I used to output from the txt file.
while(File.good()){
cout <<word << " "; //put spaces in between the words
File >> word; }
Not sure how to get the last word.
The second question is how do I store the information from the .txt file into an array that I can use later?
Does it have to be multidimensional? The maximum words in the file are 100..
Eventually I'm going to need to ignore any words < 4 characters.
I actually can't recall if I should use char or string. But iirc char is each individual character whereas string is a collection of characters? Arg. Scratch that, Looking it up in a bit..
Not allowed to use hash_, vectors, maps, stack, or lists so I'm not sure how to go about this problem
Thanks in advance for the help. I tried looking through other threads but I'm not sure if those are the ones I'm looking for... Sorry for the questions ..
You're outputting before you've input.
Try swapping your two lines in the while loop, something like:
while(File.good())
{
File >> word;
cout << word << " "; //put spaces in between the words
}
to put the data into a container, assuming word is of type string, try:
vector<string> v;
and at the bottom of your loop:
v.push_back(word);
For the first one, you need to first read into the word then output it.
The "intuitive" loop of while(File.good()) though is flawed because if you are at end of file but haven't read it yet, the stream will appear good until you try reading the next word.
As a result the read will fail. You could do
while( File.good() )
{
if( File >> word )
{
// process this word
}
}
but simpler is
while( File >> word )
{
// process the word
}
Another thing you can use is istream_iterator and copy, and then you can actually copy them into your sequence. However you are not allowed to use the standard C++ library.
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.
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.