|
Subject:
|
reading a row of characters from an input file
|
|
Posted By:
|
Omega_st1
|
Post Date:
|
10/15/2003 5:45:06 PM
|
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 By:
|
Omega_st1
|
Reply Date:
|
10/15/2003 6:23:08 PM
|
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 By:
|
Gert
|
Reply Date:
|
10/16/2003 4:07:18 AM
|
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 By:
|
jdcohen
|
Reply Date:
|
10/20/2003 12:42:51 PM
|
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 By:
|
programmed
|
Reply Date:
|
10/28/2003 12:35:16 AM
|
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.
|