-
Files & String ARrays
I'm having problems getting a complete string from the datafile. Whenever I use cin.getline, it always asks me to enter in values in order to get information from the file. I just want to display the name, the last name and than a description about themselves. Here is the coding to my file:
include <fstream.h> // Header file used to open/close files
#include <stdlib.h> // Header file that includes the 'exit' function
#include <cstring.h> // Header file that is used to declare variables as strings
#include <io.h> // Header file that includes the 'EOF' function
string name, lname; // Declares the variables as strings
string namearray [10]; // Declares an array
string lnamearray [10];
string agearray [10];
char age [256];
int a =-1;
void main ()
{
ifstream new_name; // Declares the input stream
new_name.open ("c:/names.txt", ios::in); // Opens the textfile
if (!new_name) // Checks to see if file is in the disk
{
cout << "Error opening file.\n"; // Displays information on to the screen
exit(1); // Exits the program
}
while (new_name)
{
a++; // Counter
new_name >> name >> lname; // Gets the data from the file
cin.getline (infob, '\n');
namearray [a] = name; // Stores it into an array
lnamearray [a] = lname;
agearray [a] = infob;
eof(0); // Ends the loop when it is at end of file
}
for (int v=0; v < a; v++)
cout << namearray [v] << " " << lnamearray [v] << " " << agearray [v] << endl; // Displays values from the file
new_name.close(); // Closes the file
}
-
>cin.getline (infob, '\n');
sould be
new_name.getline(infob, '\n');
-Prelude
-
instead of
cin.getline(infob, '\n) ;//what is infob?? you never declare it.
it should be
new_name.getline(age, 255, '\n')
or
new_name.getline(age, 255)
which uses the default terminating char for getline() which is the newline char, assuming lname, fname, and age are all on the same line, or at least age is not on a line by itself. If age is on a line by itself, then you need to call new_line.ignore() after the last use of new_line >> before the call to new_line.getline() or you will not get the results you want as >> doesn't clear the terminating char from the input stream and if that char is newline then the call to getline() will just see the "left over" newline char and put nothing in age and the data for age will end up in the next lname, etc.
Also, eof() is an ios class method inherited by instances of ifstreams and should be used as such--maybe something like this:
while(!new_name.eof())
{
//etc.
-
Hi! I changed the line to new_name.getline ! But now it only gets the last lines of the datafile. Why is that?
#include <fstream.h> // Header file used to open/close files
#include <stdlib.h> // Header file that includes the 'exit' function
#include <cstring.h> // Header file that is used to declare variables as strings
#include <io.h> // Header file that includes the 'EOF' function
char name [256];
char lname [256];
char *namearray [50]; // Declares an array
char *lnamearray [50];
char infob [200];
int a =-1;
void main ()
{
ifstream new_name; // Declares the input stream
new_name.open ("c:/names.txt", ios::in); // Opens the textfile
if (!new_name) // Checks to see if file is in the disk
{
cout << "Error opening file.\n"; // Displays information on to the screen
exit(1); // Exits the program
}
while (new_name)
{
a++; // Counter
new_name.getline (name, 255, '\n');
new_name.ignore();
namearray [a] = name; // Stores it into an array
eof(0); // Ends the loop when it is at end of file
}
for (int v=0; v < a; v++)
cout << namearray [v] << " " << endl;
// Displays values from the file
new_name.close(); // Closes the file
}