Simple file/string prob

T

Thomas Wintschel

David Sobey said:
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
 
T

Thomas Wintschel

David Sobey said:
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
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

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 said:
#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.
 
D

David Sobey

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
 
D

David Sobey

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,241
Latest member
Lisa1997

Latest Threads

Top