Parsing a tab delimited text file - c++

I'm trying to parse a text file from one format to another. The text file is delimited by tabs. The text file I'm using as test has as of now three types of lines. For example, the first line of the text file has an H in the beginning. Since I do not need it now, I ignore it and pass to the next line. Similarly, I have lines that start with S and L. The S lines have five strings, including the first character, which are read by:
while(std:: ifstream readFile >> string1 >> string2 >> string3)
Below this while I have an if statement to check every S at the beginning of each line, which is as follows:
if(string1[string1.length()-1]=='S')
Then I print in the console each value to make sure every line which has an S is being printed in the console, but it's not. It's showing n - 2 where n = total of lines containing S at the beginning. I should also mention that below print statement, I also have this: std::getline(readFile, string1); without this, it shows me n - 5, if I'm not mistaken.
Also, when I only use two parameters while instead of three, all lines are shown with the first two strings, but as soon as I pass three parameters it changes back to n - 2.
What could be the problem that whenever I pass more than two parameters, it does not show me every line that contain S at the beginning(specifically the first two).
Thanks, and sorry if not explicit enough.

Related

how to get rid of blank line in c++?

I'm trying to fill text document, but when I do - I get first line blank.
My text document, I'm reading from, looks like this:
3
first fr random 5
second 9
third shazam 2
I've tried removing first value and so blank line went away. So it correlates, but I need that value.
// My code
ifstream in("U2.txt");
ofstream out("U2.txt");
int n;
in>>n;
char vardaiStart[n][21];
in.read(vardaiStart[0], 21);
out << vardaiStart[0]<< endl;
output looks like this:
*blank*
first fr random
but what I want is:
first fr random
So, in other projects I won't be using old C since now, but for this project I red that [endline] and ignored it like this: in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Your input file looks like:
3[endline]
first fr random 5[endline]
second 9[endline]
third shazam 2[endline]
Note the [endline] I added: they can be '\n', '\r\n' depending on the system that generated the file.
Your first instruction is:
in >> n;
a formatted input read: you are asking to read an integer from the file, so only characters that can form valid integers will be read.
This means that after that instruction the remaining portion of the file will be:
[endline]
first fr random 5[endline]
second 9[endline]
third shazam 2[endline]
The rest of your code reads raw bytes, so it will read the first [endline] as well (and print it).
As other suggested in the comments, you should not be using variable length arrays as they are not part of any C++ standard (but part of the C one). Try to use the C++ standard library instead.

How do I loop through input line by line and also go through each of those lines in C++;

So I'm trying to go through input line by line. Each line in the input is formatted like this:
Words_More words_even more words_What I Need to Go Through_ Random_ Random_Etc.
With a random amount of word clusters (The words separated by '_')
I want, for each line, to be able to ignore all the words until I get to the fourth word cluster which in the example I gave would be: "What I Need To Go Through" and then store those separate words in some data structure that I haven't decided upon yet.
My first thought would be to use
getline(cin, trash, '_');
three times and deal with the data that follows, but then how would I loop line by line until the end of the input?
You basically have two options:
use getline for each line, then parse it
use getline(stream, string) to get a line from your stream, and store it into a string. Then construct an istringstream to parse this again (with the getline you thought of.
get what you need, and then ignore() stuff unill the next newline
You do getline() thing, and then you call ignore() (doc)
to read and discard the rest of the line, so you can start again with the next line.
which one you use is up to you. but the second one has slightly better performance, if you care about that stuff.

Reading a line of a text file from a specific position in C++

I would like to read a text file in C++ in following manner:
Ignore the entire first line as it is simply meant as an introduction.
Only read the following lines from a specific position.
That starting position for reading is a fixed one and remains the same for every line; however, the numbers after that may be of variable length. I need to save all of these numbers from line 2 to line n into an Array.
At the moment I can read a regular 2D Array with getline.
How can I work around these things?
An example for a line I want to read could be:
Person1: 25 988.3 0.0023 7
To set the file to a position, use std::ifstream::seekg().
To set the file to the beginning of a line, you must read and count the line endings. Many text files have variable length text lines.
How can I work around these things?
You can't, unless you can ensure that all of the data lines after the first line are all the same length.
If you can't ensure that, then all you can do is read through all of the preceding lines.
An alternative I have employed in the past is to generate an 'index' of line start positions in a secondary file in binary format (so that I CAN jump directly to the right place in that file), and use that to jump to the right place in the text file. Of course that means that you need to regenerate that index file every time you replace/amend the data file.

index a text file (lines with different size) in c++

I have to extract information from a text file.
In the text file there is a list of strings.
This is an example of a string: AAA101;2015-01-01 00:00:00;0.784
The value after the last ; is a non integer value, which changes from line to line, so every line has different lenght of characters.
I want to map all of these lines into a structured vector as I can access to a specific line anytime I need without scan the whole file again.
I did some research and I found some threads about a command called, which permit me to reach a specific line of a text file but I read it only works if any line has the same characters lenght of the others.
I was thinking about converting all the lines in the file in a proper format in order to be able to map that file as I want but I hope there is a better and quick way
You can try TStringList*. It creates a list of AnsiStrings. Then each AnsiString can be accessed via ->operator [](numberOfTheLine).

C++ Easy way to ignore first word of input

I am writing a program to read a text file line by line, store the line values in a vector, do some processing then write back to a new text file. This is what the text file typically looks like:
As you can see, there are two columns: one for the frame number and another for the time. What I want is only the second column (aka the time). There can be hundreds, if not thousands of lines in the text file. Previously I have been manually deleting the frame number column which i'd rather not do. So my question is: is there an easy way to edit my current code so that when I read the file with getline() it skips the first word and only gets the second? Here is the code that I use to read the text file. Thanks
ifstream sysfile(sys_time_dir);
//Store lines in a vector
vector<string> sys_times;
string textline;
while (getline(sysfile, textline))
{
sys_times.push_back(textline);
}
Since you have two numbers in each line, you can read two numbers and ignore the first number.
vector<double> sys_times;
int first;
double second;
while ( sysfile >> first >> second )
{
sys_times.push_back(second);
}
std::string ignore_me;
while (sysfile >> ignore_me, getline(sysfile, textline)) {
...
This utilizes the comma operator, reading in the first word (here defining "word" as a continuous sequence of non-space characters) of the line, but ignoring the result, then using getline to read the rest of the line.
Note that for the specific data format you describe, I would rather choose what RSahu showed in their answer. My answer is more general to the problem of "skipping the first word and reading the rest of the line".