reading in strings - problem with "\n"

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
Zach Wegner
Posts: 1922
Joined: Thu Mar 09, 2006 12:51 am
Location: Earth

Re: reading in strings - problem with "\n"

Post by Zach Wegner »

If I was calling it 1000's of times a second, I'd probably do that. It consumes no time really, so I just keep it simple.
Richard Allbert
Posts: 794
Joined: Wed Jul 19, 2006 9:58 am

Re: reading in strings - problem with "\n"

Post by Richard Allbert »

This is why I hate using c++ sometimes

Code: Select all

#include <iostream>
#include <string>
#include <fstream>

using namespace std;


int main()
{
  
  ifstream inifile;
  string inputline;

  inifile.open ("test.txt",ifstream::in);

  if (!inifile)
 {
         cout<<"\n ERROR Opening ini file";
          exit(1);
 }

    while(!inifile.eof())
    {
        getline(inifile, inputline);
        cout<<"\n inputline = "<<inputline;
    }

  inifile.close();

    return 0;
}
Output?? "ERROR Opening ini file"

It's taken me longer messing about with searching the net and using my "c++ STL reference" trying to open a file using c++ than I managed to write my tourney manger in c.

And still no open file.

Richard
mathmoi
Posts: 290
Joined: Mon Mar 13, 2006 5:23 pm
Location: Québec
Full name: Mathieu Pagé

Re: reading in strings - problem with "\n"

Post by mathmoi »

Richard Allbert wrote:This is why I hate using c++ sometimes

Code: Select all

#include <iostream>
#include <string>
#include <fstream>

using namespace std;


int main()
{
  
  ifstream inifile;
  string inputline;

  inifile.open ("test.txt",ifstream::in);

  if (!inifile)
 {
         cout<<"\n ERROR Opening ini file";
          exit(1);
 }

    while(!inifile.eof())
    {
        getline(inifile, inputline);
        cout<<"\n inputline = "<<inputline;
    }

  inifile.close();

    return 0;
}
Output?? "ERROR Opening ini file"

It's taken me longer messing about with searching the net and using my "c++ STL reference" trying to open a file using c++ than I managed to write my tourney manger in c.

And still no open file.

Richard
Hi Richard, I don't think that's C++ fault :)

I compiled and ran your code and it work fine. Maybe your test.txt file is not in the current directory of the application?
Richard Allbert
Posts: 794
Joined: Wed Jul 19, 2006 9:58 am

Re: reading in strings - problem with "\n"

Post by Richard Allbert »

mathmoi wrote:
Richard Allbert wrote:This is why I hate using c++ sometimes

Code: Select all

#include <iostream>
#include <string>
#include <fstream>

using namespace std;


int main()
{
  
  ifstream inifile;
  string inputline;

  inifile.open ("test.txt",ifstream::in);

  if (!inifile)
 {
         cout<<"\n ERROR Opening ini file";
          exit(1);
 }

    while(!inifile.eof())
    {
        getline(inifile, inputline);
        cout<<"\n inputline = "<<inputline;
    }

  inifile.close();

    return 0;
}
Output?? "ERROR Opening ini file"

It's taken me longer messing about with searching the net and using my "c++ STL reference" trying to open a file using c++ than I managed to write my tourney manger in c.

And still no open file.

Richard
Hi Richard, I don't think that's C++ fault :)

I compiled and ran your code and it work fine. Maybe your test.txt file is not in the current directory of the application?
It isn't c++ fault - I compiled it on my laptop with no problems.

:oops:

But, it won't work on my desktop, which is new and has Vista, another of my hates. This has happened with a few of my programs.

A copy of XP is on order.... :D

Richard
plattyaj

Re: reading in strings - problem with "\n"

Post by plattyaj »

Richard Allbert wrote:This is why I hate using c++ sometimes

Code: Select all

  ifstream inifile;
  string inputline;

  inifile.open ("test.txt",ifstream::in);

  if (!inifile)
 {
         cout<<"\n ERROR Opening ini file";
          exit(1);
 }
Output?? "ERROR Opening ini file"
Perhaps I'm missing something but I don't see how this could ever work. inifile is constructed (default constructor) and then you call the open method. So far so good. But I don't see that if (!inifile) can work. You should use inifile.good () here to test for the file.

Andy.
Richard Allbert
Posts: 794
Joined: Wed Jul 19, 2006 9:58 am

Re: reading in strings - problem with "\n"

Post by Richard Allbert »

Agreed, but I was using "The C++ Standard Library" as reference... I assumed it would be correct.

:)
plattyaj

Re: reading in strings - problem with "\n"

Post by plattyaj »

Richard Allbert wrote:Agreed, but I was using "The C++ Standard Library" as reference... I assumed it would be correct.

:)
Actually my bad anyway because, doh!:oops: , ! is an operator that returns if failbit or badbit is set.

Although it would be interesting to test good and just confirm it still fails.

You should probably also query errno / GetLastError and see just why it failed.

Andy.
Richard Allbert
Posts: 794
Joined: Wed Jul 19, 2006 9:58 am

Re: reading in strings - problem with "\n"

Post by Richard Allbert »

Well, after all that something else odd.

I converted the program to using strings and vectors - which means no predefining of array sizes, and no "\n" problems :)

What is odd is that the program compiles & runs on my XP laptop, my work XP laptop, but compiles and fails to run properly on my vista home computer.

Any reason for that?

May well be an irrelevant question .. a new copy of XP should be arriving in the post today, and will replace Vista :) (even though I line the MS pockets a little more)

Regards

Richard
Aleks Peshkov
Posts: 895
Joined: Sun Nov 19, 2006 9:16 pm
Location: Russia
Full name: Aleks Peshkov

Re: reading in strings - problem with "\n"

Post by Aleks Peshkov »

Probably it is too late, but I know owners of Vista Bussiness can legally downgrade to XP Professional Edition without charge.
Richard Allbert
Posts: 794
Joined: Wed Jul 19, 2006 9:58 am

Re: reading in strings - problem with "\n"

Post by Richard Allbert »

Yes I heard that, but I have the home version that came preintalled :)

It'll soon be gone.... :twisted: