Dogtree 23 Posting Whiz in Training

>> I mean like the PYTHON , you can see a nice topic ( Starting Python) in this site , i mean like that .
A good tutorial is hard to write. It takes a lot of time, and the people who know enough to write a good tutorial probably don't have enough time to do it. You should look for existing tutorials instead of waiting for something new to be written.

Dogtree 23 Posting Whiz in Training

Yes, you have posted a good class but can u give main() function code also, how to call the class and which member function. Means how to read the file (.CSV) and pass in the object of class and get the parsed content as output.

In that case, you need to ease off on your project and do something closer to your current skill level. If you can't figure out how to use such a simple class, you're not even close to being ready to parse CSV files and write MFC apps.

Dogtree 23 Posting Whiz in Training

>> can u give send me some code how to seperate the contents from commas.
That nice convenient class I posted that does exactly what you asked for must not have been what you really wanted. Why are you using char arrays anyway? How archaic!

Dogtree 23 Posting Whiz in Training

>> Extent of CSV format is like
Well that's simple enough. You don't need to do any tricky parsing if there won't be commas embedded in a field.

#include <sstream>
#include <string>
#include <vector>

using namespace std;

class ParseCSV {
public:
  typedef vector<string>::const_iterator iterator;

  ParseCSV() {}
  ParseCSV(const string& record) { assign(record); }

  void assign(const string& record);

  iterator begin() const { return split.begin(); }
  iterator end() const { return split.end(); }
private:
  string trim(const string& field);

  vector<string> split;
};

string ParseCSV::trim(const string& field)
{
  string::size_type start = field.find_first_not_of(" \t\v");

  return field.substr(start, string::npos);
}

void ParseCSV::assign(const string& record)
{
  stringstream recStream(record);
  string field;

  split.clear();

  while (getline(recStream, field, ','))
    split.push_back(trim(field));
}
Dogtree 23 Posting Whiz in Training

What's the extent of your CSV format? Is it just a bunch of fields separated by commas, or can the fields contain commas as well? A full CSV format means you need to do some tricky quotation parsing if a field needs an embedded comma.

Dogtree 23 Posting Whiz in Training

> How very true, but STL is chapter 10. I'm at chapter 6
What are you talking about? The STL has nothing to do with redirecting the output of a program to a file from the shell. If you were talking about the vector suggestion then you really need to work on formatting your quotes so that it's obvious which quote goes with which question.

> can you tell me in wich type of programs you use STL regularly
All of them.

> Understood, only, what do you mean with the expression "from the shell"?
Shell, terminal, command line, that black thingy with white text that you type and it does stuff.

Dogtree 23 Posting Whiz in Training

> Would it be something like this then?
Something like that, yea. But wouldn't it be faster to try it out for yourself? ;)

> Also, do I need to use forward or backward slashes?
Either will work from the shell. The only issue with back slashes is in C++ where they are the escape for special characters in string literals.

Dogtree 23 Posting Whiz in Training

> Do I have to enter the same path wich it has to follow as in the other ones?
Yes. For simplicity, my example assumed that both the executable program and the file were local to the root directory C:\, but unless your current working directory is the same as the executable and the output file, you need to specify an absolute path.

Dogtree 23 Posting Whiz in Training

> In other words, it means that *pp has those properties wich are mentioned in the structure right
Yea, basically.

> And can only a pointer have that ability to be written in that place
No, you're just declaring a variable. It's the same thing as this:

struct row
{
      int *col, size;
};

row *pp;

> What is the difference then when writing a structure, like this
pp is local to main in the first example, but has file scope and external linkage in the second.

> Could you tell me how I can make it redirect towards the output of a file?
If you run the program from the command line you can do this:

C:\> prog > file
C:\> type file

Or you can redirect to a file from directly in within the program. First by using the file stream instead of cout:

#include <fstream>

int main()
{
  std::ofstream out("file");

  out << "output\n";
}

Or by re-assigning the stream buffer of the file stream to cout:

#include <fstream>

int main()
{
  std::ofstream out("file");
  std::streambuf *saved_buff = std::cout.rdbuf();

  std::cout.rdbuf(out.rdbuf());

  std::cout << "output\n";

  std::cout.rdbuf(saved_buf);
}

> Well, I guess it depends on what you call simple right
Yea. :mrgreen: Simple for me may not be simple for you, and vice versa depending on the problem. Nobody knows everything. :)

Dogtree 23 Posting Whiz in Training

