reading in strings - problem with "\n"
Moderator: Ras
-
- Posts: 1922
- Joined: Thu Mar 09, 2006 12:51 am
- Location: Earth
Re: reading in strings - problem with "\n"
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.
-
- Posts: 794
- Joined: Wed Jul 19, 2006 9:58 am
Re: reading in strings - problem with "\n"
This is why I hate using c++ sometimes
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
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;
}
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
-
- Posts: 290
- Joined: Mon Mar 13, 2006 5:23 pm
- Location: Québec
- Full name: Mathieu Pagé
Re: reading in strings - problem with "\n"
Hi Richard, I don't think that's C++ faultRichard Allbert wrote:This is why I hate using c++ sometimes
Output?? "ERROR Opening ini file"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; }
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

I compiled and ran your code and it work fine. Maybe your test.txt file is not in the current directory of the application?
Mathieu Pagé
mathieu@mathieupage.com
mathieu@mathieupage.com
-
- Posts: 794
- Joined: Wed Jul 19, 2006 9:58 am
Re: reading in strings - problem with "\n"
It isn't c++ fault - I compiled it on my laptop with no problems.mathmoi wrote:Hi Richard, I don't think that's C++ faultRichard Allbert wrote:This is why I hate using c++ sometimes
Output?? "ERROR Opening ini file"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; }
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
I compiled and ran your code and it work fine. Maybe your test.txt file is not in the current directory of the application?

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....

Richard
Re: reading in strings - problem with "\n"
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.Richard Allbert wrote:This is why I hate using c++ sometimes
Output?? "ERROR Opening ini file"Code: Select all
ifstream inifile; string inputline; inifile.open ("test.txt",ifstream::in); if (!inifile) { cout<<"\n ERROR Opening ini file"; exit(1); }
Andy.
-
- Posts: 794
- Joined: Wed Jul 19, 2006 9:58 am
Re: reading in strings - problem with "\n"
Agreed, but I was using "The C++ Standard Library" as reference... I assumed it would be correct.


Re: reading in strings - problem with "\n"
Actually my bad anyway because, doh!:oops: , ! is an operator that returns if failbit or badbit is set.Richard Allbert wrote:Agreed, but I was using "The C++ Standard Library" as reference... I assumed it would be correct.
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.
-
- Posts: 794
- Joined: Wed Jul 19, 2006 9:58 am
Re: reading in strings - problem with "\n"
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
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

Regards
Richard
-
- Posts: 895
- Joined: Sun Nov 19, 2006 9:16 pm
- Location: Russia
- Full name: Aleks Peshkov
Re: reading in strings - problem with "\n"
Probably it is too late, but I know owners of Vista Bussiness can legally downgrade to XP Professional Edition without charge.
-
- Posts: 794
- Joined: Wed Jul 19, 2006 9:58 am
Re: reading in strings - problem with "\n"
Yes I heard that, but I have the home version that came preintalled 
It'll soon be gone....

It'll soon be gone....
