473,785 Members | 2,465 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Fil e.obj");
FileStream.getl ine(Buffer, '\n');
cout << Buffer << endl;
FileStream.clos e();
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
5 4984
"David Sobey" <da************ *************** *******@hotmail .com> wrote in
message news:3f******@d news.tpgi.com.a u...
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("Fil e.obj");
FileStream.getl ine(Buffer, '\n');
cout << Buffer << endl;
FileStream.clos e();
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
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
"David Sobey" <da************ *************** *******@hotmail .com> wrote in
message news:3f******@d news.tpgi.com.a u...
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
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("Fil e.obj");
FileStream.getl ine(Buffer, '\n');
Change this to:
getline (FileStream, Buffer, '\n');

The getline that use string is not a member.
cout << Buffer << endl;
FileStream.clos e();
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
ahhh got it working. Wow c++ is NOTHING like Delphi ;). Thanks for that
guys.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2082
by: Halfdan Holger Knudsen | last post by:
goodday all - here's a snippet of code that just doesn't seem to work...at all! But I haven't got the faintest idea why I keep getting an unref. local var. error. Take a look - I know I can get good help here. And PS: Thanks beforehand. If you're wondering - it's an attempt at a (crude) reproduction of the string.split() functionality for learning purposes. ---------------------- #! /usr/bin/env python
8
3296
by: Stewart | last post by:
is there any way this can be done? I've looked at the help files and checked out as many tutorials as i could find on the net (as always) but no joy. thanks
0
1693
by: Jörg Braun | last post by:
Hello NG, i have a problem With WebRequest! i want to download a htaccess saved file over a proxyconnetion with authentication. Download a file what is not saved with htaccess is no problem, download a htaccess saved file with out a proxy with authentication i also no problem, but download this file with htaccess and the auth proxy is a prob!
12
11755
by: Brett Hofer | last post by:
I must be missing something - Im a veteran C++ programmer now working with C# overall I like the language but find many weird changes... Anyway Im writing code behind an aspx. In this one C# method I am building an XML string to be insterted into a database. This string should result in: <row FIELD1="value1" FIELD2="value2" \> I am using a string type variable and I cannot get the double quotes to be added properly I have tried all of...
4
12808
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions please ???
4
1527
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those who experience. (For some reason I have to implement Array not ArrayLists) Below are the simple following code: Dim Array() As String Dim intCounter As Integer
7
3190
by: pbd22 | last post by:
hi. i am having probs understanding how to grab a file being uploaded from a remote client. i am using hidden input fields for upload such as: <input id="my_file_element" type="file" name="file_1" size=46 /><input type=submit /> so, after adding a few files, the input fields look like this:
0
1251
by: Greg Corradini | last post by:
Hello all, I'm having trouble inserting an SQL selection into a new MS Access table. I get a parameter error on my insert statement when I try this (see below for code and error msg). I'm not sure if 'insert' or 'update' is the route I should be taking. CODE: #Import Pythond Standard Library Modules import win32com.client, sys, os, string, copy, glob import mx.ODBC.Windows as odbc
8
2477
by: Huma123 | last post by:
Hi! My problem is that i cant able to run a simple c program cause of this error... Unable to open include file 'stdio.h' unable to open include file 'conio.h' while these header files r present in my include files folder. i tried to write code to 2 different styles as i found in this forum someone has this prob too u people ans him that..
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.