1) No, a structure's members are public by default, so anyone can access them. Dave put the structure in main to limit its scope to main. Consider this:

int main()
{
  struct test {};
  test t; // Okay, test visible
}

int foo()
{
  test t; // Error! test not visible outside main
}

2) That's a declaration of pp, a pointer to struct row. You can split it up by the type and the identifier, and compare it with an integer to see the similarities:

Type                            Identifier
------------------------------  ----------
struct row {int *col, size;} *  p;
int                             x;

The only difference is that int already exists while struct row is being defined.

3) You can cut and paste from the output medium, such as the console, or redirect the output to a file. It really depends on your system and compiler.

4) Dave's solution makes use of simple objects. Each row object has an array and also stores the size of that array. In all but the most trivial of programs, this solution has the benefit of simplicity.

Dogtree 23 Posting Whiz in Training

>Moron!
I'm not the one who came to a programming help forum asking other programmers to work for free.

>I came to this place for help and to learn obviously noone wants to help.
We don't offer the kind of help you want here. There's nothing I can do about that. So instead of calling me names because you don't like the answer you got, take my suggestion and go hire someone. You'll probably get a better program out of it. If you want to continue hanging around here insulting us, I'll consider you a troll and recommend that you be banned.

Have a nice day.

Dogtree 23 Posting Whiz in Training

This is a forum for programming help, not for hiring programmers. If you want someone to write this for you then you can get someone cheap at www.rentacoder.com. It's pretty clear that you have no intention of learning to program to get this working, so your thread is off-topic.

Dogtree 23 Posting Whiz in Training

PM me with the threads you're talking about and I'll see what I can do.

Dogtree 23 Posting Whiz in Training

>> My thought is that if they didn't want me to close it, they would have made the close function private or protected.
So you always use the open function too even though the constructor provides that functionality?

std::fstream file;

file.open(filename);

// ...

file.close();

If they didn't want you to open the file then they wouldn't have made it public. Since you're adding redundant code for no reason, you might as well make it explicitly symmetrical. ;)

Yes, I'm mocking you. No, it's not personal. No, it's not a flame or an insult, so don't take it as such.

Dogtree 23 Posting Whiz in Training

I agree, but only if the fstream object doesn't go out of scope before opening another. There's no need to close the stream if this is your code:

int main(int argc, char **argv)
{
  for (int i = 1; i < argc; i++) {
    fstream file(argv[i]);

    if (!file) continue;

    // Process the file
  }
}

There's no way that you can have more than one stream open at the same time because the destructor is called after each iteration of the loop. The destructor will flush unwritten output and close the stream properly. The only reason you would have for calling close explicitly is if you reuse the fstream object for another file, have enough fstream objects to reach an implementation limit of open files, or the fstream object will be unused, but still in scope for a while after processing the file.

But if you like the extra typing and redundant code, who am I to stop you? Some people prefer to close their file streams explicitly as a matter of style.

Dogtree 23 Posting Whiz in Training

>> while ( ! file.eof() )
You shouldn't use <stream>.eof() as a loop condition. Because the eofbit is only set after a request for input has failed, you'll increment the counter once more than necessary if the last line contains the search string. A better way to read input is to use the return value of your input function:

while (getline(file, buffer)) {
  if (buffer.find(string1) != string::npos)
    ++counter;
}

>> if ( buffer.find( string1 ) != -1)
Okay, this is a subtle error. string::npos is defined as (size_t)-1, which is not the same as -1. Your test may or may not work as written, so you should use string::npos directly for tests such as this one.

>> file.close();
There's no need to close an fstream explicitly if the destructor is going to be called. Just FYI.

Dogtree 23 Posting Whiz in Training

>> Theres not much diff between the 2.
Only if you limit yourself to a tiny subset of C++ and use poor style in C. When used correctly and to their fullest, there's a huge difference between C and C++.

>> plus what u use in c can be easily implemented in c++
Most of what you write in C++ can be more easily implemented in Java, or C#, or Perl, or Python, or just about any other language suited to general purpose use. However, none of those languages, including C++, can be used for some systems. For example, there are embedded architectures that lack the resources for a C++ compiler and have no choice but to rely on C or end up using assembly. Such programs are not easily implemented in C++.

