Simple file/string prob



 DEVELOP > c-Plus-Plus > Simple file/string prob

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "David Sobey"
Date: 23 Nov 2003 09:01:07 PM
Object: Simple file/string prob
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
.

User: "Thomas Wintschel"

Title: Re: Simple file/string prob 23 Nov 2003 02:20:09 AM
"David Sobey" <davefromalbury_REMOVE_THIS_TO_EMAIL_@hotmail.com> wrote in
message news:3fc06953@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
.
User: "David Sobey"

Title: Re: Simple file/string prob 23 Nov 2003 09:51:34 PM
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
.
User: "Thomas Wintschel"

Title: Re: Simple file/string prob 23 Nov 2003 03:20:43 AM
"David Sobey" <davefromalbury_REMOVE_THIS_TO_EMAIL_@hotmail.com> wrote in
message news:3fc07529@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
.



User: "=?iso-8859-1?Q?Juli=E1n?= Albo"

Title: Re: Simple file/string prob 23 Nov 2003 04:47:18 AM
David Sobey escribi=F3:

#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.
.
User: "Dave"

Title: Re: Simple file/string prob 24 Nov 2003 12:22:25 AM
ahhh got it working. Wow c++ is NOTHING like Delphi ;). Thanks for that
guys.
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER