addition of data only in even position - c++

just wanted to ask something about addition of data.
So i have this .txt file that i want to read in using c++. that one has no problem. I can read that using fstream. now that .txt file contains a data of...
Number of monitored events
Event-1:Weight-1:Event-2:Weight-2:Event-3:Weight-3:Event-4:Weight-4:
Event-5:Weight-5: ....:
those above information will have 4 pairs each row, but delimeted by a :
now my question is . is it possible to add up the values of all the weight? i can't seems to understand how to read only the weight part as it is all separated by the same delimeter.

It is possible using castings. You can use another delimiter for the weights and separate it using that token and cast it to integer and you just have to do is addition.
If u want to know how to break tokens, here is the link:
C++ Reading file Tokens

Related

How to input an arbitrary number of text files in C++?

so I'm working on a coding project for a class, and I understand the basic things I want to accomplish, but one thing that nobody seems to be able to help me with is inputting an unspecified number of text files. The user is prompted to enter the text files they want to compare (overall purpose of my code), separated by spaces, thus allowing them to compare an arbitrary amount of text files (eg. 2, 3, 8, 16, etc). I know that the getline function is helpful here, as well as searching for the number of "." because files can only contain one ".", all within a for loop. After that logic I am utterly lost. Eventually, I'm going to have to open the text files and put them in sets to compare them against every other file once, and output their similarities and differences into yet another text file. Any ideas?
Here is the general process I would try to follow (if I interpreted the prompt correctly)
Get the line of text files using getline
Put that into a stringstream
Open the next file in the stream while there is still information in the stringstream (not at eof)
Store all of that information in a Vector of strings, each new file just appended on after it is read
compare strings in the vector
If you pass the text files on the commandline rather than getting them from a little dialog with the user via stdin life will be easier. Most users will type
compare *
which on Unix type systems is expanded to a list of files. ON DOS you need to match and expand the wild card yourself.
You've got an N squared problem, but the logic is easy, it's just
int mian(int argc, char **argv)
{
int i, j;
for(i=1;i<argc;i++)
for(j=i+1;j>argc;j++)
compare(argv[i], argv[j];
}

Fortran 90: reading a generic string with enclosed some "/" characters

Hy everybody, I've found some problems in reading unformatted character strings in a simple file. When the first / is found, everything is missed after it.
This is the example of the text I would like to read: after the first 18 character blocks that are fixed (from #Mod to Flow[kW]), there is a list of chemical species' names, that are variables (in this case 5) within the program I'm writing.
#Mod ID Mod Name Type C. #Coll MF[kg/s] Pres.[Pa] Pres.[bar] Temp.[K] Temp.[C] Ent[kJ/kg K] Power[kW] RPM[rad/s] Heat Flow[kW] METHANE ETHANE PROPANE NITROGEN H2O
I would like to skip, after some formal checks, the first 18 blocks, then read the chemical species. To do the former, I created a character array with dimension of 18, each with a length of 20.
character(20), dimension(18) :: chapp
Then I would like to associate the 18 blocks to the character array
read(1,*) (chapp(i),i=1,18)
...but this is the result: from chapp(1) to chapp(7) are saved the right first 7 strings, but this is chapp(8)
chapp(8) = 'MF[kg '
and from here on, everything is leaved blank!
How could I overcome this reading problem?
The problem is due to your using list-directed input (the * as the format). List-directed input is useful for quick and dirty input, but it has its limitations and quirks.
You stumbled across a quirk: A slash (/) in the input terminates assignment of values to the input list for the READ statement. This is exactly the behavior that you described above.
This is not choice of the compiler writer, but is mandated by all relevant Fortran standards.
The solution is to use formatted input. There are several options for this:
If you know that your labels will always be in the same columns, you can use a format string like '(1X,A4,2X,A2,1X,A3,2X)' (this is not complete) to read in the individual labels. This is error-prone, and is also bad if the program that writes out the data changes format for some reason or other, or if the labes are edited by hand.
If you can control the program that writes the label, you can use tab characters to separate the individual labels (and also, later, the labels). Read in the whole line, split it into tab-separated substrings using INDEX and read in the individual fields using an (A) format. Don't use list-directed format, or you will get hit by the / quirk mentioned above. This has the advantage that your labels can also include spaces, and that the data can be imported from/to Excel rather easily. This is what I usually do in such cases.
Otherwise, you can read in the whole line and split on multiple spaces. A bit more complicated than splitting on single tab characters, but it may be the best option if you cannot control the data source. You cannot have labels containing spaces then.

How do I get specific lines from a text file in C++?

I need some help with C++
I am trying to create a program which contains excersises to practice the different German cases.
Hard-coding all questions and respective answers seems like an awful lot of work, and super inefficient.
What I want my program to do, is: grab a random line from file X, and grab the same line number from file Y. (This seems like the easiest way to get both questions and answers from external files.) To me, it seems the most logical to get a random number, and use that as a line number. But, that's about how far I got...
I know basic C++, but am very eager to learn.
Can anyone please explain to me how to pull this off, including all necessary command?
First, I would recommend that you store questions and answers in the same text file, probably by alternating between a question line and then an answer line. This will make correcting mistakes, adding/removing questions, and general maintenance of your data easier.
But if you want to keep them in separate files, the following code snippet will read your text file in and store the questions in an array (an stl vector) which you can then index or iterate any way you'd like:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::ifstream file("questions.txt");
std::string line;
std::vector<std::string> questions;
while (std::getline(file, line))
{
questions.push_back( line );
}
// Now do something interesting with your questions. You can index them
// like this: questions[5], or questions[random_index]
}
There are two ways of doing this:
If you are planning on getting question/answer pairs you would be best to just read the who file line by line and store all the lines. Then you just look it up in the array.
If for some reason you only want to get one line at a time you'll have to read lines and count until you've gotten to the line you want.
you may have a keyword for each line, like an id.
that id can be paired to both questions, and answers if you have multiple files. or just pair the question, with the answer same order, or even same file.
You are constructing a database.
You should use a database.
The problem is that the question and answers are variable length records, which make positioning difficult. If all the records were the same length, you could position to a random record much faster.
In order to find a text line, you will need to read past all the other newlines (since they are not in the same column in every line). This is fine if you only need to search once, but very slow to search many times. Now comes the reason for the database.
To make finding the questions and answers faster, create an index file or table. (Starting to smell like a database). The index file will contain records of the form [question #, file position] where file position is the position in the question's file that the question starts on.
You would load this file into memory and use it to index into the "questions" file. By storing the index contents into a file, you won't have to construct it from scratch each time your program starts; only when the question's file changes.

Taking substring from string between " sign // C++ Builder

I've got problem with following task. I need to load output txt data from other program to my one and search it for following string:
"zhi": 97.92716217041016,
and especially numerical value (97.92...). The "," sign is separator between other exported values.
I was trying to deal with that in c++ builder following way:
1. read file
2. load lines as strings
3. find position of zhi
4. add 6 to position - this will point on numbers
5. delete everything before new pointer
6. delete everything after 15 char
I know there have to be some easier way but I'm beginner with c++.
Can someone guide me what function I can use to find numbers between "zhi": and , ?
Use std::getline to read file as std::string types.
Use string::find to get the position of the text after "zhi":.
Use string::find to get the position of the ','.
Use string::substr to get a copy of the string between the two positions.
Remember, modifying files in the middle is difficult, especially if you have to delete or insert more characters.
The traditional method of modifying files is:
1. Copy all unchanged content from original file to new file.
2. Write changed text to new file.
3. Write remaining unchanged text to new file.
4. Close all files.

Reading a stream from the right in c++

I'm a bit of a noob to c++. I understand that when one reads from a stream, you read from the left. e.g If you had a file with the line:
I'm playing around with streams
When you read the file, the first string you'll retrieve from the file is I'm
Is it possible to make the first string you retrieve to be streams
Reading a stream from the right, basically.
Note: This is assuming that you don't read entire lines per time
Streams are not read from left to right, they're read from first to last. They are supposed to model things where this is the logical way to read them and in some cases (e.g. keyboard input) the only sensible way to read them. For a stream that is entirely known at the start (e.g. a file) you could, if you really really wanted, painstakingly seek to the last element and then read them in one-by-one seeking as you go. This would be slow and ugly.
Instead, I recommend you read from first to last in the usual fashion and then manipulate the data once you've got it.
What you want to do is to read the words from right to left, not the whole stream. Reading the stream from right to left would result in smaerts not in streams and I am sure you can not do that out of the box. What I suggest is that you read the words in a vector and then reverse it. Alternatively, reverse the whole string before reading from it and then reverse each word after reading it.
No, there's no support for this in the streams library. A file is generally arranged with the document data going from left to right, top to bottom. Given variable length lines, you can't generally know where the line terminates unless you scan over all the data in the line.
For this requirement, you're best off reading an entire line into a string with getline, then you have many options such as:
writing your own string scanner to find each word in turn (simple enough, fast)
creating a istringstream from the reversed line text, then looping to stream each word in turn - reversing it back before processing (convenient for stream features - whitespace skipping, conversions, status etc.)
tokenising the line into an array or similar, and iterating that in reverse order (simple conceptually, but memory hungry)