>> and c is dated ... so might as well keep c with c++...
That's not a logical argument, dude. If C is dated then it should go in the Legacy Languages forum, not lumped together with C++. And what about C++? It's not any less dated, approaching 30 years. But dated doesn't mean useless or unused. Look at FORTRAN, or COBOL. They're "dead" languages, but still used a lot if you know where to look. The C community is thriving, almost as much as the C++ community. If you don't believe that then look at the activity on the comp.lang.c, comp.lang.c.moderated, and comp.std.c newsgroups. They're active post for post with comp.lang.c++, …

Dogtree 23 Posting Whiz in Training

>> can anyone tell me what are the real time applications of C.?
Take a look at any application you use. Chances are good that it's written in C, written in a language implemented in C, or written in a language derived from C.

Dogtree 23 Posting Whiz in Training

There's really no way I could have hinted how to define the operators and call sort that would be useful without giving code. Don't worry, if possible I try not to solve other people's problems. ;) I also didn't give it to you on a silver platter. There are multiple ways to do something, some better than others, and you would still have to piece it all together even if you did use the exact code I gave.

>> Why do people like you, Narue, Dave,... make it all look so simple
We make it look simple because we worked really hard to learn it, just like you're doing now.

Dogtree 23 Posting Whiz in Training

>> Define a class "point" with two datamembers, x and y.

struct point {
  int x, y;
};

>> Use the STL-container vector to stack a row of point-objects.

#include <vector>

std::vector<point> points;

>> Enter them by using the keyboard by two(pairs), x and y.
>> Use a non numeric character to stop the input of the (pairs) x and y.

while (std::cin.peek() != 'q') {
  point item;
  int x, y;

  if (!(std::cin >> x >> y))
    break;

  item.x = x;
  item.y = y;

  points.push_back(item);
}

>> Define the operators == and < for two point-objects

bool operator==(const point& a, const point& b)
{
  return a.x == b.x && a.y == b.y;
}

bool operator<(const point& a, const point& b)
{
  return a.x < b.x || (a.x == b.x && a.y < b.y);
}

>> Use the STL-algorithm 'sort' to sort the previous entered (pairs) x and y
>> by using the defined order operator <.
std::sort uses operator< by default, so you can simply do this:

std::sort(points.begin(), points.end());
Dogtree 23 Posting Whiz in Training

>> while (cin>> x >>" ">> y, !cin.fail())
Since you already know what the problem was, I'll tell you why it's a problem, just to be thorough. cin >> is strictly for formatted input. It doesn't accept formatting strings and it already skips whitespace by default. If you want to skip precise strings then you can do this (nonportable):

#include <iostream>

namespace std {
  template <typename CharT, typename Traits>
  basic_istream<CharT, Traits>&
    operator>>(basic_istream<CharT, Traits>& in, const char *fmt)
  {
    while (in.good() && *fmt != '\0') {
      if (in.get() != *fmt++)
        in.setstate(ios::failbit);
    }

    return in;
  }
}

int main()
{
  int a, b;

  std::cout << "Enter two numbers separated by =-=: ";
  std::cin >> a >> "=-=" >> b;
  std::cout << "a: " << a << '\n' << "b: " << b << '\n';
}

It's not portable because you technically aren't allowed to add stuff to the std namespace. You can do it portably by writing a manipulator:

#include <iostream>

class skip {
  const char *_fmt;
public:
  skip(const char *fmt): _fmt(fmt) {}

  template <typename CharT, typename Traits>
  friend std::basic_istream<CharT, Traits>&
    operator>>(std::basic_istream<CharT, Traits>& in, const skip& sk)
  {
    const char *tmp = sk._fmt;

    while (in.good() && *tmp != '\0') {
      if (in.get() != *tmp++)
        in.setstate(std::ios::failbit);
    }

    return in;
  }
};

int main()
{
  int a, b;

  std::cout << "Enter two numbers separated by =-=: ";
  std::cin >> a >> skip("=-=") >> b;
  std::cout << "a: " << a << '\n' << "b: " << b << '\n';
}

But …

Dogtree 23 Posting Whiz in Training

You know, these statements are easier to believe when you can write coherent English and provide objective arguments. You haven't done either, so you can only expect to be treated as a troll.

Dogtree 23 Posting Whiz in Training

Another option if MSDN scares you is to download the Boost::filesystem library. If you can figure out how to build it and link with it then writing your program would be a breeze. :) But I still recommend using the Win32 API, and my link is a great start.

Dogtree 23 Posting Whiz in Training

Uh, that's not C++, it's Java.

Dogtree 23 Posting Whiz in Training

