Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
| Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 15th, 2003, 05:45 PM
Registered User
 
Join Date: Oct 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default reading a row of characters from an input file

Ok, I am trying to read a row of characters which should be a single string from an input file. the input file is a plain text .dat file that has each piece of data on a separate line. In my program, I have a variable which reads the character data with a 'char' data type.

problem is, it only reads the first character and that's it.
Because I'm new to C++ i'm not sure what data type i can use to have it read the entire line of characters as one. Please help!!

Reply With Quote
  #2 (permalink)  
Old October 15th, 2003, 06:23 PM
Registered User
 
Join Date: Oct 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, I'd like to take data from an input file that consists
of a string of characters, e.g. "Hello Dolly" the thing
is, I have the data type for this data setup as char, and
it returns only the first character "H"...how can I fix
this? Does it have something to do with the data type?

Reply With Quote
  #3 (permalink)  
Old October 16th, 2003, 04:07 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In Visual C++ there is a CString class you could use. I do not remember what the appropriate class is in regular C++, but maybe String?

Gert
Reply With Quote
  #4 (permalink)  
Old October 20th, 2003, 12:42 PM
Registered User
 
Join Date: Sep 2003
Location: Carlsbad, CA, .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You want the string class from the C++ standard library. It, like any other type from the standard library, exists in the 'std' namespace, so you access it by prefixing it with the namespace and the :: operator, like std::string. You have to #include <string> to get the class definition.

Likewise, this header file defines a "getline" operation that reads a line from an input stream into the string object. You can use this with the "cin" input stream to read from the console, or a file input stream you've opened to read from your file. This function, like other standard functions, is also in namespace std.

// Read a line from the console and echo it
std::string line;
std::getline(std::cin, line);
std::cout << line;

// Read a line from a file and echo it
std::string line;
std::ifstream infile("myfile.txt");
std::getline(infile, line);
std::cout << line;

Naturally you will probably want to check the stream after the getline call because, like any other stream operation, getline can fail (if, for example, there was nothing in the file). A stream can be tested as a boolean value for convenience, such as in an "if" statement. If it is "true" then the stream is still good. If it is "false" then the stream has failed somehow.

// Read a line
std::string line;
if (! std::getline(std::cin, line))
{
    std::cerr << "Unable to read a line.\n";
}
else
{
    std::cout << "Read a line: " << line << "\n";
}

Regards,
Jake.

Regards,
Jake.
Reply With Quote
  #5 (permalink)  
Old October 28th, 2003, 01:35 AM
Authorized User
 
Join Date: Oct 2003
Location: Kampala, , Uganda.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 i have something like char *name[10];
 cout<<"enter a name<<endl"
 gets(name);
  puts(name);
 This gets name as a string and later prints it on the stream using those two i/o functions. The compiler might find it necessary to include <cstring> header file since you're dealing with strings.

Bye, Programmed.

Reply With Quote




Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading input from files PROLOG Ruman29 Other Programming Languages 0 March 8th, 2007 12:49 PM
Reading Console Input code_lover Java Basics 4 February 14th, 2007 09:08 AM
Reading string data from an input file Omega_st1 Visual C++ 0 October 15th, 2003 05:10 PM
reading first characters from a cell in excel vdriet Excel VBA 2 August 17th, 2003 10:10 AM
Multiple file input single file output.... jdm_mboy Biztalk 1 July 2nd, 2003 03:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.