Author Topic: Go trough text file  (Read 2513 times)

0 Members and 1 Guest are viewing this topic.

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Go trough text file
« on: 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?
Vae Victis - suffering to the conquered

Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Re: Go trough text file
« Reply #1 on: 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.
« Last Edit: November 30, 2013, 11:04:52 pm by Matriplex »
\x64\x6F\x75\x65\x76\x65\x6E\x00

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Go trough text file
« Reply #2 on: 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/
« Last Edit: November 30, 2013, 08:15:02 pm by ande »
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: Go trough text file
« Reply #3 on: 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:


Code: [Select]
#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:
« Last Edit: November 30, 2013, 10:13:23 pm by Code.Illusionist »
Vae Victis - suffering to the conquered

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Go trough text file
« Reply #4 on: 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:
Code: [Select]
#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:

Code: [Select]
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:
Code: [Select]
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...
« Last Edit: December 01, 2013, 04:40:56 am by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: Go trough text file
« Reply #5 on: 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)
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Go trough text file
« Reply #6 on: December 01, 2013, 07:26:31 pm »

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: Go trough text file
« Reply #7 on: December 02, 2013, 04:04:17 am »


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;
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Go trough text file
« Reply #8 on: 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?  ???
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: Go trough text file
« Reply #9 on: 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
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Go trough text file
« Reply #10 on: 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.
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]