Getline error
Dec 6, 2012 at 4:37pm UTC
Hello,
I cannot use a string and must read the characters of a file into an char array. However, I'm having problems with the getline function. Also I would appreciate if someone could explain how to separate the what I'm reading in onto new lines for every new word.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <string.h>
#include <fstream>
#include <istream>
using namespace std;
//Function prototypes
void fileChecker();
void readInFile(char );
void passVal();
int main ()
{
// Variable Declaration
char passwords [40];
// Calling fileChecker function to check if passwd.txt exists
fileChecker();
// reading file into a char variable called passwords.
readInFile(passwords[40]);
}
//Function name: fileChecker
//Purpose : Check if file exists/ and is open
// Parameters :
// Returns: nothing
void fileChecker()
{
// Read in file
fstream fin;
fin.open("passwd.txt" );
// checks if file exists or not.
if (!fin.is_open())
{
cout << "Error, Text file passwd.txt does not exist in current directory." << endl;
system("PAUSE" );
}
}
//Function name: readInFile
//Purpose : Reads passwd.txt into variable
// Parameters : passwords
// Returns: nothing
void readInFile(char passwords[])
{
fstream fin;
// posChar = (passwords, '\0');
fin.open("passwd.txt" );
// while not end of file read / display passwords.
while (fin.good())
{
getline(fin, passwords);
/* for (char i; i < 40; i++)
{
fin >> passwords[i];
cout << passwords[i];
}
*/
}
}
//Function name: passVal
//Purpose : Validating passwords
// Parameters : passwords
// Returns: message stating whether or not a password is valid
void passVal(char passwords)
{
fstream fin;
while (fin.good())
{
Dec 6, 2012 at 9:28pm UTC
readInFile(passwords[40]);
passes the 40th element of passwords[] to the function.
If you want to pass the array, write readInFile(passwords);
Topic archived. No new replies allowed.