By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Simple file/string prob

P: n/a
Hi

Sorry bout this basic prob. Got a file called file.obj. tryna read the first
line from it as a string and print it to the screen. getting errors:

#include "stdafx.h"
#include <stdio.h>
#include <fstream.h>
#include <iostream.h>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string Buffer;

ifstream FileStream("File.obj");
FileStream.getline(Buffer, '\n');
cout << Buffer << endl;
FileStream.close();
return 0;
}

Also, how would i print just the first character of the string (Buffer)? Any
help much appreciated.

cheers
dave
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"David Sobey" <da**********************************@hotmail.co m> wrote in
message news:3f******@dnews.tpgi.com.au...
Hi

Sorry bout this basic prob. Got a file called file.obj. tryna read the first line from it as a string and print it to the screen. getting errors:

#include "stdafx.h"
#include <stdio.h>
#include <fstream.h>
#include <iostream.h>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string Buffer;

ifstream FileStream("File.obj");
FileStream.getline(Buffer, '\n');
cout << Buffer << endl;
FileStream.close();
return 0;
}

Also, how would i print just the first character of the string (Buffer)? Any help much appreciated.

cheers
dave


I can see any number of potential problems. When you say 'getting errors'
do you mean compile time or runtime errors? Is the file a text file or a
binary file? Why is it that you do not say what the actual errors are?
They are compile time aren't they?

Tom
Jul 22 '05 #2

P: n/a
sorry im only a beginner so i thought my errors would have been bleedingly
obvious. first, is says ifstream is ambiguous, getline doesn't accept
"Buffer", i think it needs a char pointer, also, it doesnt recognise the <<
operator.
sorry i can't be more specific, im having trouble interpreting the compiler
messages. They are all compile time errors.

cheers
dave
Jul 22 '05 #3

P: n/a
"David Sobey" <da**********************************@hotmail.co m> wrote in
message news:3f******@dnews.tpgi.com.au...
sorry im only a beginner so i thought my errors would have been bleedingly
obvious. first, is says ifstream is ambiguous, getline doesn't accept
"Buffer", i think it needs a char pointer, also, it doesnt recognise the << operator.
sorry i can't be more specific, im having trouble interpreting the compiler messages. They are all compile time errors.

cheers
dave


Okay. First off, you are using the *old* standard library. That is, the
header files with a '.h' at the end are from a previous version of the
library and things there don't necessarily work like the new ones do. Help
will be easier to come by if you use the new version (without the '.h').
Full error messages are also good.

As for as the messages you are getting:

ifstream is ambiguous could mean that it is finding more than one class
called ifstream or that there is more than one ifstream constructor that
takes an char * as the fist argument (with differing default arguments)

getline doesn't accept "Buffer", as you suspect, does need a char pointer,
and probably a buffer size too.

it doesnt recognise the << operator - possibly also complaining about
getting a string instead of a null terminated character array. If this is
the case, it should take Buffer.c_str().

As far as outputting the first character goes, and provided the string isn't
empty, Buffer[0] wil return the first character in the string.

Don't stay up for too many days in a row.

Tom

Jul 22 '05 #4

P: n/a
David Sobey escribió:
#include "stdafx.h"
#include <stdio.h>
You don't need stdio.h in this program.
#include <fstream.h>
#include <iostream.h>
Change this to:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string Buffer;

ifstream FileStream("File.obj");
FileStream.getline(Buffer, '\n');
Change this to:
getline (FileStream, Buffer, '\n');

The getline that use string is not a member.
cout << Buffer << endl;
FileStream.close();
return 0;
}

Also, how would i print just the first character of the string (Buffer)? Any
help much appreciated.


Buffer [0]

But you must test first that Buffer is not empty using Buffer.empty (),
for example.

Regards.
Jul 22 '05 #5

P: n/a
ahhh got it working. Wow c++ is NOTHING like Delphi ;). Thanks for that
guys.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.