How does that function not work? There are a few iffy parts, like not checking fopen for success and not considering that fread can return a non-zero failure code, but there are no obvious errors that I can see this early in the morning. ;)

Dogtree 23 Posting Whiz in Training

You're sorting by name, and name only. Just because a name goes in one spot doesn't mean the corresponding data goes in the same spot if you sort it too. This is basically what you're doing:

#include <algorithm>
#include <iostream>
#include <iterator>

template <typename T>
void bubble(T list[], int sz)
{
  for (int i = 0; i < sz; i++) {
    for (int j = i; j < sz; j++) {
      if (list[j] < list[i])
        std::swap(list[i], list[j]);
    }
  }
}

int main()
{
  int a[] = {9,8,7,6,5,4,3,2,1,0};
  int b[] = {6,7,5,8,9,0,4,3,2,1};

  copy(a, a + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';
  copy(b, b + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';

  bubble(a, 10);

  copy(a, a + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';
  copy(b, b + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';
}

To get it to work, you need to pass the other arrays and swap them too, but only use name in the test:

#include <algorithm>
#include <iostream>
#include <iterator>

template <typename T>
void bubble(T list1[], T list2[], int sz)
{
  for (int i = 0; i < sz; i++) {
    for (int j = i; j < sz; j++) {
      if (list1[j] < list1[i]) {
        std::swap(list1[i], list1[j]);
        std::swap(list2[i], list2[j]);
      }
    }
  }
}

int main()
{
  int a[] = {9,8,7,6,5,4,3,2,1,0};
  int b[] = {6,7,5,8,9,0,4,3,2,1};

  copy(a, a + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';
  copy(b, b + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';

  bubble(a, b, 10);

  copy(a, a + 10, std::ostream_iterator<int>(std::cout, " ")); …
Dogtree 23 Posting Whiz in Training

In that case the Win32 API would probably be your easiest solution. You can start browsing from here.

Dogtree 23 Posting Whiz in Training

Can you get a string from one file? If so then can you get a string from more than one file in the same directory? Do you have to search for the files according to some pattern, or do you have a list of filenames? I see this question a lot, and nobody gives enough detail. I need to know what you already know how to do. If you don't know how to open and read a file then my answering the question assuming your problem is with reading from multiple directories wouldn't help much. If you don't know how to open and read from different directories then I need to know what operating system and compiler you use because there's no portable solution for that problem.

Dogtree 23 Posting Whiz in Training

Here's a trick for fixing problems like this one. Cut out all of the fluff from your program so that it's absolute bare-bones, but still has the problem. Errors are easier to see when you don't have a lot of code all around them diverting your attention. There are three rules to follow:

1) Use a single source file. No headers except the required standard ones.
2) Output is irrelevant unless the problem is with formatting. Remove all pretty-print functionality.
3) Hardcode everything. User input is nice, but it bulks up a program.

Dogtree 23 Posting Whiz in Training

Dude, you're gonna have to give us something complete. A full program that doesn't work, and a chunk of the file that you're using so that we can actually test this and answer your question completely without all the back and forth.

Dogtree 23 Posting Whiz in Training

When you swap the name array, you'll have to swap all of the other arrays along with it, since each array is a unique object. A good alternative is to stuff everything in an structure and then sort an array of structs:

struct record {
  string name;
  // ...
} rec[N];

That would save you a lot of work because you can treat a record as a single unit rather than an index in a bunch of arrays.

Dogtree 23 Posting Whiz in Training

Either n is bigger than 50, which is the last accessible index in your array, or you aren't allocating memory to each Activity pointer in the array. You probably want to allocate the initial memory like this:

Activity **p = new Activity*[51];

for (int i = 0; i < 51; i++)
  p[i] = new Activity;

Then you release the memory like this:

for (int i = 0; i < 51; i++)
  delete [] p[i];
delete p;

Unless you do this, you'll have to initialize each pointer to an existing object before you try to dereference it.

Dogtree 23 Posting Whiz in Training

Was this what you were trying to do?

Activity **p = new Activity*[51];

for ( int i = 1; i <= n; ++i ) {
  // Read the activity records.
  pert >> (*(p+i))->i >> (*(p+i))->j >> (*(p+i))->pt 
    >> (*(p+i))->prt >> (*(p+i))->ot;
  pert.getline((*(p+i))->desc,21);
}
Dogtree 23 Posting Whiz in Training

Your best start is by reading the documentation to your compiler. There's no native support for printers in C++, and your compiler probably offers several ways to do it.