EvilZone

Programming and Scripting => C - C++ => : Code.Illusionist November 30, 2013, 07:38:18 PM

: Go trough text file
: Code.Illusionist November 30, 2013, 07:38:18 PM
I created 4 string arrays each with 200 strings. Soo, I have text file with this type of form:


Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code

Soo, I want two loops to go trough all 200 products and set each type in different string. For example, product name to be stored at string products. And soo on. Problem is I don't know how to read string char by char, so I can stop when I reach  | sign and store the value in string. Anyone have idea how to do so?
: Re: Go trough text file
: Matriplex November 30, 2013, 08:01:18 PM
What language would you prefer this to be in?
I'm assuming python.


Ah shit, didn't see it was in C++/C area. My bad.
: Re: Go trough text file
: ande November 30, 2013, 08:14:45 PM
What language would you prefer this to be in?
I'm assuming python.


Yes obviously he wants it in Python when he has posted in C/++.

@OP: Check out strtok(), it splits the input to tokens: char * strtok ( char * str, const char * delimiters ); You would still have to make a simple line by line read tho. I don't know exactly at what level you are so get back to me if you don't know how to do that either.


EDIT: http://www.cplusplus.com/reference/cstring/strtok/
: Re: Go trough text file
: Code.Illusionist November 30, 2013, 09:17:19 PM
I will need this for C++, not C. I see that it's possible to break character array into tokens. But problem is I have string object in the first place. Because our proffesor want we do this with strings, not character arrays.


EDIT: Atm, I managed to read row by row:


:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
    string Ime[200],Kolicina[200],Cena[200],Sifra[200],red[200];
    ifstream citaj("artikli.txt");
int i = 0;
    while (getline(citaj,red[i])) {
    cout << red[i] << endl;
    i++;
    }
    cin.get();
    return 0;
}
BUT, in the beginning of text file, when I read it there is some sign I don't have in document, like this:
(http://www.dodaj.rs/f/40/KO/4bXgFGFl/2013-11-30221225.jpg)
: Re: Go trough text file
: ArkPhaze December 01, 2013, 04:30:11 AM
Why didn't you use a class? Managing products across different arrays like that is not good at all. What's worse is that there are better containers to be used too for expandable data in C++. This array method you're using would be more of a C-like strategy. You are also not closing the stream, or checking if it is open...

Ex:
:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>

class Product
{
public:
std::string Name;
std::string Quantity;
std::string Prize;
std::string Code;
};

std::ostream& operator<<(std::ostream& o, const Product& obj)
{
o << "Name: " << obj.Name << '\n'
<< "Quantity: " << obj.Quantity << '\n'
<< "Prize: " << obj.Prize << '\n'
<< "Code: " << obj.Code << std::endl;
return o;
}

int main()
{
std::vector<Product> products;
std::ifstream fs("data.txt");

if (!fs.is_open()) return 1;
std::string ln;
while (getline(fs,ln))
{
if (std::count(ln.begin(), ln.end(), '|') == 3)
{
Product p;
size_t i = 0;
size_t index(ln.find('|', 0));
p.Name = ln.substr(i, index);

i = index + 1;
index = ln.find('|', i);
p.Quantity = ln.substr(i, index - i);

i = index + 1;
index = ln.find('|', i);
p.Prize = ln.substr(i, index - i);

i = index + 1;
index = ln.find('|', i);
p.Code = ln.substr(i, index - i);
products.push_back(p);
}
}
fs.close();
std::for_each(products.begin(), products.end(), [](Product p) { std::cout << p << std::endl; });
}

Quick and dirty implementation. Tested on a file that looks like this:

:
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code

Output:
:
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code

Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code

What would be even better is to use a std::map, and use a product ID (if there is one, I'm assuming it may be "Code"), as the link to the mapped value. This way, it would be much easier to find a Product once all the data is retrieved from the file. IMHO, even if you just choose a vector, it's much better than these arrays you're using...
: Re: Go trough text file
: bluechill December 01, 2013, 05:06:28 PM
Yes obviously he wants it in Python when he has posted in C/++.

@OP: Check out strtok(), it splits the input to tokens: char * strtok ( char * str, const char * delimiters ); You would still have to make a simple line by line read tho. I don't know exactly at what level you are so get back to me if you don't know how to do that either.


EDIT: http://www.cplusplus.com/reference/cstring/strtok/

Uh no.  Don't use C.  Use C++, just use string's find feature along with classes for each line, or a vector for each line.  So you use find and substr and put the parts in a vector and the. Put that vector into a vector of the lines (where a line is just a vector for the parts)
: Re: Go trough text file
: Kulverstukas December 01, 2013, 07:26:31 PM
Vector in a vector in a vector derp
(http://cdn.memegenerator.net/instances/500x/32144621.jpg)
: Re: Go trough text file
: bluechill December 02, 2013, 04:04:17 AM
(http://cdn.memegenerator.net/instances/500x/32144621.jpg)

typedef vector<char> string;
typedef string line;
typedef vector<line> file;
typedef vector<file> hdd;
typedef vector<hdd> computer;
typedef vector<computer> network;
typedef vector<network> brightnet;
typedef vector<network> darknet;
typedef vector<pair<brightnet, darknet>> internet;
: Re: Go trough text file
: ArkPhaze December 10, 2013, 03:54:50 AM
Uh no.  Don't use C.  Use C++, just use string's find feature along with classes for each line, or a vector for each line.  So you use find and substr and put the parts in a vector and the. Put that vector into a vector of the lines (where a line is just a vector for the parts)

Why continue to keep it in a vector though when you could encapsulate it within a user defined type that more appropriately handles the data each line is supposed to represent?  ???
: Re: Go trough text file
: bluechill December 11, 2013, 04:35:48 AM
Why continue to keep it in a vector though when you could encapsulate it within a user defined type that more appropriately handles the data each line is supposed to represent?  ???

That might too advanced for some :p
: Re: Go trough text file
: ArkPhaze December 16, 2013, 06:39:18 AM
That might too advanced for some :p

I guess. It's something I sometimes don't consider, even though it may be more appropriate for the task. But it would mean that the original programmer would know how to manage such an implementation, or at least understand it, otherwise there's no point.