My question concerns the use of std::count (or another appropriate function) to count the columns of a space separated file.
I currently use something like this:
std::ifstream inFile("file");
int lines = std::count(std::istreambuf_iterator<char>(inFile),
std::istreambuf_iterator<char>(), '\n');
to count the lines.
Since all the lines are equal (same amount of data), would something like
std::ifstream inFile("file");
int columns = std::count(std::istreambuf_iterator<char>(inFile),
std::istreambuf_iterator<char>('\n'), ' ') + 1;
do what I need?
Thanks
EDIT:
I mean, if in "file" there is data like 1 2 or 1 [many spaces here] 2, would the value of columns anyway be 2 or not?
No, you'll count spaces, not columns. You need to tokenize your line, e.g. by boost::tokenizer
Related
The first question is I have code like this for reading from the text file:
File >> product.projectNo >> product.date
>> product.productNo >> product.productQ
>> product.productTotalPrice;
And I write productNo, productQ and productTotalPrice to the file, separated by - because there can be more than one. Here is an example of the text file:
17 5 17-7051-10- 9-8-11- 720-616-55-
The last one 720-616-55- (productTotalPrice) is a list of prices. I need to read them separately and add them. So: 720+616+55. How can I do that?
The second question is I need to delete any of them, meaning productNo 17-7051-10-, needs to delete 17-. How can I do this?
The first part of your problem is reading each word from your file. This can be done using:
std::string word0, word1;
std::getline(file, word0, ' ');
std::getline(file, word1, ' ');
...
Of course, the above code assumes that there is exactly one ' ' is being used as a separator between words.
The next problem is parsing your prices. This can be done using std::stoi:
size_t index;
int price0 = `std::stoi(word, &index);
word = word.substr(index + 1);
int price1 = `std::stoi(word, &index);
word = word.substr(index + 1);
int price2 = `std::stoi(word);
int totalPrice = price0 + price1 + price2;
This works because std::stoi returns the index of the first character which is not part of the integer. So this would be the - separator between your numbers.
As for the second part of your question: You wouldn't delete 17- as a string. Instead you should simply ignore the 17 before adding it onto the sum or you should remove it from the list of numbers after parsing.
Is there a way to sort words alphabetically that appear on the same line? I know that if I have words on a new line I am able to sort them easily using:
vector<string> file;
string line;
file.clear();
ifstream infile("foo.txt", ios_base::in);
infile.seekg(3);
while (getline(infile, line)){
file.push_back(line);
}
sort(file.begin(), file.end());
ofstream outFile;
outFile.open("foo.txt");
for (const auto &e : file) outFile << e << "\n";
But what if the words were on one line in the file (unseparated by spaces), for example: catapebat(cat ape bat). Is there an easy way to do this in C++? Would I not do the getline bit, since I am only grabbing one line? How would I approach this problem?
Use getline to read a line.
Create an istringstream from the string you just read
Read words from there into a vector<string>
Sort the words in that vector
I would suggest getting a dictionary of words like this and comparing each word with the beginning of the string. If there's a match, you remove that many characters from the beginning of the string and store the word in an array. Repeat this until you get an array that contains all the words and then sort the array alphabetically.
Note: It might be worth going through the entire dictionary and keeping track of each matching word. Once the dictionary has been exhausted, select the longest match as the word before continuing on.
I have never used istringstream before. I have only split lines before with 1 delimiter so I don't know how to use istringstream. I am splitting lines from a file that look like this:
Table, Wanted, 100
Car, For Sale, 5000
I need to split the strings and then create an array of structs. I have a struct set up already I just don't know how to split the strings. My struct is called item and has types: string type, bool sale, double price. For sale, I want it to say 1 if it is for sale and 0 if it is wanted.
Basically, I want to split it so I can create new variables, type, sale, and price and then create a newItem{type, sale, price} in my item struct and go from there. An example of the code would be extremely helpful. Thanks so much.
A simple method to split a string using a delimiter character is to use std::getline.
std::string line = "Table, Wanted, 100"; // Let's say you have read a line from file.
std::istringstream input{line}; // Create an input stream from string.
// Read all characters up until the delimiter ',' on each iteration.
for (std::string token; std::getline(input, token, ',');) {
/* Do something with each token... */
}
Live example
I have several lines looking like this:
4539(random number of spaces)07235001(random number of spaces)Aach(random number of spaces)Trier Saarburg
I want to separate it to 4 columns using C++ or linux. The output I want will look like this:
4539|07235001|Aach|Trier Saasburg
So I want to treat several spaces as the delimiter but not the single one.
(random number of spaces thankfully is always > 1)
Lines do not always consist of 4 columns and the space problem is not always at the last column.
Thanks in advance
You should read each field individually. The last field can be read until a newline
character is received:
std::string column1;
std::string column2;
std::string column3;
std::string column4;
while (input_file >> column1)
{
input_file >> column2;
input_file >> column3;
getline(input_file, column4);
}
Another method is to read the entire line using getline and then fetch out the substring fields using std::string::find and std::string::substr.
You can use awk with regular expressions for this:
echo "4539 07235001 Aach Trier Saarburg" | awk 'BEGIN { FS = "[ ]{2,}" } { OFS = "|" }; {$1=$1; print $0 }'
FS variable is used to set the field separator for each record and may contain any regular expression. OFS is the output equivalent of the FS variable.
I have a file that contains rows and columns of information like:
104857 Big Screen TV 567.95
573823 Blender 45.25
I need to parse this information into three separate items, a string containing the identification number on the left, a string containing the item name, and a double variable containing the price. The information is always found in the same columns, i.e. in the same order.
I am having trouble accomplishing this. Even when not reading from the file and just using a sample string, my attempt just outputs a jumbled mess:
string input = "104857 Big Screen TV 567.95";
string tempone = "";
string temptwo = input.substr(0,1);
tempone += temptwo;
for(int i=1 ; temptwo != " " && i < input.length() ; i++)
{
temptwo = input.substr(j,j);
tempone += temp2;
}
cout << tempone;
I've tried tweaking the above code for quite some time, but no luck, and I can't think of any other way to do it at the moment.
You can find the first space and the last space using std::find_first_of and std::find_last_of . You can use this to better split the string into 3 - first space comes after the first variable and the last space comes before the third variable, everything in between is the second variable.
How about following pseudocode:
string input = "104857 Big Screen TV 567.95";
string[] parsed_output = input.split(" "); // split input string with 'space' as delimiter
// parsed_output[0] = 104857
// parsed_output[1] = Big
// parsed_output[2] = Screen
// parsed_output[3] = TV
// parsed_output[4] = 567.95
int id = stringToInt(parsed_output[0]);
string product = concat(parsed_output[1], parsed_output[2], ... ,parsed_output[length-2]);
double price = stringToDouble(parsed_output[length-1]);
I hope, that's clear.
Well try breaking down the files components:
you know a number always comes first, and we also know a number has no white spaces.
The string following the number CAN have whitespaces, but won't contain any numbers(i would assume)
After this title, you're going to have more numbers(with no whitespaces)
from these components, you can deduce:
grabbing the first number is as simple as reading in using the filestream <<.
getting the string requires you to check until you reach a number, grabbing one character at a time and inserting that into a string. the last number is just like the first, using the filestream <<
This seems like homework so i'll let you put the rest together.
I would try a regular expression, something along these lines:
^([0-9]+)\s+(.+)\s+([0-9]+\.[0-9]+)$
I am not very good at regex syntax, but ([0-9]+) corresponds to a sequence of digits (this is the id), ([0-9]+\.[0-9]+) is the floating point number (price) and (.+) is the string that is separated from the two number by sequences of "space" characters: \s+.
The next step would be to check if you need this to work with prices like ".50" or "10".